From 1dd7be9174f1740aa1cae29f6d62d6f83f5917ba Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 22 Nov 2021 22:24:17 -0800 Subject: Migrate refresh channel to flow --- .../pitchedapps/frost/views/FrostContentView.kt | 79 ++++++++++++++-------- 1 file changed, 52 insertions(+), 27 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') 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 c30ee199..1891a786 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -42,12 +42,23 @@ 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 import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.channels.ConflatedBroadcastChannel import kotlinx.coroutines.channels.ReceiveChannel +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.runningFold +import kotlinx.coroutines.flow.transformWhile import javax.inject.Inject class FrostContentWeb @JvmOverloads constructor( @@ -119,11 +130,22 @@ abstract class FrostContentViewBase( private val refresh: SwipeRefreshLayout by bindView(R.id.content_refresh) private val progress: ProgressBar by bindView(R.id.content_progress) + private val coreView: View by bindView(R.id.content_core) + /** * While this can be conflated, there exist situations where we wish to watch refresh cycles. * Here, we'd need to make sure we don't skip events */ - override val refreshChannel: BroadcastChannel = BroadcastChannel(10) + private val refreshMutableFlow = MutableSharedFlow( + extraBufferCapacity = 10, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + + override val refreshFlow: SharedFlow = refreshMutableFlow.asSharedFlow() + + override val refreshEmit: FrostEmitter = + FrostEmitter { refreshMutableFlow.tryEmit(it) } + override val progressChannel: BroadcastChannel = ConflatedBroadcastChannel() override val titleChannel: BroadcastChannel = ConflatedBroadcastChannel() @@ -160,7 +182,6 @@ abstract class FrostContentViewBase( */ protected fun init() { inflate(context, layoutRes, this) - core.parent = this reloadThemeSelf() } @@ -169,15 +190,15 @@ abstract class FrostContentViewBase( baseEnum = container.baseEnum init() scope = container - core.bind(container) + core.bind(this, container) refresh.setOnRefreshListener { core.reload(true) } - refreshChannel.subscribeDuringJob(scope, ContextHelper.coroutineContext) { r -> + refreshFlow.distinctUntilChanged().onEach { r -> L.v { "Refreshing $r" } refresh.isRefreshing = r - } + }.launchIn(scope) progressChannel.subscribeDuringJob(scope, ContextHelper.coroutineContext) { p -> progress.invisibleIf(p == 100) @@ -220,33 +241,37 @@ abstract class FrostContentViewBase( * 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 && refreshReceiver != null) { + if (!urlChanged && transitionStart != -1L) { L.v { "Consuming url load" } return false // still in progress; do not bother with load } + coreView.transition(animate) + return true + } + + private fun View.transition(animate: Boolean) { L.v { "Registered transition" } - with(core) { - refreshReceiver = refreshChannel.openSubscription().also { receiver -> - scope.launchMain { - 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 - } + transitionStart = 0L // Marker for pending transition + scope.launchMain { + refreshFlow.distinctUntilChanged() + // Pseudo windowed mode + .runningFold(false to false) { (_, prev), curr -> prev to curr } + // Take until prev was loading and current is not loading + // Unlike takeWhile, we include the last state (first non matching) + .transformWhile { emit(it); it != (true to false) } + .onEach { (prev, curr) -> + if (curr) { + transitionStart = System.currentTimeMillis() + clearAnimation() + if (isVisible) + fadeOut(duration = 200L) + } else if (prev) { // prev && !curr + 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" } } - } - } + }.collect() + transitionStart = -1L } - return true } } -- cgit v1.2.3 From 779ec08188f4bda736b3e0f2940570f1f7eb49e1 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 22 Nov 2021 22:53:00 -0800 Subject: Migrate progress channel to flow --- .../pitchedapps/frost/contracts/FrostContentContract.kt | 4 +++- .../com/pitchedapps/frost/views/FrostContentView.kt | 15 ++++++++++----- .../com/pitchedapps/frost/views/FrostRecyclerView.kt | 4 ++-- .../com/pitchedapps/frost/web/FrostChromeClients.kt | 6 +++--- 4 files changed, 18 insertions(+), 11 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') 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 + val progressFlow: SharedFlow + + val progressEmit: FrostEmitter /** * 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 = FrostEmitter { refreshMutableFlow.tryEmit(it) } - override val progressChannel: BroadcastChannel = ConflatedBroadcastChannel() + private val progressMutableFlow = MutableStateFlow(0) + + override val progressFlow: SharedFlow = progressMutableFlow.asSharedFlow() + + override val progressEmit: FrostEmitter = + FrostEmitter { progressMutableFlow.tryEmit(it) } + override val titleChannel: BroadcastChannel = 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 = web.parent.refreshChannel private val refreshEmit = web.parent.refreshEmit - private val progress: SendChannel = web.parent.progressChannel + private val progressEmit = web.parent.progressEmit private val title: SendChannel = 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( -- cgit v1.2.3 From bbfac885b89a79af2c85f5f0df7635770b49a07a Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 22 Nov 2021 22:57:55 -0800 Subject: Convert title channel to flow --- .../kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt | 8 +++----- .../com/pitchedapps/frost/contracts/FrostContentContract.kt | 6 +++--- .../main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt | 8 ++++++-- .../main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt | 5 ++--- 4 files changed, 14 insertions(+), 13 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index 171583ed..ae8d442f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -27,7 +27,6 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout import ca.allanwang.kau.swipe.SwipeBackContract import ca.allanwang.kau.swipe.kauSwipeOnCreate import ca.allanwang.kau.swipe.kauSwipeOnDestroy -import ca.allanwang.kau.utils.ContextHelper import ca.allanwang.kau.utils.bindView import ca.allanwang.kau.utils.copyToClipboard import ca.allanwang.kau.utils.darken @@ -56,7 +55,6 @@ import com.pitchedapps.frost.facebook.USER_AGENT import com.pitchedapps.frost.facebook.USER_AGENT_DESKTOP_CONST import com.pitchedapps.frost.facebook.USER_AGENT_MOBILE_CONST import com.pitchedapps.frost.facebook.formattedFbUrl -import com.pitchedapps.frost.kotlin.subscribeDuringJob import com.pitchedapps.frost.utils.ARG_URL import com.pitchedapps.frost.utils.ARG_USER_ID import com.pitchedapps.frost.utils.BiometricUtils @@ -69,6 +67,8 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.take import kotlinx.coroutines.launch import okhttp3.HttpUrl.Companion.toHttpUrlOrNull @@ -215,9 +215,7 @@ abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT content.bind(this) - content.titleChannel.subscribeDuringJob(this, ContextHelper.coroutineContext) { - toolbar.title = it - } + content.titleFlow.onEach { toolbar.title = it }.launchIn(this) with(web) { userAgentString = userAgent 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 8ebf7af7..d32cb873 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt @@ -21,7 +21,6 @@ import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.web.FrostEmitter import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.flow.SharedFlow /** @@ -72,8 +71,9 @@ interface FrostContentParent : DynamicUiContract { /** * Observable to get new title data (unique values only) */ - // todo note that this should be like a behavior subject vs publish subject - val titleChannel: BroadcastChannel + val titleFlow: SharedFlow + + val titleEmit: FrostEmitter var baseUrl: String 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 3ec80f36..b76f6e39 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -80,7 +80,6 @@ class FrostContentRecycler @JvmOverloads constructor( override val layoutRes: Int = R.layout.view_content_base_recycler } -@UseExperimental(ExperimentalCoroutinesApi::class) abstract class FrostContentView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -152,7 +151,12 @@ abstract class FrostContentViewBase( override val progressEmit: FrostEmitter = FrostEmitter { progressMutableFlow.tryEmit(it) } - override val titleChannel: BroadcastChannel = ConflatedBroadcastChannel() + private val titleMutableFlow = MutableStateFlow("") + + override val titleFlow: SharedFlow = titleMutableFlow.asSharedFlow() + + override val titleEmit: FrostEmitter = + FrostEmitter { titleMutableFlow.tryEmit(it) } override lateinit var scope: CoroutineScope 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 9f2437b0..90345aa2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClients.kt @@ -34,7 +34,6 @@ import com.pitchedapps.frost.contracts.WebFileChooser import com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.views.FrostWebView -import kotlinx.coroutines.channels.SendChannel /** * Created by Allan Wang on 2017-05-31. @@ -54,7 +53,7 @@ class FrostChromeClient( // private val refresh: SendChannel = web.parent.refreshChannel private val refreshEmit = web.parent.refreshEmit private val progressEmit = web.parent.progressEmit - private val title: SendChannel = web.parent.titleChannel + private val titleEmit = web.parent.titleEmit private val context = web.context!! override fun getDefaultVideoPoster(): Bitmap? = @@ -69,7 +68,7 @@ class FrostChromeClient( override fun onReceivedTitle(view: WebView, title: String) { super.onReceivedTitle(view, title) if (title.startsWith("http")) return - this.title.offer(title) + titleEmit(title) } override fun onProgressChanged(view: WebView, newProgress: Int) { -- cgit v1.2.3 From dcd0db9282d92beacd35b3418d924ff3c607dead Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 22 Nov 2021 23:21:34 -0800 Subject: Convert header channel to flow --- .../pitchedapps/frost/activities/MainActivity.kt | 39 ++++++++++++++++------ .../frost/contracts/ActivityContract.kt | 7 +++- .../pitchedapps/frost/views/FrostContentView.kt | 2 -- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 5 ++- 4 files changed, 36 insertions(+), 17 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt index 755064cd..4b18088c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -18,23 +18,33 @@ package com.pitchedapps.frost.activities import android.os.Bundle import androidx.viewpager.widget.ViewPager -import ca.allanwang.kau.utils.withMainContext import com.google.android.material.tabs.TabLayout import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.parsers.BadgeParser -import com.pitchedapps.frost.kotlin.subscribeDuringJob import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.views.BadgedIcon +import com.pitchedapps.frost.web.FrostEmitter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel -import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.mapNotNull +import kotlinx.coroutines.flow.onEach @UseExperimental(ExperimentalCoroutinesApi::class) class MainActivity : BaseMainActivity() { override val fragmentChannel = BroadcastChannel(10) - override val headerBadgeChannel = BroadcastChannel(Channel.CONFLATED) + + private val headerMutableFlow = MutableStateFlow("") + override val headerFlow: SharedFlow = headerMutableFlow.asSharedFlow() + override val headerEmit: FrostEmitter = FrostEmitter { headerMutableFlow.tryEmit(it) } override fun onNestedCreate(savedInstanceState: Bundle?) { with(contentBinding) { @@ -90,12 +100,18 @@ class MainActivity : BaseMainActivity() { (tab.customView as BadgedIcon).badgeText = null } }) - headerBadgeChannel.subscribeDuringJob(this@MainActivity, Dispatchers.IO) { html -> - val data = - BadgeParser.parseFromData(cookie = fbCookie.webCookie, text = html)?.data - ?: return@subscribeDuringJob - L.v { "Badges $data" } - withMainContext { + headerFlow + .filter { it.isNotBlank() } + .mapNotNull { html -> + BadgeParser.parseFromData( + cookie = fbCookie.webCookie, + text = html + )?.data + } + .distinctUntilChanged() + .flowOn(Dispatchers.IO) + .onEach { data -> + L.v { "Badges $data" } tabsForEachView { _, view -> when (view.iicon) { FbItem.FEED.icon -> view.badgeText = data.feed @@ -105,6 +121,7 @@ class MainActivity : BaseMainActivity() { } } } - } + .flowOn(Dispatchers.Main) + .launchIn(this@MainActivity) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt index 756b1f3d..83110417 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt @@ -18,13 +18,18 @@ package com.pitchedapps.frost.contracts import com.mikepenz.iconics.typeface.IIcon import com.pitchedapps.frost.fragments.BaseFragment +import com.pitchedapps.frost.web.FrostEmitter import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.flow.SharedFlow @UseExperimental(ExperimentalCoroutinesApi::class) interface MainActivityContract : MainFabContract { val fragmentChannel: BroadcastChannel - val headerBadgeChannel: BroadcastChannel + + val headerFlow: SharedFlow + val headerEmit: FrostEmitter + fun setTitle(res: Int) fun setTitle(text: CharSequence) 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 b76f6e39..75d1ffe4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -44,9 +44,7 @@ import com.pitchedapps.frost.web.FrostEmitter import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.BroadcastChannel 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 diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt index f43f3b81..4d92e8c2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -32,7 +32,6 @@ import com.pitchedapps.frost.utils.isIndependent import com.pitchedapps.frost.utils.launchImageActivity import com.pitchedapps.frost.utils.showWebContextMenu import com.pitchedapps.frost.views.FrostWebView -import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.launch import javax.inject.Inject @@ -50,7 +49,7 @@ class FrostJSI @Inject internal constructor( private val mainActivity: MainActivity? = activity as? MainActivity private val webActivity: WebOverlayActivityBase? = activity as? WebOverlayActivityBase - private val header: SendChannel? = mainActivity?.headerBadgeChannel + private val headerEmit: FrostEmitter? = mainActivity?.headerEmit private val cookies: List = activity.cookies() /** @@ -159,7 +158,7 @@ class FrostJSI @Inject internal constructor( @JavascriptInterface fun handleHeader(html: String?) { html ?: return - header?.offer(html) + headerEmit?.invoke(html) } @JavascriptInterface -- cgit v1.2.3 From 0c97abd838ee834ef17d1b2e746fa26ea9663bd5 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 23 Nov 2021 11:50:06 -0800 Subject: Create extension for converting emitters --- .../kotlin/com/pitchedapps/frost/activities/MainActivity.kt | 8 ++++---- .../kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt | 1 - .../kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt | 1 - .../kotlin/com/pitchedapps/frost/views/FrostContentView.kt | 10 ++++------ app/src/main/kotlin/com/pitchedapps/frost/web/FrostWeb.kt | 3 +++ 5 files changed, 11 insertions(+), 12 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt index 97067b21..2e44e5f9 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -24,9 +24,9 @@ import com.pitchedapps.frost.facebook.parsers.BadgeParser import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.views.BadgedIcon import com.pitchedapps.frost.web.FrostEmitter +import com.pitchedapps.frost.web.asFrostEmitter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -41,14 +41,14 @@ import kotlinx.coroutines.flow.onEach @UseExperimental(ExperimentalCoroutinesApi::class) class MainActivity : BaseMainActivity() { - + private val fragmentMutableFlow = MutableSharedFlow(extraBufferCapacity = 10, onBufferOverflow = BufferOverflow.DROP_OLDEST) override val fragmentFlow: SharedFlow = fragmentMutableFlow.asSharedFlow() - override val fragmentEmit: FrostEmitter = FrostEmitter { fragmentMutableFlow.tryEmit(it) } + override val fragmentEmit: FrostEmitter = fragmentMutableFlow.asFrostEmitter() private val headerMutableFlow = MutableStateFlow("") override val headerFlow: SharedFlow = headerMutableFlow.asSharedFlow() - override val headerEmit: FrostEmitter = FrostEmitter { headerMutableFlow.tryEmit(it) } + override val headerEmit: FrostEmitter = headerMutableFlow.asFrostEmitter() override fun onNestedCreate(savedInstanceState: Bundle?) { with(contentBinding) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt index 3c5d7412..721282aa 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt @@ -20,7 +20,6 @@ import com.mikepenz.iconics.typeface.IIcon import com.pitchedapps.frost.fragments.BaseFragment import com.pitchedapps.frost.web.FrostEmitter import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.flow.SharedFlow @UseExperimental(ExperimentalCoroutinesApi::class) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt index 5c97de65..e10fd141 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt @@ -47,7 +47,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.isActive 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 75d1ffe4..f9d04ad1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -41,6 +41,7 @@ import com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.prefs.Prefs import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.web.FrostEmitter +import com.pitchedapps.frost.web.asFrostEmitter import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -139,22 +140,19 @@ abstract class FrostContentViewBase( override val refreshFlow: SharedFlow = refreshMutableFlow.asSharedFlow() - override val refreshEmit: FrostEmitter = - FrostEmitter { refreshMutableFlow.tryEmit(it) } + override val refreshEmit: FrostEmitter = refreshMutableFlow.asFrostEmitter() private val progressMutableFlow = MutableStateFlow(0) override val progressFlow: SharedFlow = progressMutableFlow.asSharedFlow() - override val progressEmit: FrostEmitter = - FrostEmitter { progressMutableFlow.tryEmit(it) } + override val progressEmit: FrostEmitter = progressMutableFlow.asFrostEmitter() private val titleMutableFlow = MutableStateFlow("") override val titleFlow: SharedFlow = titleMutableFlow.asSharedFlow() - override val titleEmit: FrostEmitter = - FrostEmitter { titleMutableFlow.tryEmit(it) } + override val titleEmit: FrostEmitter = titleMutableFlow.asFrostEmitter() override lateinit var scope: CoroutineScope diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWeb.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWeb.kt index 30845a79..ba05a2c4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWeb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWeb.kt @@ -25,6 +25,7 @@ import dagger.hilt.DefineComponent import dagger.hilt.EntryPoint import dagger.hilt.InstallIn import dagger.hilt.android.components.ViewComponent +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import javax.inject.Qualifier import javax.inject.Scope @@ -62,6 +63,8 @@ interface FrostWebEntryPoint { fun interface FrostEmitter : (T) -> Unit +fun MutableSharedFlow.asFrostEmitter(): FrostEmitter = FrostEmitter { tryEmit(it) } + @Module @InstallIn(FrostWebComponent::class) object FrostWebFlowModule { -- cgit v1.2.3 From e8d9dca1ede1295f67e27faf731a5caa1bd2810a Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 23 Nov 2021 11:58:30 -0800 Subject: Convert login refresh channel to flow and remove some outdated tests --- .../pitchedapps/frost/activities/LoginActivity.kt | 34 ++++++-- .../com/pitchedapps/frost/utils/KotlinUtils.kt | 36 -------- .../pitchedapps/frost/views/FrostContentView.kt | 3 + .../com/pitchedapps/frost/utils/CoroutineTest.kt | 99 ---------------------- 4 files changed, 28 insertions(+), 144 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt index 949f1ddd..a95e931b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt @@ -45,13 +45,20 @@ import com.pitchedapps.frost.utils.frostEvent import com.pitchedapps.frost.utils.frostJsoup import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.utils.logFrostEvent -import com.pitchedapps.frost.utils.uniqueOnly +import com.pitchedapps.frost.web.FrostEmitter import com.pitchedapps.frost.web.LoginWebView +import com.pitchedapps.frost.web.asFrostEmitter import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async -import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext @@ -76,7 +83,15 @@ class LoginActivity : BaseActivity() { private val profile: ImageView by bindView(R.id.profile) private lateinit var profileLoader: RequestManager - private val refreshChannel = Channel(10) + + private val refreshMutableFlow = MutableSharedFlow( + extraBufferCapacity = 10, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + + private val refreshFlow: SharedFlow = refreshMutableFlow.asSharedFlow() + + private val refreshEmit: FrostEmitter = refreshMutableFlow.asFrostEmitter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -87,11 +102,12 @@ class LoginActivity : BaseActivity() { toolbar(toolbar) } profileLoader = GlideApp.with(profile) - launch { - for (refreshing in refreshChannel.uniqueOnly(this)) { - swipeRefresh.isRefreshing = refreshing - } - } + + refreshFlow + .distinctUntilChanged() + .onEach { swipeRefresh.isRefreshing = it } + .launchIn(this) + launch { val cookie = web.loadLogin { refresh(it != 100) }.await() L.d { "Login found" } @@ -107,7 +123,7 @@ class LoginActivity : BaseActivity() { } private fun refresh(refreshing: Boolean) { - refreshChannel.offer(refreshing) + refreshEmit(refreshing) } private suspend fun loadInfo(cookie: CookieEntity): Unit = withMainContext { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt deleted file mode 100644 index f4357c9b..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.pitchedapps.frost.utils - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.channels.ReceiveChannel -import kotlinx.coroutines.channels.produce -import kotlinx.coroutines.isActive - -@UseExperimental(ExperimentalCoroutinesApi::class) -fun ReceiveChannel.uniqueOnly(scope: CoroutineScope): ReceiveChannel = scope.produce { - var previous: T? = null - for (current in this@uniqueOnly) { - if (!scope.isActive) { - cancel() - } else if (previous != current) { - previous = current - send(current) - } - } -} 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 f9d04ad1..d2083816 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -132,6 +132,9 @@ abstract class FrostContentViewBase( /** * While this can be conflated, there exist situations where we wish to watch refresh cycles. * Here, we'd need to make sure we don't skip events + * + * TODO ensure there is only one flow provider is this is still separated in login + * Use case for shared flow is to avoid emitting before subscribing; buffer can probably be size 1 */ private val refreshMutableFlow = MutableSharedFlow( extraBufferCapacity = 10, diff --git a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt index 2744d0d8..43bd1563 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt @@ -26,7 +26,6 @@ import kotlinx.coroutines.async import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow @@ -41,7 +40,6 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import java.util.concurrent.Executors import kotlin.coroutines.EmptyCoroutineContext -import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -162,103 +160,6 @@ class CoroutineTest { } } - /** - * Not a true throttle, but for things like fetching header badges, we want to avoid simultaneous fetches. - * As a result, I want to test that the usage of offer along with a conflated channel will work as I expect. - * Events should be consumed when there is no pending consumer on previous elements. - */ - @Test - @Ignore("Move to flow") - fun throttledChannel() { - val channel = Channel(Channel.CONFLATED) - runBlocking { - val deferred = async { - listen(channel) { - // Throttle consumer - delay(10) - return@listen false - } - } - (0..100).forEach { - channel.offer(it) - delay(1) - } - channel.close() - val received = deferred.await() - assertTrue( - received.size < 20, - "Received data should be throttled; expected that around 1/10th of all events are consumed, but received ${received.size}" - ) - println(received) - } - } - - @Test - fun uniqueOnly() { - val channel = BroadcastChannel(100) - runBlocking { - val fullReceiver = channel.openSubscription() - val uniqueReceiver = channel.openSubscription().uniqueOnly(this) - - val fullDeferred = async { listen(fullReceiver) } - val uniqueDeferred = async { listen(uniqueReceiver) } - - listOf(0, 1, 2, 3, 3, 3, 4, 3, 5, 5, 1).forEach { - channel.offer(it) - } - channel.close() - - val fullData = fullDeferred.await() - val uniqueData = uniqueDeferred.await() - - assertEquals( - listOf(0, 1, 2, 3, 3, 3, 4, 3, 5, 5, 1), - fullData, - "Full receiver should get all channel events" - ) - assertEquals( - listOf(0, 1, 2, 3, 4, 3, 5, 1), - uniqueData, - "Unique receiver should not have two consecutive events that are equal" - ) - } - } - - /** - * When using [uniqueOnly] for channels with limited capacity, - * the duplicates should not count towards the actual capacity - */ - @Ignore("Not yet working as unique only buffered removes the capacity limitation of the channel") - @Test - fun uniqueOnlyBuffer() { - val channel = Channel(3) - runBlocking { - - val deferred = async { - listen(channel.uniqueOnly(GlobalScope)) { - // Throttle consumer - delay(50) - return@listen false - } - } - - listOf(0, 1, 1, 1, 1, 1, 2, 2, 2).forEach { - delay(10) - channel.offer(it) - } - - channel.close() - - val data = deferred.await() - - assertEquals( - listOf(0, 1, 2), - data, - "Unique receiver should not have two consecutive events that are equal" - ) - } - } - class TestException(msg: String) : RuntimeException(msg) @Test -- cgit v1.2.3 From 3599803b05188b8f9f4758b43fe120578fa8cf06 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 23 Nov 2021 12:10:23 -0800 Subject: Remove all experimental annotations --- app/build.gradle | 1 - .../main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt | 3 --- .../main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt | 2 -- .../kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt | 2 -- app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt | 2 -- app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt | 2 -- app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt | 2 -- 7 files changed, 14 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/build.gradle b/app/build.gradle index 7e064152..f13c71e1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -135,7 +135,6 @@ android { } def compilerArgs = [ - "-Xuse-experimental=kotlin.Experimental", // "-XXLanguage:+InlineClasses", "-Xopt-in=kotlin.RequiresOptIn", ] diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index ae8d442f..8dbf9d5c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -65,7 +65,6 @@ import com.pitchedapps.frost.views.FrostVideoViewer import com.pitchedapps.frost.views.FrostWebView import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -87,7 +86,6 @@ import javax.inject.Inject * Used by notifications. Unlike the other overlays, this runs as a singleInstance * Going back will bring you back to the previous app */ -@UseExperimental(ExperimentalCoroutinesApi::class) class FrostWebActivity : WebOverlayActivityBase() { override fun onCreate(savedInstanceState: Bundle?) { @@ -151,7 +149,6 @@ class WebOverlayDesktopActivity : WebOverlayActivityBase(USER_AGENT_DESKTOP_CONS */ class WebOverlayActivity : WebOverlayActivityBase() -@UseExperimental(ExperimentalCoroutinesApi::class) @AndroidEntryPoint abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT) : BaseActivity(), diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt index 721282aa..2b7f7b2c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt @@ -19,10 +19,8 @@ package com.pitchedapps.frost.contracts import com.mikepenz.iconics.typeface.IIcon import com.pitchedapps.frost.fragments.BaseFragment import com.pitchedapps.frost.web.FrostEmitter -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.SharedFlow -@UseExperimental(ExperimentalCoroutinesApi::class) interface MainActivityContract : MainFabContract { val fragmentFlow: SharedFlow val fragmentEmit: FrostEmitter 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 d32cb873..7f91f901 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FrostContentContract.kt @@ -20,7 +20,6 @@ import android.view.View import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.web.FrostEmitter import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.SharedFlow /** @@ -47,7 +46,6 @@ interface FrostContentContainer : CoroutineScope { * Contract for components shared among * all content providers */ -@UseExperimental(ExperimentalCoroutinesApi::class) interface FrostContentParent : DynamicUiContract { val scope: CoroutineScope diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt index e10fd141..a3303638 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt @@ -44,7 +44,6 @@ import com.pitchedapps.frost.utils.REQUEST_TEXT_ZOOM import com.pitchedapps.frost.utils.frostEvent import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.flow.launchIn @@ -59,7 +58,6 @@ import kotlin.coroutines.CoroutineContext * All fragments pertaining to the main view * Must be attached to activities implementing [MainActivityContract] */ -@UseExperimental(ExperimentalCoroutinesApi::class) @AndroidEntryPoint abstract class BaseFragment : Fragment(), 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 d2083816..c77ae590 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -44,7 +44,6 @@ import com.pitchedapps.frost.web.FrostEmitter import com.pitchedapps.frost.web.asFrostEmitter import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.flow.MutableSharedFlow @@ -96,7 +95,6 @@ abstract class FrostContentView @JvmOverloads constructor( /** * Subsection of [FrostContentView] that is [AndroidEntryPoint] friendly (no generics) */ -@UseExperimental(ExperimentalCoroutinesApi::class) @AndroidEntryPoint abstract class FrostContentViewBase( context: Context, 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 9e21ede8..04ee7f3c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt @@ -29,7 +29,6 @@ import com.pitchedapps.frost.contracts.FrostContentParent import com.pitchedapps.frost.fragments.RecyclerContentContract import com.pitchedapps.frost.prefs.Prefs import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch import javax.inject.Inject @@ -37,7 +36,6 @@ import javax.inject.Inject * Created by Allan Wang on 2017-05-29. * */ -@UseExperimental(ExperimentalCoroutinesApi::class) @AndroidEntryPoint class FrostRecyclerView @JvmOverloads constructor( context: Context, -- cgit v1.2.3 From db646e23ce0047461e68be1af7021ce6171ac850 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 23 Nov 2021 18:19:24 -0800 Subject: Remove channels from frost debugger --- .../main/kotlin/com/pitchedapps/frost/settings/Debug.kt | 14 +++++--------- .../kotlin/com/pitchedapps/frost/views/FrostContentView.kt | 7 +++++-- app/src/main/play/en-US/whatsnew | 6 ++---- app/src/main/res/xml/frost_changelog.xml | 5 +++++ docs/Changelog.md | 3 +++ 5 files changed, 20 insertions(+), 15 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt index b2bb1d11..6ba2c64d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt @@ -42,7 +42,7 @@ import com.pitchedapps.frost.utils.frostUriFromFile import com.pitchedapps.frost.utils.sendFrostEmail import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job -import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch import java.io.File @@ -71,6 +71,7 @@ fun SettingsActivity.getDebugPrefs(): KPrefAdapterBuilder.() -> Unit = { val parsers = arrayOf(NotifParser, MessageParser, SearchParser) materialDialog { + // noinspection CheckResult listItems(items = parsers.map { string(it.nameRes) }) { dialog, position, _ -> dialog.dismiss() val parser = parsers[position] @@ -133,20 +134,15 @@ fun SettingsActivity.sendDebug(url: String, html: String?) { onDismiss { job.cancel() } } - val progressChannel = Channel(10) + val progressFlow = MutableStateFlow(0) - launchMain { - for (p in progressChannel) { -// md.setProgress(p) - } - } +// progressFlow.onEach { md.setProgress(it) }.launchIn(this) launchMain { val success = downloader.loadAndZip(ZIP_NAME) { - progressChannel.offer(it) + progressFlow.tryEmit(it) } md.dismiss() - progressChannel.close() if (success) { val zipUri = frostUriFromFile( File(downloader.baseDir, "$ZIP_NAME.zip") 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 c77ae590..16c28c02 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -44,8 +44,8 @@ import com.pitchedapps.frost.web.FrostEmitter import com.pitchedapps.frost.web.asFrostEmitter import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BufferOverflow -import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow @@ -58,6 +58,7 @@ import kotlinx.coroutines.flow.runningFold import kotlinx.coroutines.flow.transformWhile import javax.inject.Inject +@ExperimentalCoroutinesApi class FrostContentWeb @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -68,6 +69,7 @@ class FrostContentWeb @JvmOverloads constructor( override val layoutRes: Int = R.layout.view_content_base_web } +@ExperimentalCoroutinesApi class FrostContentRecycler @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -78,6 +80,7 @@ class FrostContentRecycler @JvmOverloads constructor( override val layoutRes: Int = R.layout.view_content_base_recycler } +@ExperimentalCoroutinesApi abstract class FrostContentView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -96,6 +99,7 @@ abstract class FrostContentView @JvmOverloads constructor( * Subsection of [FrostContentView] that is [AndroidEntryPoint] friendly (no generics) */ @AndroidEntryPoint +@ExperimentalCoroutinesApi abstract class FrostContentViewBase( context: Context, attrs: AttributeSet?, @@ -239,7 +243,6 @@ abstract class FrostContentViewBase( } private var transitionStart: Long = -1 - private var refreshReceiver: ReceiveChannel? = null /** * Hook onto the refresh observable for one cycle diff --git a/app/src/main/play/en-US/whatsnew b/app/src/main/play/en-US/whatsnew index 7ca9e6af..eb46a757 100644 --- a/app/src/main/play/en-US/whatsnew +++ b/app/src/main/play/en-US/whatsnew @@ -1,5 +1,3 @@ -v3.1.2 +v3.2.0 -* Fix loading full size images -* Fix menu tab -* Always load messenger internally \ No newline at end of file +* Improve loading process \ No newline at end of file diff --git a/app/src/main/res/xml/frost_changelog.xml b/app/src/main/res/xml/frost_changelog.xml index b3227a6e..2bdfef82 100644 --- a/app/src/main/res/xml/frost_changelog.xml +++ b/app/src/main/res/xml/frost_changelog.xml @@ -6,6 +6,11 @@ --> + + + + + diff --git a/docs/Changelog.md b/docs/Changelog.md index b8dace0a..0e9c638e 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,5 +1,8 @@ # Changelog +## v3.2.0 +* Improve loading process + ## v3.1.2 * Fix loading full size images * Fix menu tab -- cgit v1.2.3