Skip to content

Commit

Permalink
Fixed loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Jul 27, 2024
1 parent 60b392d commit 9b012b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.maxrave.simpmusic.viewModel.FilterState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -150,7 +151,9 @@ class SimpleMediaServiceHandler(

private var normalizeVolume = false

private var job: Job? = null
private var progressJob: Job? = null

private var bufferedJob : Job? = null

private var updateNotificationJob: Job? = null

Expand All @@ -167,7 +170,8 @@ class SimpleMediaServiceHandler(

init {
player.addListener(this)
job = Job()
progressJob = Job()
bufferedJob = Job()
sleepTimerJob = Job()
volumeNormalizationJob = Job()
updateNotificationJob = Job()
Expand Down Expand Up @@ -793,32 +797,32 @@ class SimpleMediaServiceHandler(
isPlaying,
)
if (isPlaying) {
coroutineScope.launch(Dispatchers.Main) {
startProgressUpdate()
}
startProgressUpdate()
} else {
stopProgressUpdate()
mayBeSaveRecentSong()
}
updateNextPreviousTrackAvailability()
}

private suspend fun startProgressUpdate() =
job.run {
private fun startProgressUpdate() {
progressJob = coroutineScope.launch {
while (true) {
delay(100)
_simpleMediaState.value = SimpleMediaState.Progress(player.currentPosition)
}
}
}

private suspend fun startBufferedUpdate() =
job.run {
private fun startBufferedUpdate() {
bufferedJob = coroutineScope.launch {
while (true) {
delay(500)
_simpleMediaState.value =
SimpleMediaState.Loading(player.bufferedPercentage, player.duration)
}
}
}

fun loadMore() {
// Separate local and remote data
Expand Down Expand Up @@ -925,11 +929,12 @@ class SimpleMediaServiceHandler(
}

private fun stopProgressUpdate() {
job?.cancel()
progressJob?.cancel()
Log.w(TAG, "stopProgressUpdate: ${progressJob?.isActive}")
}

private fun stopBufferedUpdate() {
job?.cancel()
bufferedJob?.cancel()
_simpleMediaState.value =
SimpleMediaState.Loading(player.bufferedPercentage, player.duration)
}
Expand All @@ -939,9 +944,7 @@ class SimpleMediaServiceHandler(
_simpleMediaState.value =
SimpleMediaState.Loading(player.bufferedPercentage, player.duration)
if (isLoading) {
coroutineScope.launch(Dispatchers.Main) {
startBufferedUpdate()
}
startBufferedUpdate()
} else {
stopBufferedUpdate()
}
Expand Down Expand Up @@ -1153,9 +1156,13 @@ class SimpleMediaServiceHandler(
player.playWhenReady = false
player.removeListener(this)
sendCloseEqualizerIntent()
if (job?.isActive == true) {
job?.cancel()
job = null
if (progressJob?.isActive == true) {
progressJob?.cancel()
progressJob = null
}
if (bufferedJob?.isActive == true) {
bufferedJob?.cancel()
bufferedJob = null
}
if (sleepTimerJob?.isActive == true) {
sleepTimerJob?.cancel()
Expand All @@ -1177,7 +1184,9 @@ class SimpleMediaServiceHandler(
loadJob?.cancel()
loadJob = null
}
Log.w("Service", "Check job: ${job?.isActive}")
if (coroutineScope.isActive) {
coroutineScope.cancel()
}
Log.w("Service", "scope is active: ${coroutineScope.isActive}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ constructor(
@Inject
lateinit var downloadUtils: DownloadUtils

private var restoreLastPlayedTrackDone: Boolean = false

var simpleMediaServiceHandler: SimpleMediaServiceHandler? = null

private var _songDB: MutableLiveData<SongEntity?> = MutableLiveData()
Expand Down Expand Up @@ -160,11 +158,8 @@ constructor(

val intent: MutableStateFlow<Intent?> = MutableStateFlow(null)


private var getFormatFlowJob: Job? = null

private var getLyricsJob: Job? = null

var playlistId: MutableStateFlow<String?> = MutableStateFlow(null)

private var _listYouTubeLiked: MutableStateFlow<ArrayList<String>?> = MutableStateFlow(null)
Expand Down Expand Up @@ -1587,14 +1582,6 @@ sealed class UIEvent {
data object ToggleLike: UIEvent()
}

sealed class UIState {
object Initial : UIState()

object Ready : UIState()

object Ended : UIState()
}

enum class LyricsProvider {
MUSIXMATCH,
YOUTUBE,
Expand Down

0 comments on commit 9b012b9

Please sign in to comment.