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 b9298af1d..823bc8363 100644 --- a/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/loading/LoadingFragment.kt @@ -17,8 +17,11 @@ import org.oxycblt.auxio.music.MusicLoadResponse class LoadingFragment : Fragment() { private val loadingModel: LoadingViewModel by lazy { - ViewModelProvider(this, LoadingViewModel.Factory( - requireActivity().application) + ViewModelProvider( + this, + LoadingViewModel.Factory( + requireActivity().application + ) ).get(LoadingViewModel::class.java) } @@ -34,21 +37,62 @@ class LoadingFragment : Fragment() { ) binding.lifecycleOwner = this + binding.loadingModel = loadingModel - loadingModel.musicRepoResponse.observe(viewLifecycleOwner, Observer { response -> - onMusicLoadResponse(response) - }) + loadingModel.musicRepoResponse.observe( + viewLifecycleOwner, + Observer { response -> + onMusicLoadResponse(response) + } + ) + + loadingModel.doRetry.observe( + viewLifecycleOwner, + Observer { retry -> + onRetry(retry) + } + ) Log.d(this::class.simpleName, "Fragment created.") return binding.root } - private fun onMusicLoadResponse(response: MusicLoadResponse) { - if (response == MusicLoadResponse.DONE) { - this.findNavController().navigate( - LoadingFragmentDirections.actionToLibrary() - ) + private fun onMusicLoadResponse(repoResponse: MusicLoadResponse?) { + + // Don't run this if the value is null, Which is what the value changes to after + // this is run. + repoResponse?.let { response -> + if (response == MusicLoadResponse.DONE) { + this.findNavController().navigate( + LoadingFragmentDirections.actionToLibrary() + ) + } else { + // If the response wasn't a success, then show the specific error message + // depending on which error response was given, along with a retry button + + binding.loadingBar.visibility = View.GONE + binding.statusText.visibility = View.VISIBLE + binding.resetButton.visibility = View.VISIBLE + + if (response == MusicLoadResponse.NO_MUSIC) { + binding.statusText.text = getString(R.string.error_no_music) + } else { + binding.statusText.text = getString(R.string.error_music_load_failed) + } + } + + loadingModel.doneWithResponse() } } -} \ No newline at end of file + + private fun onRetry(retry: Boolean) { + if (retry) { + binding.loadingBar.visibility = View.VISIBLE + binding.statusText.visibility = View.GONE + binding.resetButton.visibility = View.GONE + + loadingModel.doneWithRetry() + } + } +} 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 daab79b58..aba0da2f7 100644 --- a/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt +++ b/app/src/main/java/org/oxycblt/auxio/loading/LoadingViewModel.kt @@ -6,12 +6,16 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.oxycblt.auxio.music.MusicLoadResponse import org.oxycblt.auxio.music.MusicRepository class LoadingViewModel(private val app: Application) : ViewModel() { - + private val loadingJob = Job() private val ioScope = CoroutineScope( Dispatchers.IO @@ -20,6 +24,9 @@ class LoadingViewModel(private val app: Application) : ViewModel() { private val mMusicRepoResponse = MutableLiveData() val musicRepoResponse: LiveData get() = mMusicRepoResponse + private val mDoRetry = MutableLiveData() + val doRetry: LiveData get() = mDoRetry + init { startMusicRepo() @@ -40,6 +47,20 @@ class LoadingViewModel(private val app: Application) : ViewModel() { } } + fun doneWithResponse() { + mMusicRepoResponse.value = null + } + + fun retry() { + mDoRetry.value = true + + startMusicRepo() + } + + fun doneWithRetry() { + mDoRetry.value = false + } + override fun onCleared() { super.onCleared() @@ -53,7 +74,8 @@ class LoadingViewModel(private val app: Application) : ViewModel() { if (modelClass.isAssignableFrom(LoadingViewModel::class.java)) { return LoadingViewModel(application) as T } - throw IllegalArgumentException("Unknown ViewModel class") + + throw IllegalArgumentException("Unknown ViewModel class.") } } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt index e1210bcc6..f9525e756 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt @@ -149,7 +149,6 @@ class MusicRepository { ) return songList - } catch (error: Exception) { // TODO: Add better error handling diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6c34f6f2b..7501e97c8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -19,5 +19,4 @@ tools:ignore="FragmentTagUsage" /> - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_library.xml b/app/src/main/res/layout/fragment_library.xml index b8e7b16da..500aed1ef 100644 --- a/app/src/main/res/layout/fragment_library.xml +++ b/app/src/main/res/layout/fragment_library.xml @@ -1,6 +1,5 @@ - diff --git a/app/src/main/res/layout/fragment_loading.xml b/app/src/main/res/layout/fragment_loading.xml index 6d6e6ecda..3849e01de 100644 --- a/app/src/main/res/layout/fragment_loading.xml +++ b/app/src/main/res/layout/fragment_loading.xml @@ -3,6 +3,13 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> + + + + + @@ -14,22 +21,37 @@ android:indeterminateTint="?attr/colorAccent" android:indeterminateTintMode="src_in" android:paddingBottom="@dimen/padding_small" - app:layout_constraintBottom_toTopOf="@+id/text_indexing_library" + app:layout_constraintBottom_toTopOf="@+id/status_text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintVertical_chainStyle="packed"/> + app:layout_constraintVertical_chainStyle="packed" /> +