aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-27 02:15:10 -0500
committerAllan Wang <me@allanwang.ca>2018-12-27 02:15:10 -0500
commite6dcbd7b32dc49b11184b6beca598819c3f071fd (patch)
treea02691a1eaf1b1506930c7e50c5f43f6f0fc953a /app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
parent7d85262ada198501d2d5844e1196c9b45f4a38f5 (diff)
downloadfrost-e6dcbd7b32dc49b11184b6beca598819c3f071fd.tar.gz
frost-e6dcbd7b32dc49b11184b6beca598819c3f071fd.tar.bz2
frost-e6dcbd7b32dc49b11184b6beca598819c3f071fd.zip
Begin replacing observables with channels
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt66
1 files changed, 43 insertions, 23 deletions
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 d17a424c..72b81b37 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt
@@ -45,6 +45,13 @@ import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.channels.BroadcastChannel
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
class FrostContentWeb @JvmOverloads constructor(
context: Context,
@@ -66,6 +73,7 @@ class FrostContentRecycler @JvmOverloads constructor(
override val layoutRes: Int = R.layout.view_content_base_recycler
}
+@UseExperimental(ExperimentalCoroutinesApi::class)
abstract class FrostContentView<out T> @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@@ -85,7 +93,11 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
override val refreshObservable: PublishSubject<Boolean> = PublishSubject.create()
override val titleObservable: BehaviorSubject<String> = BehaviorSubject.create()
- private val compositeDisposable = CompositeDisposable()
+ override val refreshChannel: BroadcastChannel<Boolean> = BroadcastChannel(Channel.UNLIMITED)
+ override val progressChannel: BroadcastChannel<Int> = BroadcastChannel(Channel.UNLIMITED)
+ override val titleChannel: BroadcastChannel<String> = BroadcastChannel(Channel.UNLIMITED)
+
+ override lateinit var scope: CoroutineScope
override lateinit var baseUrl: String
override var baseEnum: FbItem? = null
@@ -107,24 +119,6 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
protected fun init() {
inflate(context, layoutRes, this)
coreView.parent = this
-
- // bind observables
- progressObservable.observeOn(AndroidSchedulers.mainThread()).subscribe {
- progress.invisibleIf(it == 100)
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
- progress.setProgress(it, true)
- else
- progress.progress = it
- }.addTo(compositeDisposable)
-
- refreshObservable
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe {
- refresh.isRefreshing = it
- refresh.isEnabled = true
- }.addTo(compositeDisposable)
- refresh.setOnRefreshListener { coreView.reload(true) }
-
reloadThemeSelf()
}
@@ -132,7 +126,34 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
baseUrl = container.baseUrl
baseEnum = container.baseEnum
init()
+ scope = container
core.bind(container)
+ refresh.setOnRefreshListener {
+ with(coreView) {
+ reload(true)
+ }
+ }
+ scope.launch(Dispatchers.Default) {
+ launch {
+ for (r in refreshChannel.openSubscription()) {
+ withContext(Dispatchers.Main) {
+ refresh.isRefreshing = r
+ refresh.isEnabled = true
+ }
+ }
+ }
+ launch {
+ for (p in progressChannel.openSubscription()) {
+ withContext(Dispatchers.Main) {
+ progress.invisibleIf(p == 100)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+ progress.setProgress(p, true)
+ else
+ progress.progress = p
+ }
+ }
+ }
+ }
}
override fun reloadTheme() {
@@ -155,11 +176,10 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
}
override fun destroy() {
- titleObservable.onComplete()
- progressObservable.onComplete()
- refreshObservable.onComplete()
+ titleChannel.close()
+ progressChannel.close()
+ refreshChannel.close()
core.destroy()
- compositeDisposable.dispose()
}
private var dispose: Disposable? = null