Skip to content

Commit

Permalink
Fix notification seek position not updating on some transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed Oct 30, 2024
1 parent 6019518 commit 60bb637
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ actual class PlatformExternalPlayerService: ForegroundPlayerService(play_when_re
}

override fun seekTo(index: Int, position_ms: Long) {
println("PROXY SEEK $index $position_ms")
server.seekToSong(index)
server.seekTo(position_ms)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class NotificationState(
val playback_state: Int? = PlaybackState.STATE_NONE,
val paused: Boolean = true,
val current_liked_status: SongLikedStatus? = null,
val authenticated: Boolean = false
val authenticated: Boolean = false,
val position_ms: Long? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.withContext

class NotificationStateManager(
private val media_session: MediaSession,
private val player: Player
) {
class NotificationStateManager(private val media_session: MediaSession) {
var current: NotificationState = NotificationState()
private set

Expand All @@ -27,14 +24,16 @@ class NotificationStateManager(
playback_state: Int? = current.playback_state,
paused: Boolean = current.paused,
current_liked_status: SongLikedStatus? = current.current_liked_status,
authenticated: Boolean = current.authenticated
authenticated: Boolean = current.authenticated,
position_ms: Long? = current.position_ms
) {
val new_state: NotificationState =
NotificationState(
playback_state,
paused,
current_liked_status,
authenticated
authenticated,
position_ms
)

if (new_state == current) {
Expand All @@ -58,7 +57,7 @@ class NotificationStateManager(
state_builder.setState(
playback_state
?: if (paused) PlaybackState.STATE_PAUSED else PlaybackState.STATE_PLAYING,
player.currentPosition,
position_ms ?: 0,
if (paused) 0f else 1f,
SystemClock.elapsedRealtime()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class PlayerServiceNotificationManager(
private val media_session: MediaSession,
private val notification_manager: NotificationManager,
private val service: ForegroundPlayerService,
player: Player
private val player: Player
) {
private var current_song: Song? = null
private val thumbnail_load_scope: CoroutineScope = CoroutineScope(Job())
private val auth_state_observe_scope: CoroutineScope = CoroutineScope(Job())
private val song_liked_load_scope: CoroutineScope = CoroutineScope(Job())

private val metadata_builder: MediaMetadata.Builder = MediaMetadata.Builder()
private val state: NotificationStateManager = NotificationStateManager(media_session, player)
private val state: NotificationStateManager = NotificationStateManager(media_session)

private val notification_listener: PlayerNotificationManager.NotificationListener =
object : PlayerNotificationManager.NotificationListener {
Expand Down Expand Up @@ -85,7 +85,10 @@ class PlayerServiceNotificationManager(
}

current_song = song
state.update(current_liked_status = song?.Liked?.get(context.database))
state.update(
current_liked_status = song?.Liked?.get(context.database),
position_ms = player.currentPosition
)

if (song != null) {
context.database.songQueries.likedById(song.id).addListener(song_liked_listener)
Expand All @@ -102,10 +105,18 @@ class PlayerServiceNotificationManager(
}
}

override fun onSeeked(position_ms: Long) {
state.update(position_ms = position_ms)
}

override fun onPlayingChanged(is_playing: Boolean) {
state.update(paused = !is_playing)
}

override fun onEvents() {
state.update(position_ms = player.currentPosition)
}

override fun onStateChanged(state: SpMsPlayerState) {
this@PlayerServiceNotificationManager.state.update(
playback_state =
Expand Down

0 comments on commit 60bb637

Please sign in to comment.