From f9e3a324e47a81a30aade003cf6f829d03c81414 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 27 Dec 2018 14:34:29 -0500 Subject: Convert remaining view observables --- .../pitchedapps/frost/views/FrostContentView.kt | 62 +++++++++++----------- .../pitchedapps/frost/views/FrostRecyclerView.kt | 6 +-- 2 files changed, 33 insertions(+), 35 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views') 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 72b81b37..38591d38 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -39,17 +39,14 @@ import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.WEB_LOAD_DELAY import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.disposables.CompositeDisposable 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.channels.ReceiveChannel import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -89,13 +86,9 @@ abstract class FrostContentView @JvmOverloads constructor( override val core: FrostContentCore get() = coreView - override val progressObservable: PublishSubject = PublishSubject.create() - override val refreshObservable: PublishSubject = PublishSubject.create() - override val titleObservable: BehaviorSubject = BehaviorSubject.create() - - override val refreshChannel: BroadcastChannel = BroadcastChannel(Channel.UNLIMITED) - override val progressChannel: BroadcastChannel = BroadcastChannel(Channel.UNLIMITED) - override val titleChannel: BroadcastChannel = BroadcastChannel(Channel.UNLIMITED) + override val refreshChannel: BroadcastChannel = BroadcastChannel(100) + override val progressChannel: BroadcastChannel = BroadcastChannel(100) + override val titleChannel: BroadcastChannel = BroadcastChannel(100) override lateinit var scope: CoroutineScope @@ -133,9 +126,13 @@ abstract class FrostContentView @JvmOverloads constructor( reload(true) } } + // Begin subscription in the main thread + val refreshReceiver = refreshChannel.openSubscription() + val progressReceiver = progressChannel.openSubscription() + scope.launch(Dispatchers.Default) { launch { - for (r in refreshChannel.openSubscription()) { + for (r in refreshReceiver) { withContext(Dispatchers.Main) { refresh.isRefreshing = r refresh.isEnabled = true @@ -143,7 +140,7 @@ abstract class FrostContentView @JvmOverloads constructor( } } launch { - for (p in progressChannel.openSubscription()) { + for (p in progressReceiver) { withContext(Dispatchers.Main) { progress.invisibleIf(p == 100) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) @@ -184,6 +181,7 @@ abstract class FrostContentView @JvmOverloads constructor( private var dispose: Disposable? = null private var transitionStart: Long = -1 + private var refreshReceiver: ReceiveChannel? = null /** * Hook onto the refresh observable for one cycle @@ -191,32 +189,32 @@ abstract class FrostContentView @JvmOverloads constructor( * The cycle only starts on the first load since there may have been another process when this is registered */ override fun registerTransition(urlChanged: Boolean, animate: Boolean): Boolean { - if (!urlChanged && dispose != null) { + if (!urlChanged && refreshReceiver != null) { L.v { "Consuming url load" } return false // still in progress; do not bother with load } L.v { "Registered transition" } with(coreView) { - var loading = dispose != null - dispose?.dispose() - dispose = refreshObservable - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { - if (it) { - loading = true - transitionStart = System.currentTimeMillis() - clearAnimation() - if (isVisible) - fadeOut(duration = 200L) - } else if (loading) { - loading = false - if (animate && Prefs.animate) circularReveal(offset = WEB_LOAD_DELAY) - else fadeIn(duration = 200L, offset = WEB_LOAD_DELAY) - L.v { "Transition loaded in ${System.currentTimeMillis() - transitionStart} ms" } - dispose?.dispose() - dispose = null + refreshReceiver = refreshChannel.openSubscription().also { receiver -> + scope.launch(Dispatchers.Main) { + var loading = false + for (r in receiver) { + if (r) { + loading = true + transitionStart = System.currentTimeMillis() + clearAnimation() + if (isVisible) + fadeOut(duration = 200L) + } else if (loading) { + if (animate && Prefs.animate) circularReveal(offset = WEB_LOAD_DELAY) + else fadeIn(duration = 200L, offset = WEB_LOAD_DELAY) + L.v { "Transition loaded in ${System.currentTimeMillis() - transitionStart} ms" } + receiver.cancel() + refreshReceiver = null + } } } + } } return true } 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 fb20c3ba..2ba78c5e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt @@ -73,10 +73,10 @@ class FrostRecyclerView @JvmOverloads constructor( override fun reloadBase(animate: Boolean) { if (Prefs.animate) fadeOut(onFinish = onReloadClear) scope.launch { - parent.refreshChannel.send(true) + parent.refreshChannel.offer(true) val loaded = recyclerContract.reload { parent.progressChannel.offer(it) } - parent.progressChannel.send(100) - parent.refreshChannel.send(false) + parent.progressChannel.offer(100) + parent.refreshChannel.offer(false) if (Prefs.animate) circularReveal() } } -- cgit v1.2.3