aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2021-11-22 22:53:00 -0800
committerAllan Wang <me@allanwang.ca>2021-11-22 22:53:00 -0800
commit779ec08188f4bda736b3e0f2940570f1f7eb49e1 (patch)
tree2bbff3f522055f01fc8ad5074e619d739017ddef
parent1dd7be9174f1740aa1cae29f6d62d6f83f5917ba (diff)
downloadfrost-779ec08188f4bda736b3e0f2940570f1f7eb49e1.tar.gz
frost-779ec08188f4bda736b3e0f2940570f1f7eb49e1.tar.bz2
frost-779ec08188f4bda736b3e0f2940570f1f7eb49e1.zip
Migrate progress channel to flow
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt4
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt15
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt4
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt6
4 files changed, 18 insertions, 11 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt
index 1d429138..8ebf7af7 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt
@@ -65,7 +65,9 @@ interface FrostContentParent : DynamicUiContract {
/**
* Observable to get data on refresh progress, with range [0, 100]
*/
- val progressChannel: BroadcastChannel<Int>
+ val progressFlow: SharedFlow<Int>
+
+ val progressEmit: FrostEmitter<Int>
/**
* Observable to get new title data (unique values only)
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
index 1891a786..3ec80f36 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
@@ -22,7 +22,6 @@ import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ProgressBar
-import ca.allanwang.kau.utils.ContextHelper
import ca.allanwang.kau.utils.bindView
import ca.allanwang.kau.utils.circularReveal
import ca.allanwang.kau.utils.fadeIn
@@ -39,7 +38,6 @@ import com.pitchedapps.frost.contracts.FrostContentParent
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.WEB_LOAD_DELAY
import com.pitchedapps.frost.injectors.ThemeProvider
-import com.pitchedapps.frost.kotlin.subscribeDuringJob
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.web.FrostEmitter
@@ -51,6 +49,7 @@ import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collect
@@ -146,7 +145,13 @@ abstract class FrostContentViewBase(
override val refreshEmit: FrostEmitter<Boolean> =
FrostEmitter { refreshMutableFlow.tryEmit(it) }
- override val progressChannel: BroadcastChannel<Int> = ConflatedBroadcastChannel()
+ private val progressMutableFlow = MutableStateFlow(0)
+
+ override val progressFlow: SharedFlow<Int> = progressMutableFlow.asSharedFlow()
+
+ override val progressEmit: FrostEmitter<Int> =
+ FrostEmitter { progressMutableFlow.tryEmit(it) }
+
override val titleChannel: BroadcastChannel<String> = ConflatedBroadcastChannel()
override lateinit var scope: CoroutineScope
@@ -200,13 +205,13 @@ abstract class FrostContentViewBase(
refresh.isRefreshing = r
}.launchIn(scope)
- progressChannel.subscribeDuringJob(scope, ContextHelper.coroutineContext) { p ->
+ progressFlow.onEach { p ->
progress.invisibleIf(p == 100)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
progress.setProgress(p, true)
else
progress.progress = p
- }
+ }.launchIn(scope)
}
override fun reloadTheme() {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt
index a2b71572..9e21ede8 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt
@@ -80,8 +80,8 @@ class FrostRecyclerView @JvmOverloads constructor(
if (prefs.animate) fadeOut(onFinish = onReloadClear)
scope.launch {
parent.refreshEmit(true)
- recyclerContract.reload { parent.progressChannel.offer(it) }
- parent.progressChannel.offer(100)
+ recyclerContract.reload { parent.progressEmit(it) }
+ parent.progressEmit(100)
parent.refreshEmit(false)
if (prefs.animate) circularReveal()
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
index 372a7bad..9f2437b0 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt
@@ -53,7 +53,7 @@ class FrostChromeClient(
// private val refresh: SendChannel<Boolean> = web.parent.refreshChannel
private val refreshEmit = web.parent.refreshEmit
- private val progress: SendChannel<Int> = web.parent.progressChannel
+ private val progressEmit = web.parent.progressEmit
private val title: SendChannel<String> = web.parent.titleChannel
private val context = web.context!!
@@ -74,7 +74,7 @@ class FrostChromeClient(
override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress)
- progress.offer(newProgress)
+ progressEmit(newProgress)
}
override fun onShowFileChooser(
@@ -89,7 +89,7 @@ class FrostChromeClient(
private fun JsResult.frostCancel() {
cancel()
refreshEmit(false)
- progress.offer(100)
+ progressEmit(100)
}
override fun onJsAlert(