Add Loading fallback states
Add fallback states for when there is no music & if the load fails, along with a retry button if that happens.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onRetry(retry: Boolean) {
|
||||
if (retry) {
|
||||
binding.loadingBar.visibility = View.VISIBLE
|
||||
binding.statusText.visibility = View.GONE
|
||||
binding.resetButton.visibility = View.GONE
|
||||
|
||||
loadingModel.doneWithRetry()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<MusicLoadResponse>()
|
||||
val musicRepoResponse: LiveData<MusicLoadResponse> get() = mMusicRepoResponse
|
||||
|
||||
private val mDoRetry = MutableLiveData<Boolean>()
|
||||
val doRetry: LiveData<Boolean> 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.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,6 @@ class MusicRepository {
|
||||
)
|
||||
|
||||
return songList
|
||||
|
||||
} catch (error: Exception) {
|
||||
// TODO: Add better error handling
|
||||
|
||||
|
||||
Reference in New Issue
Block a user