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:
OxygenCobalt 2020-08-21 14:53:00 -06:00
parent ef9d097dc3
commit f07542fd3a
6 changed files with 44 additions and 36 deletions

View file

@ -12,7 +12,7 @@ import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentLoadingBinding
import org.oxycblt.auxio.music.MusicLoaderResponse
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
class LoadingFragment : Fragment() {

View file

@ -11,8 +11,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.oxycblt.auxio.music.MusicLoaderResponse
import org.oxycblt.auxio.music.MusicRepository
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
class LoadingViewModel(private val app: Application) : ViewModel() {

View file

@ -6,6 +6,9 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.Dispatchers
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.Artist
import org.oxycblt.auxio.music.models.Song
@ -22,13 +25,15 @@ class MusicRepository {
val songs: LiveData<List<Song>> get() = mSongs
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)
if (loader.response == MusicLoaderResponse.DONE) {
// If the loading succeeds, then process the songs into lists of
// songs, albums, and artists on the main thread.
// If the loading succeeds, then process the songs and set them
// as the values on the main thread.
withContext(Dispatchers.Main) {
val sorter = MusicSorter(
loader.artists,
@ -40,7 +45,12 @@ class MusicRepository {
mAlbums.value = sorter.albums
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."
)
}
}

View file

@ -1,7 +1,10 @@
package org.oxycblt.auxio.music
// Compatibility layer to convert old int-based genres to new genres
val ID3_GENRES = arrayOf<String>(
import android.content.ContentUris
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",
"Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno",
"Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno",
@ -28,6 +31,7 @@ val ID3_GENRES = arrayOf<String>(
const val PAREN_FILTER = "()"
// Convert legacy ID3 genres to a named genre
fun intToNamedGenre(genre: String): String {
// Strip the genres of any parentheses, and convert it to an int
val intGenre = genre.filterNot {
@ -36,3 +40,10 @@ fun intToNamedGenre(genre: String): String {
return ID3_GENRES.getOrNull(intGenre) ?: ""
}
fun Long.toURI(): Uri {
return ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
this
)
}

View file

@ -1,4 +1,4 @@
package org.oxycblt.auxio.music
package org.oxycblt.auxio.music.processing
import android.app.Application
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.Media
import android.util.Log
import org.oxycblt.auxio.music.intToNamedGenre
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.music.models.Genre
@ -94,16 +95,9 @@ class MusicLoader(private val app: Application) {
cursor.close()
}
// Remove dupes
genres = genres.distinctBy {
it.name
}.toMutableList()
Log.d(
this::class.simpleName,
"Genre search finished with " +
genres.size.toString() +
" genres found."
"Genre search finished with ${genres.size} genres found."
)
}
@ -159,9 +153,7 @@ class MusicLoader(private val app: Application) {
Log.d(
this::class.simpleName,
"Artist search finished with " +
artists.size.toString() +
" artists found."
"Artist search finished with ${artists.size} artists found."
)
}
@ -171,13 +163,12 @@ class MusicLoader(private val app: Application) {
albumCursor = resolver.query(
Albums.EXTERNAL_CONTENT_URI,
arrayOf(
Albums._ID,
Albums.ALBUM,
Albums.ARTIST,
Albums._ID, // 0
Albums.ALBUM, // 1
Albums.ARTIST, // 2
// FIXME: May be an issue for albums whose songs released in multiple years
Albums.FIRST_YEAR,
Albums.NUMBER_OF_SONGS
Albums.FIRST_YEAR, // 3
Albums.NUMBER_OF_SONGS // 4
),
null, null,
Albums.DEFAULT_SORT_ORDER
@ -215,9 +206,7 @@ class MusicLoader(private val app: Application) {
Log.d(
this::class.simpleName,
"Album search finished with " +
albums.size.toString() +
" albums found."
"Album search finished with ${albums.size} albums found"
)
}
@ -271,9 +260,7 @@ class MusicLoader(private val app: Application) {
Log.d(
this::class.simpleName,
"Song search finished with " +
songs.size.toString() +
" songs found."
"Song search finished with ${songs.size} found"
)
}
}

View file

@ -1,4 +1,4 @@
package org.oxycblt.auxio.music
package org.oxycblt.auxio.music.processing
import android.util.Log
import org.oxycblt.auxio.music.models.Album
@ -24,7 +24,7 @@ class MusicSorter(
// Find all songs that match the current 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)
unknownSongs.removeAll(albumSongs)
@ -47,7 +47,7 @@ class MusicSorter(
Log.d(
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(
this::class.simpleName,
"Placed " + unknownAlbums.size.toString() + " albums into an unknown artist"
"${unknownAlbums.size} albums were placed into an unknown artist."
)
}
}