Fix bug with app restart

Fix bug where the app would be unaware if MusicViewModel was cleared due to it being suspended in the background.
This commit is contained in:
OxygenCobalt 2020-10-05 09:26:22 -06:00
parent 7928347f9b
commit 21ff93d5f0
6 changed files with 30 additions and 12 deletions

View file

@ -24,18 +24,13 @@ TODOs surrounded with !s are things I tried to do, but failed for reasons includ
/library/
- Exit functionality
- ? Remove gap from where I removed the overflow menu ?
- ? Add icons to overflow menu items ?
- ? Implement filtering for search [Will resolve gap issue] ?
- ? Move into ViewPager ?
- ? Add Nested Nav to Library ViewPager fragment [Hold fire on this until everything else is added, there could be sneaky bugs later on if you add it now] ?
- ! Move Adapter functionality to ListAdapter [RecyclerView scrolls to middle/bottom when data is re-sorted] !
/bugs/
- Fix issue where fast navigations will cause the app to not display anything
/other/
- Standardize fragment init
To be added:
/prefs/
/playback/

View file

@ -5,7 +5,6 @@ import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import org.oxycblt.auxio.theme.accent
class MainActivity : AppCompatActivity() {
@ -19,7 +18,6 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ActivityCompat.postponeEnterTransition(this)
setContentView(R.layout.activity_main)
}
}

View file

@ -7,11 +7,14 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import org.oxycblt.auxio.databinding.FragmentMainBinding
import org.oxycblt.auxio.library.LibraryFragment
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.songs.SongsFragment
import org.oxycblt.auxio.theme.accent
import org.oxycblt.auxio.theme.getInactiveAlpha
@ -19,6 +22,10 @@ import org.oxycblt.auxio.theme.getTransparentAccent
import org.oxycblt.auxio.theme.toColor
class MainFragment : Fragment() {
private val musicModel: MusicViewModel by activityViewModels {
MusicViewModel.Factory(requireActivity().application)
}
private val shownFragments = listOf(0, 1)
private val tabIcons = listOf(
@ -33,6 +40,14 @@ class MainFragment : Fragment() {
): View? {
val binding = FragmentMainBinding.inflate(inflater)
// If musicModel was cleared while the app was closed [Likely due to Auxio being suspended
// in the background], then navigate back to loading to reload the music.
if (musicModel.response.value == null) {
findNavController().navigate(MainFragmentDirections.actionReturnToLoading())
return null
}
val colorActive = accent.first.toColor(requireContext())
val colorInactive = getTransparentAccent(
requireContext(),

View file

@ -31,7 +31,10 @@ import org.oxycblt.auxio.theme.resolveAttr
class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
private val musicModel: MusicViewModel by activityViewModels()
private val musicModel: MusicViewModel by activityViewModels {
MusicViewModel.Factory(requireActivity().application)
}
private val libraryModel: LibraryViewModel by activityViewModels()
override fun onCreateView(

View file

@ -10,6 +10,7 @@ import android.view.MenuItem
import android.widget.ImageButton
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.recyclerview.widget.DividerItemDecoration
@ -43,7 +44,7 @@ val accent = ACCENTS[5]
// Get the transparent variant of a color int
@ColorInt
fun getTransparentAccent(context: Context, color: Int, alpha: Int): Int {
fun getTransparentAccent(context: Context, @ColorRes color: Int, alpha: Int): Int {
return ColorUtils.setAlphaComponent(
ContextCompat.getColor(context, color),
alpha
@ -52,7 +53,7 @@ fun getTransparentAccent(context: Context, color: Int, alpha: Int): Int {
// Get the inactive transparency of an accent
@ColorInt
fun getInactiveAlpha(color: Int): Int {
fun getInactiveAlpha(@ColorRes color: Int): Int {
return if (color == R.color.yellow) 100 else 150
}
@ -86,13 +87,14 @@ fun resolveAttr(context: Context, @AttrRes attr: Int): Int {
}
// Apply a color to a Menu Item
fun MenuItem.applyColor(color: Int) {
fun MenuItem.applyColor(@ColorRes color: Int) {
SpannableString(title).apply {
setSpan(ForegroundColorSpan(color), 0, length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
title = this
}
}
// Disable an ImageButton
fun ImageButton.disable(context: Context) {
imageTintList = ColorStateList.valueOf(
R.color.inactive_color.toColor(context)

View file

@ -46,6 +46,11 @@
app:popEnterAnim="@anim/fragment_fade_enter"
app:popExitAnim="@anim/fragment_fade_exit"
app:destination="@id/genreDetailFragment" />
<action
android:id="@+id/action_return_to_loading"
app:destination="@id/loading_fragment"
app:popUpTo="@id/main_fragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/artist_detail_fragment"