Use flow for DownloadsScene UI updates
This commit is contained in:
1 parent
23a44e2f3b
commit
a148fd6ec3
5 files changed
+32
-63
No files matched your search
@@ -272,23 +272,23 @@ object DownloadManager : OnSpiderListener {
|
||||
private val _stateFlow = callbackFlow {
|
||||
val listener = object : DownloadInfoListener {
|
||||
override fun onAdd(info: DownloadInfo, list: List<DownloadInfo>, position: Int) {
|
||||
trySend(info)
|
||||
trySend(info.gid)
|
||||
}
|
||||
|
||||
override fun onUpdate(info: DownloadInfo, list: List<DownloadInfo>) {
|
||||
trySend(info)
|
||||
trySend(info.gid)
|
||||
}
|
||||
|
||||
override fun onUpdateAll() {
|
||||
// No-op
|
||||
trySend(0)
|
||||
}
|
||||
|
||||
override fun onReload() {
|
||||
// No-op
|
||||
trySend(0)
|
||||
}
|
||||
|
||||
override fun onRemove(info: DownloadInfo, list: List<DownloadInfo>, position: Int) {
|
||||
trySend(info)
|
||||
trySend(info.gid)
|
||||
}
|
||||
}
|
||||
addDownloadInfoListener(listener)
|
||||
@@ -297,7 +297,9 @@ object DownloadManager : OnSpiderListener {
|
||||
}
|
||||
}.shareIn(callbackFlowScope, SharingStarted.Eagerly)
|
||||
|
||||
fun stateFlow(gid: Long) = _stateFlow.filter { it.gid == gid }
|
||||
fun stateFlow(gid: Long) = _stateFlow.filter { it == gid }
|
||||
|
||||
fun stateFlow() = _stateFlow
|
||||
|
||||
suspend fun startAllDownload() {
|
||||
var update = false
|
||||
|
||||
@@ -26,7 +26,10 @@ import androidx.core.view.GravityCompat
|
||||
import androidx.core.view.SoftwareKeyboardControllerCompat
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelStoreOwner
|
||||
import androidx.lifecycle.setViewTreeLifecycleOwner
|
||||
import androidx.lifecycle.setViewTreeViewModelStoreOwner
|
||||
import androidx.savedstate.SavedStateRegistryOwner
|
||||
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
|
||||
import com.google.android.material.sidesheet.SideSheetDialog
|
||||
import com.hippo.ehviewer.ui.MainActivity
|
||||
@@ -155,8 +158,10 @@ abstract class BaseScene : Fragment() {
|
||||
createDrawerView(savedInstanceState)?.let {
|
||||
sideSheetDialog = SideSheetDialog(requireContext()).apply {
|
||||
window?.decorView?.apply {
|
||||
setViewTreeLifecycleOwner(viewLifecycleOwner)
|
||||
setViewTreeSavedStateRegistryOwner(this@BaseScene)
|
||||
val owner = viewLifecycleOwner
|
||||
setViewTreeLifecycleOwner(owner)
|
||||
setViewTreeViewModelStoreOwner(owner as ViewModelStoreOwner)
|
||||
setViewTreeSavedStateRegistryOwner(owner as SavedStateRegistryOwner)
|
||||
}
|
||||
setContentView(it)
|
||||
}
|
||||
|
||||
@@ -81,7 +81,6 @@ import com.hippo.ehviewer.databinding.ItemDownloadBinding
|
||||
import com.hippo.ehviewer.databinding.ItemDrawerListBinding
|
||||
import com.hippo.ehviewer.databinding.SceneDownloadBinding
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener
|
||||
import com.hippo.ehviewer.download.DownloadService
|
||||
import com.hippo.ehviewer.download.DownloadService.Companion.clear
|
||||
import com.hippo.ehviewer.download.downloadDir
|
||||
@@ -113,6 +112,7 @@ import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import eu.kanade.tachiyomi.util.system.pxToDp
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import rikka.core.res.resolveColor
|
||||
import com.hippo.ehviewer.download.DownloadManager as downloadManager
|
||||
@@ -120,7 +120,6 @@ import com.hippo.ehviewer.download.DownloadManager as downloadManager
|
||||
@SuppressLint("RtlHardcoded")
|
||||
class DownloadsScene :
|
||||
SearchBarScene(),
|
||||
DownloadInfoListener,
|
||||
OnClickFabListener {
|
||||
/*---------------
|
||||
Whole life cycle
|
||||
@@ -181,7 +180,6 @@ class DownloadsScene :
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
DownloadManager.addDownloadInfoListener(this)
|
||||
if (savedInstanceState == null) {
|
||||
onInit()
|
||||
} else {
|
||||
@@ -189,11 +187,6 @@ class DownloadsScene :
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
DownloadManager.removeDownloadInfoListener(this)
|
||||
}
|
||||
|
||||
private fun filter(info: DownloadInfo) = info.run {
|
||||
(mType == -1 || state == mType) &&
|
||||
mKeyword?.let {
|
||||
@@ -358,6 +351,11 @@ class DownloadsScene :
|
||||
addAboveSnackView(fabLayout)
|
||||
updateForLabel()
|
||||
}
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
DownloadManager.stateFlow().collectLatest {
|
||||
updateInfoList()
|
||||
}
|
||||
}
|
||||
setOnApplySearch {
|
||||
mKeyword = it.takeUnless { it.isEmpty() }
|
||||
updateInfoList()
|
||||
@@ -376,11 +374,6 @@ class DownloadsScene :
|
||||
hideSearchFab(true)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
updateTitle()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
binding.recyclerView.stopScroll()
|
||||
@@ -615,30 +608,6 @@ class DownloadsScene :
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateUI() = viewLifecycleOwner.lifecycleScope.launchUI {
|
||||
updateInfoList()
|
||||
}
|
||||
|
||||
override fun onAdd(info: DownloadInfo, list: List<DownloadInfo>, position: Int) {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
override fun onUpdate(info: DownloadInfo, list: List<DownloadInfo>) {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
override fun onUpdateAll() {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
override fun onReload() {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
override fun onRemove(info: DownloadInfo, list: List<DownloadInfo>, position: Int) {
|
||||
updateUI()
|
||||
}
|
||||
|
||||
private class DownloadLabelHolder(val binding: ItemDrawerListBinding) :
|
||||
RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
@@ -649,16 +618,14 @@ class DownloadsScene :
|
||||
DownloadLabelHolder(ItemDrawerListBinding.inflate(mInflater, parent, false))
|
||||
holder.itemView.setOnClickListener {
|
||||
val position = holder.bindingAdapterPosition
|
||||
val label1: String? = if (position == 0) {
|
||||
null
|
||||
} else {
|
||||
mLabels[position]
|
||||
}
|
||||
if (mLabel != label1) {
|
||||
mLabel = label1
|
||||
val label = mLabels[position].takeUnless { position == 0 }
|
||||
if (mLabel != label) {
|
||||
mKeyword = null
|
||||
setSearchBarText(null)
|
||||
mLabel = label
|
||||
updateForLabel()
|
||||
closeSideSheet()
|
||||
}
|
||||
closeSideSheet()
|
||||
}
|
||||
holder.binding.edit.setOnClickListener {
|
||||
val context = context
|
||||
|
||||
@@ -87,12 +87,11 @@ import com.hippo.ehviewer.util.getValue
|
||||
import com.hippo.ehviewer.util.lazyMut
|
||||
import com.hippo.ehviewer.util.setValue
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import rikka.core.res.resolveColor
|
||||
import java.time.Instant
|
||||
@@ -337,9 +336,8 @@ class FavoritesScene : SearchBarScene() {
|
||||
binding.refreshLayout.setOnRefreshListener { mAdapter?.refresh() }
|
||||
val transition = ViewTransition(binding.refreshLayout, binding.progress, binding.tip)
|
||||
val empty = getString(R.string.gallery_list_empty_hit)
|
||||
adapter.addLoadStateListener {
|
||||
viewLifecycleOwner.lifecycleScope.launchUI {
|
||||
ensureActive()
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
adapter.loadStateFlow.collectLatest {
|
||||
when (val state = it.refresh) {
|
||||
is LoadState.Loading -> {
|
||||
showSearchBar()
|
||||
|
||||
@@ -104,11 +104,9 @@ import com.hippo.ehviewer.util.getValue
|
||||
import com.hippo.ehviewer.util.lazyMut
|
||||
import com.hippo.ehviewer.util.setValue
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
@@ -365,9 +363,8 @@ class GalleryListScene : SearchBarScene() {
|
||||
binding.refreshLayout.setOnRefreshListener { mAdapter?.refresh() }
|
||||
val transition = ViewTransition(binding.refreshLayout, binding.progress, binding.tip)
|
||||
val empty = getString(R.string.gallery_list_empty_hit)
|
||||
adapter.addLoadStateListener {
|
||||
viewLifecycleOwner.lifecycleScope.launchUI {
|
||||
ensureActive()
|
||||
launch {
|
||||
adapter.loadStateFlow.collectLatest {
|
||||
when (val state = it.refresh) {
|
||||
is LoadState.Loading -> {
|
||||
showSearchBar()
|
||||
|
||||
Reference in new issue
Block a user