diff --git a/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt b/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt index c54870a80..15a61e2b9 100644 --- a/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt @@ -20,9 +20,7 @@ import org.oxycblt.auxio.music.MusicStore * @author OxygenCobalt */ class LoadingFragment : Fragment() { - private val loadingModel: LoadingViewModel by viewModels { - LoadingViewModel.Factory(requireActivity().application) - } + private val loadingModel: LoadingViewModel by viewModels() override fun onCreateView( inflater: LayoutInflater, @@ -71,7 +69,7 @@ class LoadingFragment : Fragment() { } if (loadingModel.response.value == null) { - loadingModel.load() + loadingModel.load(requireContext()) } return binding.root @@ -110,7 +108,7 @@ class LoadingFragment : Fragment() { if (granted) { // If granted, its now safe to load, which will clear the NO_PERMS response // we applied earlier. - loadingModel.load() + loadingModel.load(requireContext()) } } diff --git a/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt b/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt index 241388a66..dfa16db09 100644 --- a/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt +++ b/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt @@ -1,10 +1,9 @@ package org.oxycblt.auxio.loading -import android.app.Application +import android.content.Context import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch import org.oxycblt.auxio.music.MusicStore @@ -13,7 +12,7 @@ import org.oxycblt.auxio.music.MusicStore * ViewModel responsible for the loading UI and beginning the loading process overall. * @author OxygenCobalt */ -class LoadingViewModel(private val app: Application) : ViewModel() { +class LoadingViewModel : ViewModel() { private val mResponse = MutableLiveData(null) private val mDoGrant = MutableLiveData(false) @@ -27,9 +26,9 @@ class LoadingViewModel(private val app: Application) : ViewModel() { private val musicStore = MusicStore.getInstance() /** - * Begin the music loading process. The response is pushed to [response] + * Begin the music loading process. The response from MusicStore is pushed to [response] */ - fun load() { + fun load(context: Context) { // Dont start a new load if the last one hasnt finished if (isBusy) return @@ -37,7 +36,7 @@ class LoadingViewModel(private val app: Application) : ViewModel() { mResponse.value = null viewModelScope.launch { - mResponse.value = musicStore.load(app) + mResponse.value = musicStore.load(context) isBusy = false } } @@ -62,15 +61,4 @@ class LoadingViewModel(private val app: Application) : ViewModel() { fun notifyNoPermissions() { mResponse.value = MusicStore.Response.NO_PERMS } - - @Suppress("UNCHECKED_CAST") - class Factory(private val application: Application) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { - if (modelClass.isAssignableFrom(LoadingViewModel::class.java)) { - return LoadingViewModel(application) as T - } - - throw IllegalArgumentException("Unknown ViewModel class.") - } - } } diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt index e6d8e0720..a4eb16acd 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicStore.kt @@ -1,7 +1,7 @@ package org.oxycblt.auxio.music -import android.app.Application import android.content.ContentResolver +import android.content.Context import android.net.Uri import android.provider.OpenableColumns import kotlinx.coroutines.Dispatchers @@ -42,42 +42,45 @@ class MusicStore private constructor() { /** * Load/Sort the entire music library. Should always be ran on a coroutine. - * @param app [Application] required to load the music. */ - suspend fun load(app: Application): Response { + suspend fun load(context: Context): Response { return withContext(Dispatchers.IO) { - // TODO: Move this to an internal function - this@MusicStore.logD("Starting initial music load...") + loadMusicInternal(context) + } + } + /** + * Do the internal music loading process. + */ + private fun loadMusicInternal(context: Context): Response { + logD("Starting initial music load...") + + try { val start = System.currentTimeMillis() - try { - val loader = MusicLoader(app) - loader.load() + val loader = MusicLoader(context) + loader.load() - if (loader.songs.isEmpty()) { - return@withContext Response.NO_MUSIC - } - - mSongs = loader.songs - mAlbums = loader.albums - mArtists = loader.artists - mGenres = loader.genres - - this@MusicStore.logD( - "Music load completed successfully in ${System.currentTimeMillis() - start}ms." - ) - } catch (e: Exception) { - logE("Something went horribly wrong.") - logE(e.stackTraceToString()) - - return@withContext Response.FAILED + if (loader.songs.isEmpty()) { + return Response.NO_MUSIC } - loaded = true + mSongs = loader.songs + mAlbums = loader.albums + mArtists = loader.artists + mGenres = loader.genres - return@withContext Response.SUCCESS + logD("Music load completed successfully in ${System.currentTimeMillis() - start}ms.") + } catch (e: Exception) { + logE("Something went horribly wrong.") + logE(e.stackTraceToString()) + + return Response.FAILED } + + loaded = true + + return Response.SUCCESS } /** diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt index 721f5f2d1..9c35a026d 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt @@ -12,8 +12,8 @@ import org.oxycblt.auxio.R import org.oxycblt.auxio.ui.getPlural /** - * List of ID3 genres + Winamp extensions, each index corresponds to their int value. - * There are a lot more int-genre extensions as far as Im aware, but this works for most cases. + * A complete array of all the hardcoded genre values for ID3