Skip to content

Commit 2a042b4

Browse files
committed
Initialize root as soon as possible
1 parent 7c80a6a commit 2a042b4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/library/MediaItemProvider.kt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ internal class MediaItemProvider(private val context: Context) {
7171

7272
private val audioHelper = context.audioHelper
7373

74+
init {
75+
buildRoot()
76+
}
77+
7478
fun whenReady(performAction: (Boolean) -> Unit): Boolean {
7579
return when (state) {
7680
STATE_CREATED, STATE_INITIALIZING -> {
@@ -155,21 +159,15 @@ internal class MediaItemProvider(private val context: Context) {
155159
state = STATE_INITIALIZING
156160

157161
ensureBackgroundThread {
158-
val root = buildMediaItem(
159-
title = context.getString(com.simplemobiletools.commons.R.string.root),
160-
mediaId = SMP_ROOT_ID,
161-
mediaType = MediaMetadata.MEDIA_TYPE_FOLDER_MIXED
162-
)
163-
val rootChildren = RootCategories.buildRootChildren(context)
164-
addNodeAndChildren(item = root, children = rootChildren)
162+
buildRoot()
165163

166164
try {
167-
reloadPlaylists()
168-
reloadFolders()
169-
reloadArtists()
170-
reloadAlbums()
171-
reloadTracks()
172-
reloadGenres()
165+
buildPlaylists()
166+
buildFolders()
167+
buildArtists()
168+
buildAlbums()
169+
buildTracks()
170+
buildGenres()
173171
} catch (e: Exception) {
174172
state = STATE_ERROR
175173
}
@@ -178,38 +176,48 @@ internal class MediaItemProvider(private val context: Context) {
178176
}
179177
}
180178

181-
private fun reloadPlaylists() = with(audioHelper) {
179+
private fun buildRoot() {
180+
val root = buildMediaItem(
181+
title = context.getString(com.simplemobiletools.commons.R.string.root),
182+
mediaId = SMP_ROOT_ID,
183+
mediaType = MediaMetadata.MEDIA_TYPE_FOLDER_MIXED
184+
)
185+
val rootChildren = RootCategories.buildRootChildren(context)
186+
addNodeAndChildren(item = root, children = rootChildren)
187+
}
188+
189+
private fun buildPlaylists() = with(audioHelper) {
182190
getAllPlaylists().forEach { playlist ->
183191
addNodeAndChildren(SMP_PLAYLISTS_ROOT_ID, playlist.toMediaItem(), getPlaylistTracks(playlist.id).map { it.toMediaItem() })
184192
}
185193
}
186194

187-
private fun reloadFolders() = with(audioHelper) {
195+
private fun buildFolders() = with(audioHelper) {
188196
getAllFolders().forEach { folder ->
189197
addNodeAndChildren(SMP_FOLDERS_ROOT_ID, folder.toMediaItem(), getFolderTracks(folder.title).map { it.toMediaItem() })
190198
}
191199
}
192200

193-
private fun reloadArtists() = with(audioHelper) {
201+
private fun buildArtists() = with(audioHelper) {
194202
getAllArtists().forEach { artist ->
195203
addNodeAndChildren(SMP_ARTISTS_ROOT_ID, artist.toMediaItem(), getArtistAlbums(artist.id).map { it.toMediaItem() })
196204
}
197205
}
198206

199-
private fun reloadAlbums() = with(audioHelper) {
207+
private fun buildAlbums() = with(audioHelper) {
200208
getAllAlbums().forEach { album ->
201209
addNodeAndChildren(SMP_ALBUMS_ROOT_ID, album.toMediaItem(), getAlbumTracks(album.id).map { it.toMediaItem() })
202210
}
203211
}
204212

205-
private fun reloadTracks() = with(audioHelper) {
213+
private fun buildTracks() = with(audioHelper) {
206214
getAllTracks().forEach { track ->
207215
addNodeAndChildren(SMP_TRACKS_ROOT_ID, track.toMediaItem())
208216
titleMap[track.title.lowercase()] = MediaItemNode(track.toMediaItem())
209217
}
210218
}
211219

212-
private fun reloadGenres() = with(audioHelper) {
220+
private fun buildGenres() = with(audioHelper) {
213221
getAllGenres().forEach { genre ->
214222
addNodeAndChildren(SMP_GENRES_ROOT_ID, genre.toMediaItem(), getGenreTracks(genre.id).map { it.toMediaItem() })
215223
}

0 commit comments

Comments
 (0)