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/ /library/
- Exit functionality - Exit functionality
- ? Remove gap from where I removed the overflow menu ?
- ? Add icons to overflow menu items ? - ? Add icons to overflow menu items ?
- ? Implement filtering for search [Will resolve gap issue] ? - ? 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 into ViewPager ?
- ! Move Adapter functionality to ListAdapter [RecyclerView scrolls to middle/bottom when data is re-sorted] ! - ! Move Adapter functionality to ListAdapter [RecyclerView scrolls to middle/bottom when data is re-sorted] !
/bugs/ /bugs/
- Fix issue where fast navigations will cause the app to not display anything - Fix issue where fast navigations will cause the app to not display anything
/other/
- Standardize fragment init
To be added: To be added:
/prefs/ /prefs/
/playback/ /playback/

View file

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

View file

@ -7,11 +7,14 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import org.oxycblt.auxio.databinding.FragmentMainBinding import org.oxycblt.auxio.databinding.FragmentMainBinding
import org.oxycblt.auxio.library.LibraryFragment import org.oxycblt.auxio.library.LibraryFragment
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.songs.SongsFragment import org.oxycblt.auxio.songs.SongsFragment
import org.oxycblt.auxio.theme.accent import org.oxycblt.auxio.theme.accent
import org.oxycblt.auxio.theme.getInactiveAlpha import org.oxycblt.auxio.theme.getInactiveAlpha
@ -19,6 +22,10 @@ import org.oxycblt.auxio.theme.getTransparentAccent
import org.oxycblt.auxio.theme.toColor import org.oxycblt.auxio.theme.toColor
class MainFragment : Fragment() { class MainFragment : Fragment() {
private val musicModel: MusicViewModel by activityViewModels {
MusicViewModel.Factory(requireActivity().application)
}
private val shownFragments = listOf(0, 1) private val shownFragments = listOf(0, 1)
private val tabIcons = listOf( private val tabIcons = listOf(
@ -33,6 +40,14 @@ class MainFragment : Fragment() {
): View? { ): View? {
val binding = FragmentMainBinding.inflate(inflater) 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 colorActive = accent.first.toColor(requireContext())
val colorInactive = getTransparentAccent( val colorInactive = getTransparentAccent(
requireContext(), requireContext(),

View file

@ -31,7 +31,10 @@ import org.oxycblt.auxio.theme.resolveAttr
class LibraryFragment : Fragment(), SearchView.OnQueryTextListener { 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() private val libraryModel: LibraryViewModel by activityViewModels()
override fun onCreateView( override fun onCreateView(

View file

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

View file

@ -46,6 +46,11 @@
app:popEnterAnim="@anim/fragment_fade_enter" app:popEnterAnim="@anim/fragment_fade_enter"
app:popExitAnim="@anim/fragment_fade_exit" app:popExitAnim="@anim/fragment_fade_exit"
app:destination="@id/genreDetailFragment" /> 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>
<fragment <fragment
android:id="@+id/artist_detail_fragment" android:id="@+id/artist_detail_fragment"