Skip to content

Commit

Permalink
Fix file intent crash
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OxygenCobalt committed Feb 21, 2021
1 parent 89174b8 commit 044f74b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 16 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/org/oxycblt/auxio/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand All @@ -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()
Expand All @@ -85,7 +85,7 @@ class LoadingFragment : Fragment() {

// --- PERMISSIONS ---

private fun noPermissions(): Boolean {
private fun hasNoPermissions(): Boolean {
val needRationale = shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE
)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/org/oxycblt/auxio/music/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/org/oxycblt/auxio/playback/PlaybackUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 044f74b

Please sign in to comment.