Skip to content

Commit 7843b1a

Browse files
author
tanpengcheng
committed
[fix] Fix MyDirFragment and RemoteDirFragment handle back press bug.
1 parent d639832 commit 7843b1a

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

app/src/main/java/com/tans/tfiletransporter/ui/filetransport/MyDirFragment.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ import com.tans.tfiletransporter.R
77
import com.tans.tfiletransporter.databinding.MyDirFragmentBinding
88
import com.tans.tfiletransporter.file.*
99
import kotlinx.coroutines.Dispatchers
10-
import androidx.activity.addCallback
11-
import androidx.core.view.ViewCompat
12-
import androidx.core.view.WindowInsetsCompat
1310
import com.tans.tfiletransporter.Settings
1411
import com.tans.tfiletransporter.logs.AndroidLog
1512
import com.tans.tfiletransporter.transferproto.fileexplore.FileExplore
1613
import com.tans.tfiletransporter.transferproto.fileexplore.requestSendFilesSuspend
1714
import com.tans.tfiletransporter.ui.FileTreeUI
18-
import com.tans.tfiletransporter.utils.dp2px
1915
import com.tans.tuiutils.fragment.BaseCoroutineStateFragment
2016
import kotlinx.coroutines.CoroutineScope
2117
import 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

app/src/main/java/com/tans/tfiletransporter/ui/filetransport/RemoteDirFragment.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.tans.tfiletransporter.ui.filetransport
33
import android.view.View
44
import androidx.activity.OnBackPressedCallback
55
import androidx.activity.OnBackPressedDispatcher
6-
import androidx.activity.addCallback
76
import com.tans.tfiletransporter.R
87
import com.tans.tfiletransporter.Settings
98
import com.tans.tfiletransporter.databinding.RemoteDirFragmentBinding
@@ -15,10 +14,12 @@ import com.tans.tfiletransporter.transferproto.fileexplore.requestScanDirSuspend
1514
import com.tans.tfiletransporter.ui.FileTreeUI
1615
import com.tans.tuiutils.fragment.BaseCoroutineStateFragment
1716
import kotlinx.coroutines.CoroutineScope
17+
import kotlinx.coroutines.Dispatchers
1818
import kotlinx.coroutines.channels.BufferOverflow
1919
import kotlinx.coroutines.channels.Channel
2020
import kotlinx.coroutines.flow.MutableStateFlow
2121
import kotlinx.coroutines.flow.distinctUntilChanged
22+
import kotlinx.coroutines.flow.flowOn
2223
import kotlinx.coroutines.flow.map
2324
import kotlinx.coroutines.launch
2425
import java.io.File
@@ -32,13 +33,16 @@ class RemoteDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
3233

3334
private var fileTreeUI: FileTreeUI? = null
3435

35-
private val onBackPressedDispatcher: OnBackPressedDispatcher by lazy {
36-
requireActivity().onBackPressedDispatcher
37-
}
36+
private val onBackPressedDispatcher: OnBackPressedDispatcher
37+
get() = requireActivity().onBackPressedDispatcher
3838

3939
private val onBackPressedCallback: OnBackPressedCallback by lazy {
40-
onBackPressedDispatcher.addCallback {
41-
fileTreeUI?.backPress()
40+
object : OnBackPressedCallback(false) {
41+
override fun handleOnBackPressed() {
42+
uiCoroutineScope?.launch {
43+
fileTreeUI?.backPress()
44+
}
45+
}
4246
}
4347
}
4448

@@ -61,6 +65,7 @@ class RemoteDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
6165
override fun CoroutineScope.firstLaunchInitDataCoroutine() { }
6266

6367
override fun CoroutineScope.bindContentViewCoroutine(contentView: View) {
68+
onBackPressedDispatcher.addCallback(this@RemoteDirFragment, onBackPressedCallback)
6469
val viewBinding = RemoteDirFragmentBinding.bind(contentView)
6570
val context = requireActivity() as FileTransportActivity
6671
val fileTreeUI = FileTreeUI(
@@ -117,6 +122,8 @@ class RemoteDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
117122
launch {
118123
fileTreeUI.stateFlow()
119124
.map { it.fileTree }
125+
.distinctUntilChanged()
126+
.flowOn(Dispatchers.Main)
120127
.collect { tree ->
121128
val tab = context.currentState().selectedTabType
122129
onBackPressedCallback.isEnabled = !tree.isRootFileTree() && tab == FileTransportActivity.Companion.DirTabType.RemoteDir
@@ -127,8 +134,10 @@ class RemoteDirFragment : BaseCoroutineStateFragment<Unit>(Unit) {
127134
context.stateFlow()
128135
.map { it.selectedTabType }
129136
.distinctUntilChanged()
130-
.collect {
131-
onBackPressedCallback.isEnabled = it == FileTransportActivity.Companion.DirTabType.RemoteDir
137+
.flowOn(Dispatchers.Main)
138+
.collect { tab ->
139+
val tree = fileTreeUI.currentState().fileTree
140+
onBackPressedCallback.isEnabled = !tree.isRootFileTree() && tab == FileTransportActivity.Companion.DirTabType.RemoteDir
132141
}
133142
}
134143

0 commit comments

Comments
 (0)