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:
OxygenCobalt 2021-02-20 21:26:37 -07:00
parent 89174b8011
commit 044f74b45b
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
8 changed files with 23 additions and 16 deletions

View file

@ -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())

View file

@ -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
)

View file

@ -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)

View file

@ -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)
}
}

View file

@ -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())
}

View file

@ -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)
}

View file

@ -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()
}

View file

@ -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