Minor Logging/Structure changes
Switch logging to templates instead of concat, move MusicLoader/MusicSorter to their own package, rename GenreCompat to MusicUtils
This commit is contained in:
parent
ef9d097dc3
commit
f07542fd3a
6 changed files with 44 additions and 36 deletions
|
@ -12,7 +12,7 @@ import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentLoadingBinding
|
import org.oxycblt.auxio.databinding.FragmentLoadingBinding
|
||||||
import org.oxycblt.auxio.music.MusicLoaderResponse
|
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||||
|
|
||||||
class LoadingFragment : Fragment() {
|
class LoadingFragment : Fragment() {
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.oxycblt.auxio.music.MusicLoaderResponse
|
|
||||||
import org.oxycblt.auxio.music.MusicRepository
|
import org.oxycblt.auxio.music.MusicRepository
|
||||||
|
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||||
|
|
||||||
class LoadingViewModel(private val app: Application) : ViewModel() {
|
class LoadingViewModel(private val app: Application) : ViewModel() {
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,9 @@ import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.oxycblt.auxio.music.processing.MusicLoader
|
||||||
|
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||||
|
import org.oxycblt.auxio.music.processing.MusicSorter
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
import org.oxycblt.auxio.music.models.Artist
|
import org.oxycblt.auxio.music.models.Artist
|
||||||
import org.oxycblt.auxio.music.models.Song
|
import org.oxycblt.auxio.music.models.Song
|
||||||
|
@ -22,13 +25,15 @@ class MusicRepository {
|
||||||
val songs: LiveData<List<Song>> get() = mSongs
|
val songs: LiveData<List<Song>> get() = mSongs
|
||||||
|
|
||||||
suspend fun init(app: Application): MusicLoaderResponse {
|
suspend fun init(app: Application): MusicLoaderResponse {
|
||||||
Log.i(this::class.simpleName, "Starting initial music load")
|
Log.i(this::class.simpleName, "Starting initial music load...")
|
||||||
|
|
||||||
|
val start = System.currentTimeMillis()
|
||||||
|
|
||||||
val loader = MusicLoader(app)
|
val loader = MusicLoader(app)
|
||||||
|
|
||||||
if (loader.response == MusicLoaderResponse.DONE) {
|
if (loader.response == MusicLoaderResponse.DONE) {
|
||||||
// If the loading succeeds, then process the songs into lists of
|
// If the loading succeeds, then process the songs and set them
|
||||||
// songs, albums, and artists on the main thread.
|
// as the values on the main thread.
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
val sorter = MusicSorter(
|
val sorter = MusicSorter(
|
||||||
loader.artists,
|
loader.artists,
|
||||||
|
@ -40,7 +45,12 @@ class MusicRepository {
|
||||||
mAlbums.value = sorter.albums
|
mAlbums.value = sorter.albums
|
||||||
mArtists.value = sorter.artists
|
mArtists.value = sorter.artists
|
||||||
|
|
||||||
Log.i(this::class.simpleName, "Finished initial music load.")
|
val elapsed = System.currentTimeMillis() - start
|
||||||
|
|
||||||
|
Log.i(
|
||||||
|
this::class.simpleName,
|
||||||
|
"Music load completed successfully in ${elapsed}ms."
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package org.oxycblt.auxio.music
|
package org.oxycblt.auxio.music
|
||||||
|
|
||||||
// Compatibility layer to convert old int-based genres to new genres
|
import android.content.ContentUris
|
||||||
val ID3_GENRES = arrayOf<String>(
|
import android.net.Uri
|
||||||
|
import android.provider.MediaStore
|
||||||
|
|
||||||
|
private val ID3_GENRES = arrayOf<String>(
|
||||||
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz",
|
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz",
|
||||||
"Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
|
"Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
|
||||||
"Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno",
|
"Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno",
|
||||||
|
@ -28,6 +31,7 @@ val ID3_GENRES = arrayOf<String>(
|
||||||
|
|
||||||
const val PAREN_FILTER = "()"
|
const val PAREN_FILTER = "()"
|
||||||
|
|
||||||
|
// Convert legacy ID3 genres to a named genre
|
||||||
fun intToNamedGenre(genre: String): String {
|
fun intToNamedGenre(genre: String): String {
|
||||||
// Strip the genres of any parentheses, and convert it to an int
|
// Strip the genres of any parentheses, and convert it to an int
|
||||||
val intGenre = genre.filterNot {
|
val intGenre = genre.filterNot {
|
||||||
|
@ -36,3 +40,10 @@ fun intToNamedGenre(genre: String): String {
|
||||||
|
|
||||||
return ID3_GENRES.getOrNull(intGenre) ?: ""
|
return ID3_GENRES.getOrNull(intGenre) ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Long.toURI(): Uri {
|
||||||
|
return ContentUris.withAppendedId(
|
||||||
|
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||||
|
this
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package org.oxycblt.auxio.music
|
package org.oxycblt.auxio.music.processing
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
|
@ -8,6 +8,7 @@ import android.provider.MediaStore.Audio.Artists
|
||||||
import android.provider.MediaStore.Audio.Genres
|
import android.provider.MediaStore.Audio.Genres
|
||||||
import android.provider.MediaStore.Audio.Media
|
import android.provider.MediaStore.Audio.Media
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import org.oxycblt.auxio.music.intToNamedGenre
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
import org.oxycblt.auxio.music.models.Artist
|
import org.oxycblt.auxio.music.models.Artist
|
||||||
import org.oxycblt.auxio.music.models.Genre
|
import org.oxycblt.auxio.music.models.Genre
|
||||||
|
@ -94,16 +95,9 @@ class MusicLoader(private val app: Application) {
|
||||||
cursor.close()
|
cursor.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove dupes
|
|
||||||
genres = genres.distinctBy {
|
|
||||||
it.name
|
|
||||||
}.toMutableList()
|
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Genre search finished with " +
|
"Genre search finished with ${genres.size} genres found."
|
||||||
genres.size.toString() +
|
|
||||||
" genres found."
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,9 +153,7 @@ class MusicLoader(private val app: Application) {
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Artist search finished with " +
|
"Artist search finished with ${artists.size} artists found."
|
||||||
artists.size.toString() +
|
|
||||||
" artists found."
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,13 +163,12 @@ class MusicLoader(private val app: Application) {
|
||||||
albumCursor = resolver.query(
|
albumCursor = resolver.query(
|
||||||
Albums.EXTERNAL_CONTENT_URI,
|
Albums.EXTERNAL_CONTENT_URI,
|
||||||
arrayOf(
|
arrayOf(
|
||||||
Albums._ID,
|
Albums._ID, // 0
|
||||||
Albums.ALBUM,
|
Albums.ALBUM, // 1
|
||||||
Albums.ARTIST,
|
Albums.ARTIST, // 2
|
||||||
|
|
||||||
// FIXME: May be an issue for albums whose songs released in multiple years
|
Albums.FIRST_YEAR, // 3
|
||||||
Albums.FIRST_YEAR,
|
Albums.NUMBER_OF_SONGS // 4
|
||||||
Albums.NUMBER_OF_SONGS
|
|
||||||
),
|
),
|
||||||
null, null,
|
null, null,
|
||||||
Albums.DEFAULT_SORT_ORDER
|
Albums.DEFAULT_SORT_ORDER
|
||||||
|
@ -215,9 +206,7 @@ class MusicLoader(private val app: Application) {
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Album search finished with " +
|
"Album search finished with ${albums.size} albums found"
|
||||||
albums.size.toString() +
|
|
||||||
" albums found."
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,9 +260,7 @@ class MusicLoader(private val app: Application) {
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Song search finished with " +
|
"Song search finished with ${songs.size} found"
|
||||||
songs.size.toString() +
|
|
||||||
" songs found."
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package org.oxycblt.auxio.music
|
package org.oxycblt.auxio.music.processing
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
|
@ -24,7 +24,7 @@ class MusicSorter(
|
||||||
// Find all songs that match the current album title
|
// Find all songs that match the current album title
|
||||||
val albumSongs = songs.filter { it.albumName == album.title }
|
val albumSongs = songs.filter { it.albumName == album.title }
|
||||||
|
|
||||||
// And then add them to the album
|
// Then add them to the album, along with refreshing the cover
|
||||||
album.songs.addAll(albumSongs)
|
album.songs.addAll(albumSongs)
|
||||||
|
|
||||||
unknownSongs.removeAll(albumSongs)
|
unknownSongs.removeAll(albumSongs)
|
||||||
|
@ -47,7 +47,7 @@ class MusicSorter(
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Placed " + unknownSongs.size.toString() + " songs into an unknown album"
|
"${unknownSongs.size} songs were placed into an unknown album."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ class MusicSorter(
|
||||||
|
|
||||||
Log.d(
|
Log.d(
|
||||||
this::class.simpleName,
|
this::class.simpleName,
|
||||||
"Placed " + unknownAlbums.size.toString() + " albums into an unknown artist"
|
"${unknownAlbums.size} albums were placed into an unknown artist."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue