change runnable to coroutine job, and fix fragment compose memory leak (#152)

This commit is contained in:
Astesia authored and GitHub committed 2023-10-30 22:39:29 +08:00
1 parent afe2bac672
commit aafa750ca3
1 file changed
+16 -15
@@ -33,7 +33,6 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.currentRecomposeScope
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.stringResource
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
@@ -94,6 +93,7 @@ import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
@@ -153,15 +153,7 @@ class FavoritesScene : SearchBarScene() {
private val binding get() = _binding!!
private var mAdapter: GalleryAdapter? = null
private val tracker get() = mAdapter!!.tracker!!
private val showNormalFabsRunnable = Runnable {
updateJumpFab() // index: 0, 2
binding.fabLayout.run {
setSecondaryFabVisibilityAt(1, true)
for (i in 3..6) {
setSecondaryFabVisibilityAt(i, false)
}
}
}
private var showNormalFabsJob: Job? = null
private val dialogState = DialogState()
@@ -233,7 +225,7 @@ class FavoritesScene : SearchBarScene() {
savedInstanceState: Bundle?,
): View {
_binding = SceneFavoritesBinding.inflate(inflater, container!!)
container.addView(ComposeView(inflater.context).apply { setMD3Content { dialogState.Intercept() } })
container.addView(ComposeWithViewLifecycle().apply { setMD3Content { dialogState.Intercept() } })
setOnApplySearch {
if (!tracker.isInCustomChoice) {
switchFav(urlBuilder.favCat, it)
@@ -444,9 +436,10 @@ class FavoritesScene : SearchBarScene() {
binding.recyclerView.stopScroll()
mAdapter = null
_binding = null
showNormalFabsJob = null
}
override fun onCreateDrawerView(inflater: LayoutInflater) = ComposeView(inflater.context).apply {
override fun onCreateDrawerView(inflater: LayoutInflater) = ComposeWithViewLifecycle().apply {
setMD3Content {
val localFavCount by vm.localFavCount.collectAsState(0)
ElevatedCard {
@@ -522,12 +515,20 @@ class FavoritesScene : SearchBarScene() {
private fun showNormalFab() {
// Delay showing normal fab to avoid mutation
SimpleHandler.removeCallbacks(showNormalFabsRunnable)
SimpleHandler.postDelayed(showNormalFabsRunnable, 300)
showNormalFabsJob = viewLifecycleOwner.lifecycleScope.launch {
delay(300)
updateJumpFab() // index: 0, 2
binding.fabLayout.run {
setSecondaryFabVisibilityAt(1, true)
for (i in 3..6) {
setSecondaryFabVisibilityAt(i, false)
}
}
}
}
private fun showSelectionFab() {
SimpleHandler.removeCallbacks(showNormalFabsRunnable)
showNormalFabsJob?.cancel()
binding.fabLayout.run {
for (i in 0..2) {
setSecondaryFabVisibilityAt(i, false)