[MusicService] Save songs to history and play count with Coroutines

This commit is contained in:
Prathamesh More 2022-05-17 19:34:25 +05:30
parent 17f104b27a
commit d877bfbdfb
3 changed files with 26 additions and 57 deletions

View file

@ -89,7 +89,7 @@ class AppWidgetCircle : BaseAppWidget() {
).toBitmap() ).toBitmap()
) )
val isFavorite = runBlocking(Dispatchers.IO) { val isFavorite = runBlocking(Dispatchers.IO) {
return@runBlocking MusicUtil.repository.isSongFavorite(song.id) return@runBlocking MusicUtil.isFavorite(song)
} }
val favoriteRes = val favoriteRes =
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border

View file

@ -303,38 +303,12 @@ class MusicService : MediaBrowserServiceCompat(),
initNotification() initNotification()
mediaStoreObserver = MediaStoreObserver(this, playerHandler!!) mediaStoreObserver = MediaStoreObserver(this, playerHandler!!)
throttledSeekHandler = ThrottledSeekHandler(this, playerHandler!!) throttledSeekHandler = ThrottledSeekHandler(this, playerHandler!!)
contentResolver contentResolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
.registerContentObserver( true,
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mediaStoreObserver mediaStoreObserver)
) contentResolver.registerContentObserver(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
contentResolver true,
.registerContentObserver( mediaStoreObserver)
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Albums.INTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Artists.INTERNAL_CONTENT_URI, true, mediaStoreObserver
)
contentResolver
.registerContentObserver(
MediaStore.Audio.Genres.INTERNAL_CONTENT_URI, true, mediaStoreObserver
)
val audioVolumeObserver = AudioVolumeObserver(this) val audioVolumeObserver = AudioVolumeObserver(this)
audioVolumeObserver.register(AudioManager.STREAM_MUSIC, this) audioVolumeObserver.register(AudioManager.STREAM_MUSIC, this)
registerOnSharedPreferenceChangedListener(this) registerOnSharedPreferenceChangedListener(this)
@ -858,7 +832,8 @@ class MusicService : MediaBrowserServiceCompat(),
fun toggleFavorite() { fun toggleFavorite() {
serviceScope.launch { serviceScope.launch {
toggleFavorite(this@MusicService, currentSong) toggleFavorite(currentSong)
sendBroadcast(Intent(FAVORITE_STATE_CHANGED))
} }
} }
@ -874,6 +849,7 @@ class MusicService : MediaBrowserServiceCompat(),
fun quit() { fun quit() {
pause() pause()
stopForeground(true) stopForeground(true)
isForeground = false
notificationManager?.cancel(PlayingNotification.NOTIFICATION_ID) notificationManager?.cancel(PlayingNotification.NOTIFICATION_ID)
playbackManager.release() playbackManager.release()
AudioManagerCompat.abandonAudioFocusRequest(audioManager!!, AudioManagerCompat.abandonAudioFocusRequest(audioManager!!,
@ -1066,7 +1042,8 @@ class MusicService : MediaBrowserServiceCompat(),
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName) .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, (getPosition() + 1).toLong()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER,
(getPosition() + 1).toLong())
.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year.toLong()) .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year.toLong())
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null)
.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, playingQueue.size.toLong()) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, playingQueue.size.toLong())
@ -1128,17 +1105,6 @@ class MusicService : MediaBrowserServiceCompat(),
playingNotification?.updateFavorite(isFavorite) playingNotification?.updateFavorite(isFavorite)
startForegroundOrNotify() startForegroundOrNotify()
} }
updateMediaSessionMetaData()
updateMediaSessionPlaybackState()
savePosition()
savePositionInTrack()
val currentSong = currentSong
HistoryStore.getInstance(this).addSongId(currentSong.id)
if (songPlayCountHelper.shouldBumpPlayCount()) {
SongPlayCountStore.getInstance(this).bumpPlayCount(songPlayCountHelper.song.id)
}
songPlayCountHelper.notifySongChanged(currentSong)
} }
META_CHANGED -> { META_CHANGED -> {
playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() } playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() }
@ -1149,14 +1115,17 @@ class MusicService : MediaBrowserServiceCompat(),
updateMediaSessionMetaData() updateMediaSessionMetaData()
updateMediaSessionPlaybackState() updateMediaSessionPlaybackState()
savePosition() serviceScope.launch(IO) {
savePositionInTrack() savePosition()
val currentSong = currentSong savePositionInTrack()
HistoryStore.getInstance(this).addSongId(currentSong.id) val currentSong = currentSong
if (songPlayCountHelper.shouldBumpPlayCount()) { HistoryStore.getInstance(this@MusicService).addSongId(currentSong.id)
SongPlayCountStore.getInstance(this).bumpPlayCount(songPlayCountHelper.song.id) if (songPlayCountHelper.shouldBumpPlayCount()) {
SongPlayCountStore.getInstance(this@MusicService)
.bumpPlayCount(songPlayCountHelper.song.id)
}
songPlayCountHelper.notifySongChanged(currentSong)
} }
songPlayCountHelper.notifySongChanged(currentSong)
} }
QUEUE_CHANGED -> { QUEUE_CHANGED -> {
mediaSession?.setQueueTitle(getString(R.string.now_playing_queue)) mediaSession?.setQueueTitle(getString(R.string.now_playing_queue))
@ -1394,7 +1363,8 @@ class MusicService : MediaBrowserServiceCompat(),
const val ACTION_QUIT = "$RETRO_MUSIC_PACKAGE_NAME.quitservice" const val ACTION_QUIT = "$RETRO_MUSIC_PACKAGE_NAME.quitservice"
const val ACTION_PENDING_QUIT = "$RETRO_MUSIC_PACKAGE_NAME.pendingquitservice" const val ACTION_PENDING_QUIT = "$RETRO_MUSIC_PACKAGE_NAME.pendingquitservice"
const val INTENT_EXTRA_PLAYLIST = RETRO_MUSIC_PACKAGE_NAME + "intentextra.playlist" const val INTENT_EXTRA_PLAYLIST = RETRO_MUSIC_PACKAGE_NAME + "intentextra.playlist"
const val INTENT_EXTRA_SHUFFLE_MODE = "$RETRO_MUSIC_PACKAGE_NAME.intentextra.shufflemode" const val INTENT_EXTRA_SHUFFLE_MODE =
"$RETRO_MUSIC_PACKAGE_NAME.intentextra.shufflemode"
const val APP_WIDGET_UPDATE = "$RETRO_MUSIC_PACKAGE_NAME.appreciate" const val APP_WIDGET_UPDATE = "$RETRO_MUSIC_PACKAGE_NAME.appreciate"
const val EXTRA_APP_WIDGET_NAME = RETRO_MUSIC_PACKAGE_NAME + "app_widget_name" const val EXTRA_APP_WIDGET_NAME = RETRO_MUSIC_PACKAGE_NAME + "app_widget_name"

View file

@ -332,8 +332,8 @@ object MusicUtil : KoinComponent {
return false return false
} }
val repository = get<Repository>() private val repository = get<Repository>()
suspend fun toggleFavorite(context: Context, song: Song) { suspend fun toggleFavorite(song: Song) {
withContext(IO) { withContext(IO) {
val playlist: PlaylistEntity = repository.favoritePlaylist() val playlist: PlaylistEntity = repository.favoritePlaylist()
val songEntity = song.toSongEntity(playlist.playListId) val songEntity = song.toSongEntity(playlist.playListId)
@ -343,7 +343,6 @@ object MusicUtil : KoinComponent {
} else { } else {
repository.insertSongs(listOf(song.toSongEntity(playlist.playListId))) repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
} }
context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
} }
} }