Skip to content

Commit

Permalink
fix: incremental count and change to channelFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
techmaved committed Mar 11, 2024
1 parent 9e8fa38 commit 284d107
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ fun GetSongsButton(
MediaItemTree.populateMediaTree().collect {
CoroutineScope(Dispatchers.IO).launch {
mediaItemDao.inset(it)
countState.value++
}

countState.value += countState.value
}

loadingState.value = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import com.adamratzman.spotify.models.Track
import com.google.common.collect.ImmutableList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -209,7 +211,7 @@ object MediaItemTree {
treeNodes[ROOT_ID]!!.addChildren(arrayOf(LIKED_SONG_ID, PLAYLIST_ID, ALBUM_ID, SHOW_ID))
}

suspend fun populateMediaTree(): Flow<de.techmaved.mediabrowserforspotify.entities.MediaItem> = flow {
suspend fun populateMediaTree(): Flow<de.techmaved.mediabrowserforspotify.entities.MediaItem> = channelFlow {
username = guardValidSpotifyApi { api: SpotifyClientApi -> api.getUserId() }

spotifyWebApiService.getPlaylists(username)?.forEach { simplePlaylist: SimplePlaylist ->
Expand All @@ -221,14 +223,14 @@ object MediaItemTree {
LIKED_SONG_ID,
simplePlaylist.uri.uri
)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())
}
}
return@forEach
}

addBrowsableToTree(simplePlaylist.name, simplePlaylist.id, PLAYLIST_ID)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())

spotifyWebApiService.getPlaylistTracks(simplePlaylist.id).forEach { playlistTrack: PlaylistTrack ->
playlistTrack.track?.asTrack?.let {
Expand All @@ -237,14 +239,14 @@ object MediaItemTree {
PLAYLIST_ID + simplePlaylist.id,
simplePlaylist.uri.uri
)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())
}
}
}

spotifyWebApiService.getSavedAlbums().forEach { savedAlbum: SavedAlbum ->
addBrowsableToTree(savedAlbum.album.name, savedAlbum.album.id, ALBUM_ID)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())

spotifyWebApiService.getAlbumTracks(savedAlbum.album.id).forEach { simpleTrack: SimpleTrack ->
CoroutineScope(Dispatchers.IO).launch {
Expand All @@ -254,7 +256,7 @@ object MediaItemTree {
it,
ALBUM_ID + savedAlbum.album.id, savedAlbum.album.uri.uri
)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())
}
}
}
Expand All @@ -263,11 +265,11 @@ object MediaItemTree {

spotifyWebApiService.getSavedShows().forEach { savedShow: SavedShow ->
addBrowsableToTree(savedShow.show.name, savedShow.show.id, SHOW_ID)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())

spotifyWebApiService.getShowEpisodes(savedShow.show.id).forEach { simpleEpisode: SimpleEpisode ->
addEpisodeNodeToTree(simpleEpisode, SHOW_ID + savedShow.show.id, savedShow.show.uri.uri)
emit(toBeSavedMediaItems.last())
send(toBeSavedMediaItems.last())
}
}
}
Expand Down

0 comments on commit 284d107

Please sign in to comment.