Skip to content

Commit

Permalink
fix: android back button doesn't work due to dependency upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 25, 2024
1 parent bfa55d3 commit 38fb470
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/StartupTheme"
tools:targetApi="n">
android:enableOnBackInvokedCallback="true"
tools:targetApi="tiramisu">

<activity
android:name=".ui.activities.WelcomeActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class MainActivity : BaseActivity() {

setupSubscriptionsBadge()

// new way of handling back presses
onBackPressedDispatcher.addCallback {
if (playerViewModel.isFullscreen.value == true) {
runOnPlayerFragment { unsetFullscreen() }
val fullscreenUnsetSuccess = runOnPlayerFragment { unsetFullscreen() }
if (fullscreenUnsetSuccess) return@addCallback
}

if (binding.mainMotionLayout.progress == 0F) {
Expand Down Expand Up @@ -497,12 +497,19 @@ class MainActivity : BaseActivity() {
}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
return runOnPlayerFragment { onKeyUp(keyCode, event) } ?: false
return runOnPlayerFragment { onKeyUp(keyCode, event) }
}

private fun <T> runOnPlayerFragment(action: PlayerFragment.() -> T): T? {
return supportFragmentManager.fragments.filterIsInstance<PlayerFragment>()
/**
* Attempt to run code on the player fragment if running
* Returns true if a running player fragment was found and the action got consumed, else false
*/
private fun runOnPlayerFragment(action: PlayerFragment.() -> Unit): Boolean {
supportFragmentManager.fragments.filterIsInstance<PlayerFragment>()
.firstOrNull()
?.let(action)
?.run { return true }

return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ open class BaseActivity : AppCompatActivity() {
}

/**
* Rotate according to the preference
* Rotate the screen according to the app orientation preference
*/
fun requestOrientationChange() {
requestedOrientation = screenOrientationPref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.view.marginStart
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.commit
import androidx.media3.common.C
import androidx.media3.common.Player
import androidx.media3.common.text.Cue
Expand Down

0 comments on commit 38fb470

Please sign in to comment.