Skip to content

Commit

Permalink
Improve NowPlayingScreen, User can turn off translucent bottom naviga…
Browse files Browse the repository at this point in the history
…tion bar in Settings
  • Loading branch information
maxrave-dev committed Jul 22, 2024
1 parent 491e017 commit 864bbe2
Show file tree
Hide file tree
Showing 22 changed files with 877 additions and 717 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,25 @@ class DataStoreManager
}
}

val translucentBottomBar =
settingsDataStore.data.map { preferences ->
preferences[TRANSLUCENT_BOTTOM_BAR] ?: TRUE
}

suspend fun setTranslucentBottomBar(translucent: Boolean) {
withContext(Dispatchers.IO) {
if (translucent) {
settingsDataStore.edit { settings ->
settings[TRANSLUCENT_BOTTOM_BAR] = TRUE
}
} else {
settingsDataStore.edit { settings ->
settings[TRANSLUCENT_BOTTOM_BAR] = FALSE
}
}
}
}

companion object Settings {
val COOKIE = stringPreferencesKey("cookie")
val LOGGED_IN = stringPreferencesKey("logged_in")
Expand All @@ -562,8 +581,8 @@ class DataStoreManager
val SEND_BACK_TO_GOOGLE = stringPreferencesKey("send_back_to_google")
val FROM_SAVED_PLAYLIST = stringPreferencesKey("from_saved_playlist")
val MUSIXMATCH_LOGGED_IN = stringPreferencesKey("musixmatch_logged_in")
val YOUTUBE = "youtube"
val MUSIXMATCH = "musixmatch"
const val YOUTUBE = "youtube"
const val MUSIXMATCH = "musixmatch"
val LYRICS_PROVIDER = stringPreferencesKey("lyrics_provider")
val TRANSLATION_LANGUAGE = stringPreferencesKey("translation_language")
val USE_TRANSLATION_LANGUAGE = stringPreferencesKey("use_translation_language")
Expand All @@ -582,6 +601,7 @@ class DataStoreManager
val SPOTIFY_CLIENT_TOKEN = stringPreferencesKey("spotify_client_token")
val HOME_LIMIT = intPreferencesKey("home_limit")
val CHART_KEY = stringPreferencesKey("chart_key")
val TRANSLUCENT_BOTTOM_BAR = stringPreferencesKey("translucent_bottom_bar")
const val REPEAT_MODE_OFF = "REPEAT_MODE_OFF"
const val REPEAT_ONE = "REPEAT_ONE"
const val REPEAT_ALL = "REPEAT_ALL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2278,11 +2278,15 @@ class MainRepository
videoId: String
) {
localDataSource.getNewFormat(videoId)?.let { oldFormat ->
Log.w("Stream", "oldFormatExpired: ${oldFormat.expiredTime}")
Log.w("Stream", "now: ${LocalDateTime.now()}")
Log.w("Stream", "isExpired: ${oldFormat.expiredTime.isBefore(LocalDateTime.now())}")
if (oldFormat.expiredTime.isBefore(LocalDateTime.now())) {
YouTube.player(videoId).onSuccess { triple ->
val response = triple.second
localDataSource.updateNewFormat(
oldFormat.copy(
expiredTime = LocalDateTime.now().plusSeconds(response.streamingData?.expiresInSeconds?.toLong() ?: 0L),
playbackTrackingVideostatsPlaybackUrl =
response.playbackTracking?.videostatsPlaybackUrl?.baseUrl?.replace(
"https://s.youtube.com",
Expand Down Expand Up @@ -2356,6 +2360,7 @@ class MainRepository
}
Log.w("Stream", "format: $format")
Log.d("Stream", "expireInSeconds ${response.streamingData?.expiresInSeconds}")
Log.w("Stream", "expired at ${LocalDateTime.now().plusSeconds(response.streamingData?.expiresInSeconds?.toLong() ?: 0L)}")
runBlocking {
insertNewFormat(
NewFormatEntity(
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/maxrave/simpmusic/extension/UIExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.maxrave.simpmusic.extension

import android.content.Context
import android.view.View
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
Expand Down Expand Up @@ -32,6 +33,7 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.ColorUtils
import com.kmpalette.palette.graphics.Palette
import com.maxrave.simpmusic.R
Expand Down Expand Up @@ -316,4 +318,6 @@ fun LazyListState.animateScrollAndCentralizeItem(index: Int, scope: CoroutineSco
this@animateScrollAndCentralizeItem.animateScrollToItem(index)
}
}
}
}
@Composable
fun KeepScreenOn() = AndroidView({ View(it).apply { keepScreenOn = true } })
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.extractor.ExtractorsFactory
import androidx.media3.extractor.mkv.MatroskaExtractor
import androidx.media3.extractor.mp4.FragmentedMp4Extractor
import androidx.media3.extractor.text.DefaultSubtitleParserFactory
import androidx.media3.session.DefaultMediaNotificationProvider
import androidx.media3.session.MediaController
import androidx.media3.session.MediaLibraryService
Expand Down Expand Up @@ -275,7 +276,7 @@ class SimpleMediaService : MediaLibraryService() {
if (dataSpec.length >= 0) dataSpec.length else 1
) || playerCache.isCached(mediaId, dataSpec.position, CHUNK_LENGTH)
) {
coroutineScope.launch {
coroutineScope.launch(Dispatchers.IO) {
mainRepository.updateFormat(
if (mediaId.contains(MergingMediaSourceFactory.isVideo)) {
mediaId.removePrefix(MergingMediaSourceFactory.isVideo)
Expand Down Expand Up @@ -315,9 +316,15 @@ class SimpleMediaService : MediaLibraryService() {
@UnstableApi
fun provideExtractorFactory(): ExtractorsFactory = ExtractorsFactory {
arrayOf(
MatroskaExtractor(),
FragmentedMp4Extractor(),
androidx.media3.extractor.mp4.Mp4Extractor(),
MatroskaExtractor(
DefaultSubtitleParserFactory()
),
FragmentedMp4Extractor(
DefaultSubtitleParserFactory()
),
androidx.media3.extractor.mp4.Mp4Extractor(
DefaultSubtitleParserFactory()
),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.ASC
import com.maxrave.simpmusic.common.DESC
import com.maxrave.simpmusic.common.LOCAL_PLAYLIST_ID
import com.maxrave.simpmusic.common.LOCAL_PLAYLIST_ID_SAVED_QUEUE
import com.maxrave.simpmusic.common.MEDIA_CUSTOM_COMMAND
import com.maxrave.simpmusic.data.dataStore.DataStoreManager
import com.maxrave.simpmusic.data.db.entities.SongEntity
Expand All @@ -42,7 +43,9 @@ import com.maxrave.simpmusic.extension.getScreenSize
import com.maxrave.simpmusic.extension.isVideo
import com.maxrave.simpmusic.extension.toArrayListTrack
import com.maxrave.simpmusic.extension.toListName
import com.maxrave.simpmusic.extension.toMediaItem
import com.maxrave.simpmusic.extension.toSongEntity
import com.maxrave.simpmusic.extension.toTrack
import com.maxrave.simpmusic.service.test.source.MergingMediaSourceFactory
import com.maxrave.simpmusic.ui.widget.BasicWidget
import com.maxrave.simpmusic.utils.Resource
Expand Down Expand Up @@ -176,6 +179,7 @@ class SimpleMediaServiceHandler(
mediaSessionCallback.apply {
toggleLike = ::toggleLike
}
mayBeRestoreQueue()
}
private var getDataOfNowPlayingTrackStateJob: Job? = null
private fun getDataOfNowPlayingState(mediaItem: MediaItem) {
Expand All @@ -194,19 +198,6 @@ class SimpleMediaServiceHandler(
updateWidget(mediaItem)
getDataOfNowPlayingTrackStateJob?.cancel()
getDataOfNowPlayingTrackStateJob = coroutineScope.launch {
if (track != null) {
mainRepository.updateSongInLibrary(
LocalDateTime.now(),
track.videoId,
)
mainRepository.updateListenCount(track.videoId)
track.durationSeconds?.let {
mainRepository.updateDurationSeconds(
it,
track.videoId,
)
}
}
Log.w(TAG, "getDataOfNowPlayingState: $videoId")
mainRepository.getSongById(videoId).cancellable().singleOrNull().let { songEntity ->
if (songEntity != null) {
Expand Down Expand Up @@ -409,6 +400,7 @@ class SimpleMediaServiceHandler(
PlayerEvent.Stop -> {
stopProgressUpdate()
player.stop()
_nowPlayingState.value = NowPlayingTrackState.initial()
}

is PlayerEvent.UpdateProgress -> player.seekTo((player.duration * playerEvent.newProgress / 100).toLong())
Expand Down Expand Up @@ -599,6 +591,7 @@ class SimpleMediaServiceHandler(
}
} else {
stopProgressUpdate()
mayBeSaveRecentSong()
}
updateNextPreviousTrackAvailability()
}
Expand Down Expand Up @@ -795,6 +788,29 @@ class SimpleMediaServiceHandler(
player.skipSilenceEnabled = skipSilent
}

private fun mayBeRestoreQueue() {
coroutineScope.launch {
if (dataStoreManager.saveRecentSongAndQueue.first() == DataStoreManager.TRUE) {
val currentPlayingTrack = mainRepository.getSongById(dataStoreManager.recentMediaId.first()).singleOrNull()?.toTrack()
if (currentPlayingTrack != null) {
val queue = mainRepository.getSavedQueue().singleOrNull()
setQueueData(
QueueData(
listTracks = queue?.firstOrNull()?.listTrack?.toCollection(arrayListOf()) ?: arrayListOf(currentPlayingTrack),
firstPlayedTrack = currentPlayingTrack,
playlistId = LOCAL_PLAYLIST_ID_SAVED_QUEUE,
playlistName = dataStoreManager.playlistFromSaved.first(),
playlistType = PlaylistType.PLAYLIST,
continuation = null
)
)
addMediaItem(currentPlayingTrack.toMediaItem(), playWhenReady = false)
seekTo(dataStoreManager.recentPosition.first())
}
}
}
}

private fun updateWidget(nowPlaying: MediaItem) {
basicWidget.performUpdate(
context,
Expand Down Expand Up @@ -854,26 +870,19 @@ class SimpleMediaServiceHandler(


fun mayBeSaveRecentSong() {
runBlocking {
coroutineScope.launch {
if (dataStoreManager.saveRecentSongAndQueue.first() == DataStoreManager.TRUE) {
dataStoreManager.saveRecentSong(
player.currentMediaItem?.mediaId ?: "",
player.contentPosition,
)
dataStoreManager.setPlaylistFromSaved(queueData.value?.playlistName ?: "")
Log.d("Check saved", player.currentMediaItem?.mediaMetadata?.title.toString())
val temp: ArrayList<Track> = ArrayList()
temp.clear()
temp.addAll(_queueData.value?.listTracks ?: arrayListOf())
temp.find { it.videoId == player.currentMediaItem?.mediaId }?.let { track ->
temp.remove(track)
}
Log.w("Check recover queue", temp.toString())
mainRepository.recoverQueue(temp)
dataStoreManager.putString(
DataStoreManager.RESTORE_LAST_PLAYED_TRACK_AND_QUEUE_DONE,
DataStoreManager.FALSE,
)
dataStoreManager.setPlaylistFromSaved(queueData.first()?.playlistName ?: "")
}
}
}
Expand Down Expand Up @@ -1577,6 +1586,10 @@ data class NowPlayingTrackState(
val track: Track?,
val songEntity: SongEntity?
) {
fun isNotEmpty(): Boolean {
return this != initial()
}

companion object {
fun initial(): NowPlayingTrackState {
return NowPlayingTrackState(
Expand Down
Loading

0 comments on commit 864bbe2

Please sign in to comment.