@@ -7,15 +7,11 @@ import com.tans.tfiletransporter.R
77import com.tans.tfiletransporter.databinding.MyDirFragmentBinding
88import com.tans.tfiletransporter.file.*
99import kotlinx.coroutines.Dispatchers
10- import androidx.activity.addCallback
11- import androidx.core.view.ViewCompat
12- import androidx.core.view.WindowInsetsCompat
1310import com.tans.tfiletransporter.Settings
1411import com.tans.tfiletransporter.logs.AndroidLog
1512import com.tans.tfiletransporter.transferproto.fileexplore.FileExplore
1613import com.tans.tfiletransporter.transferproto.fileexplore.requestSendFilesSuspend
1714import com.tans.tfiletransporter.ui.FileTreeUI
18- import com.tans.tfiletransporter.utils.dp2px
1915import com.tans.tuiutils.fragment.BaseCoroutineStateFragment
2016import kotlinx.coroutines.CoroutineScope
2117import kotlinx.coroutines.channels.BufferOverflow
@@ -32,9 +28,8 @@ class MyDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
3228
3329 override val layoutId: Int = R .layout.my_dir_fragment
3430
35- private val onBackPressedDispatcher: OnBackPressedDispatcher by lazy {
36- requireActivity().onBackPressedDispatcher
37- }
31+ private val onBackPressedDispatcher: OnBackPressedDispatcher
32+ get() = requireActivity().onBackPressedDispatcher
3833
3934 private val fileExplore: FileExplore by lazy {
4035 (requireActivity() as FileTransportActivity ).fileExplore
@@ -43,9 +38,11 @@ class MyDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
4338 private var fileTreeUI : FileTreeUI ? = null
4439
4540 private val onBackPressedCallback: OnBackPressedCallback by lazy {
46- onBackPressedDispatcher.addCallback {
47- uiCoroutineScope?.launch {
48- fileTreeUI?.backPress()
41+ object : OnBackPressedCallback (false ) {
42+ override fun handleOnBackPressed () {
43+ uiCoroutineScope?.launch {
44+ fileTreeUI?.backPress()
45+ }
4946 }
5047 }
5148 }
@@ -65,6 +62,7 @@ class MyDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
6562 override fun CoroutineScope.firstLaunchInitDataCoroutine () { }
6663
6764 override fun CoroutineScope.bindContentViewCoroutine (contentView : View ) {
65+ onBackPressedDispatcher.addCallback(this @MyDirFragment, onBackPressedCallback)
6866 val viewBinding = MyDirFragmentBinding .bind(contentView)
6967 val fileTreeUI = FileTreeUI (
7068 viewBinding = viewBinding.fileTreeLayout,
@@ -91,6 +89,7 @@ class MyDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
9189 fileTreeUI.stateFlow()
9290 .map { it.fileTree }
9391 .distinctUntilChanged()
92+ .flowOn(Dispatchers .Main )
9493 .collect { tree ->
9594 val tab = context.currentState().selectedTabType
9695 onBackPressedCallback.isEnabled = ! tree.isRootFileTree() && tab == FileTransportActivity .Companion .DirTabType .MyDir
@@ -103,7 +102,8 @@ class MyDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
103102 .distinctUntilChanged()
104103 .flowOn(Dispatchers .Main )
105104 .collect { tab ->
106- onBackPressedCallback.isEnabled = tab == FileTransportActivity .Companion .DirTabType .MyDir
105+ val tree = fileTreeUI.currentState().fileTree
106+ onBackPressedCallback.isEnabled = ! tree.isRootFileTree() && tab == FileTransportActivity .Companion .DirTabType .MyDir
107107 }
108108 }
109109
0 commit comments