Fix file intent crash
Keep tweaking this buggy system to fix another bug I caused where the app would crash if selecting a song while the app was inactive.
This commit is contained in:
parent
89174b8011
commit
044f74b45b
8 changed files with 23 additions and 16 deletions
|
@ -19,6 +19,7 @@ import org.oxycblt.auxio.detail.DetailViewModel
|
|||
import org.oxycblt.auxio.music.MusicStore
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.playback.handleFileIntent
|
||||
import org.oxycblt.auxio.playback.shouldHandleFileIntent
|
||||
import org.oxycblt.auxio.ui.Accent
|
||||
import org.oxycblt.auxio.ui.fixAnimInfoLeak
|
||||
|
@ -122,9 +123,7 @@ class MainFragment : Fragment() {
|
|||
// File intents chain-navigate towards PlaybackFragment
|
||||
// Not for any express purpose, I just prefer it this way.
|
||||
if (shouldHandleFileIntent()) {
|
||||
findNavController().navigate(
|
||||
MainFragmentDirections.actionGoToPlayback()
|
||||
)
|
||||
handleFileIntent(playbackModel)
|
||||
} else {
|
||||
// If there is no file intent restore playback as usual
|
||||
playbackModel.restorePlaybackIfNeeded(requireContext())
|
||||
|
|
|
@ -47,7 +47,7 @@ class LoadingFragment : Fragment() {
|
|||
|
||||
loadingModel.response.observe(viewLifecycleOwner) { response ->
|
||||
when (response) {
|
||||
// Success should lead to Auxio navigating away from the fragment
|
||||
// Success should lead to navigation to the main fragment
|
||||
MusicStore.Response.SUCCESS -> findNavController().navigate(
|
||||
LoadingFragmentDirections.actionToMain()
|
||||
)
|
||||
|
@ -60,7 +60,7 @@ class LoadingFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
|
||||
if (noPermissions()) {
|
||||
if (hasNoPermissions()) {
|
||||
// MusicStore.Response.NO_PERMS isnt actually returned by MusicStore, its just
|
||||
// a way to keep the current permission state across device changes
|
||||
loadingModel.notifyNoPermissions()
|
||||
|
@ -85,7 +85,7 @@ class LoadingFragment : Fragment() {
|
|||
|
||||
// --- PERMISSIONS ---
|
||||
|
||||
private fun noPermissions(): Boolean {
|
||||
private fun hasNoPermissions(): Boolean {
|
||||
val needRationale = shouldShowRequestPermissionRationale(
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
)
|
||||
|
|
|
@ -148,8 +148,7 @@ data class Genre(
|
|||
}
|
||||
}
|
||||
|
||||
val totalDuration: String get() =
|
||||
songs.sumOf { it.seconds }.toDuration()
|
||||
val totalDuration: String get() = songs.sumOf { it.seconds }.toDuration()
|
||||
|
||||
fun linkSong(song: Song) {
|
||||
mSongs.add(song)
|
||||
|
|
|
@ -194,12 +194,7 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
|
|||
}
|
||||
|
||||
if (shouldHandleFileIntent()) {
|
||||
val intent = requireActivity().intent
|
||||
|
||||
// Ensure that this wont fire again by putting a boolean extra
|
||||
intent.putExtra(PlaybackUtils.KEY_INTENT_FIRED, true)
|
||||
|
||||
playbackModel.playWithIntent(intent, requireContext())
|
||||
handleFileIntent(playbackModel)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,3 +17,15 @@ fun Fragment.shouldHandleFileIntent(): Boolean {
|
|||
intent.action == Intent.ACTION_VIEW &&
|
||||
!intent.getBooleanExtra(PlaybackUtils.KEY_INTENT_FIRED, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually use the intent and push it to [playbackModel]
|
||||
*/
|
||||
fun Fragment.handleFileIntent(playbackModel: PlaybackViewModel) {
|
||||
val intent = requireActivity().intent
|
||||
|
||||
// Ensure that this wont fire again by putting a boolean extra
|
||||
intent.putExtra(PlaybackUtils.KEY_INTENT_FIRED, true)
|
||||
|
||||
playbackModel.playWithIntent(intent, requireContext())
|
||||
}
|
||||
|
|
|
@ -329,7 +329,7 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
|
|||
* @param context [Context] required.
|
||||
*/
|
||||
fun restorePlaybackIfNeeded(context: Context) {
|
||||
if (!playbackManager.isRestored) {
|
||||
if (!playbackManager.isRestored && playbackManager.song == null) {
|
||||
viewModelScope.launch {
|
||||
playbackManager.getStateFromDatabase(context)
|
||||
}
|
||||
|
|
|
@ -102,6 +102,8 @@ class QueueFragment : Fragment() {
|
|||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
// QueueFragment shouldn't be handling file intents, as the effects it has on the recycler
|
||||
// are really weird.
|
||||
if (shouldHandleFileIntent()) {
|
||||
findNavController().navigateUp()
|
||||
}
|
||||
|
|
|
@ -214,7 +214,7 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean {
|
|||
return (!canMove || width < height)
|
||||
}
|
||||
|
||||
// --- FRAGMENT NONSENSE ---
|
||||
// --- HACKY NIGHTMARES ---
|
||||
|
||||
/**
|
||||
* Use reflection to fix a memory leak in the [Fragment] source code where the focused view will
|
||||
|
|
Loading…
Reference in a new issue