mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
Merge pull request #2814 from Isira-Seneviratne/addCallback
Use OnBackPressedDispatcher.addCallback() extension.
This commit is contained in:
commit
f119e39a1e
@ -11,7 +11,7 @@ import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ScrollView
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.addCallback
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.core.view.children
|
||||
@ -144,38 +144,36 @@ class MainActivity : BaseActivity() {
|
||||
val playerViewModel = ViewModelProvider(this)[PlayerViewModel::class.java]
|
||||
|
||||
// new way of handling back presses
|
||||
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
if (playerViewModel.isFullscreen.value == true) {
|
||||
for (fragment in supportFragmentManager.fragments) {
|
||||
if (fragment is PlayerFragment) {
|
||||
fragment.unsetFullscreen()
|
||||
return
|
||||
}
|
||||
onBackPressedDispatcher.addCallback {
|
||||
if (playerViewModel.isFullscreen.value == true) {
|
||||
supportFragmentManager.fragments.filterIsInstance<PlayerFragment>()
|
||||
.firstOrNull()
|
||||
?.let {
|
||||
it.unsetFullscreen()
|
||||
return@addCallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (binding.mainMotionLayout.progress == 0F) {
|
||||
runCatching {
|
||||
minimizePlayer()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
when (navController.currentDestination?.id) {
|
||||
startFragmentId -> {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
R.id.searchResultFragment -> {
|
||||
navController.popBackStack(R.id.searchFragment, true) ||
|
||||
navController.popBackStack()
|
||||
}
|
||||
else -> {
|
||||
navController.popBackStack()
|
||||
}
|
||||
if (binding.mainMotionLayout.progress == 0F) {
|
||||
runCatching {
|
||||
minimizePlayer()
|
||||
return@addCallback
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
when (navController.currentDestination?.id) {
|
||||
startFragmentId -> {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
R.id.searchResultFragment -> {
|
||||
navController.popBackStack(R.id.searchFragment, true) ||
|
||||
navController.popBackStack()
|
||||
}
|
||||
else -> {
|
||||
navController.popBackStack()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadIntentData()
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ package com.github.libretube.ui.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.addCallback
|
||||
import androidx.fragment.app.commit
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.databinding.ActivityNointernetBinding
|
||||
import com.github.libretube.ui.base.BaseActivity
|
||||
@ -40,21 +41,15 @@ class NoInternetActivity : BaseActivity() {
|
||||
|
||||
setContentView(binding.root)
|
||||
|
||||
onBackPressedDispatcher.addCallback(
|
||||
this,
|
||||
object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
supportFragmentManager.fragments.forEach {
|
||||
if (it is DownloadsFragment) {
|
||||
supportFragmentManager.beginTransaction()
|
||||
.remove(it)
|
||||
.commit()
|
||||
return
|
||||
}
|
||||
onBackPressedDispatcher.addCallback(this) {
|
||||
supportFragmentManager.fragments.filterIsInstance<DownloadsFragment>()
|
||||
.firstOrNull()
|
||||
?.let {
|
||||
supportFragmentManager.commit {
|
||||
remove(it)
|
||||
}
|
||||
finishAffinity()
|
||||
}
|
||||
}
|
||||
)
|
||||
?: finishAffinity()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.github.libretube.ui.activities
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.addCallback
|
||||
import androidx.fragment.app.commit
|
||||
import androidx.fragment.app.replace
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.databinding.ActivitySettingsBinding
|
||||
import com.github.libretube.ui.base.BaseActivity
|
||||
@ -29,25 +31,16 @@ class SettingsActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
// new way of dealing with back presses instead of onBackPressed()
|
||||
onBackPressedDispatcher.addCallback(
|
||||
this, // lifecycle owner
|
||||
object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
when (supportFragmentManager.findFragmentById(R.id.settings)) {
|
||||
is MainSettings -> {
|
||||
finishAndRemoveTask()
|
||||
}
|
||||
else -> {
|
||||
supportFragmentManager
|
||||
.beginTransaction()
|
||||
.replace(R.id.settings, MainSettings())
|
||||
.commit()
|
||||
changeTopBarText(getString(R.string.settings))
|
||||
}
|
||||
}
|
||||
onBackPressedDispatcher.addCallback(this) {
|
||||
if (supportFragmentManager.findFragmentById(R.id.settings) is MainSettings) {
|
||||
finishAndRemoveTask()
|
||||
} else {
|
||||
supportFragmentManager.commit {
|
||||
replace<MainSettings>(R.id.settings)
|
||||
}
|
||||
changeTopBarText(getString(R.string.settings))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun changeTopBarText(text: String) {
|
||||
|
Loading…
Reference in New Issue
Block a user