1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-10 04:52:38 +01:00

Fix/open broadcast (#1345)

* Convert jsi related channels to broadcasts

* Close channel in debug activity
This commit is contained in:
Allan Wang 2019-02-06 12:12:10 -05:00 committed by GitHub
parent a75555f346
commit 91b7d53fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 59 deletions

View File

@ -489,7 +489,6 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract,
controlWebview?.destroy()
super.onDestroy()
fragmentChannel.close()
headerBadgeChannel.close()
}
override fun collapseAppBar() {

View File

@ -21,6 +21,7 @@ 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.kotlin.subscribeDuringJob
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.views.BadgedIcon
import kotlinx.coroutines.Dispatchers
@ -34,7 +35,7 @@ import org.jsoup.Jsoup
class MainActivity : BaseMainActivity() {
override val fragmentChannel = BroadcastChannel<Int>(10)
override val headerBadgeChannel = Channel<String>(Channel.CONFLATED)
override val headerBadgeChannel = BroadcastChannel<String>(Channel.CONFLATED)
var lastPosition = -1
override fun onNestedCreate(savedInstanceState: Bundle?) {
@ -86,35 +87,34 @@ class MainActivity : BaseMainActivity() {
(tab.customView as BadgedIcon).badgeText = null
}
})
launch(Dispatchers.IO) {
for (html in headerBadgeChannel) {
try {
val doc = Jsoup.parse(html)
if (doc.select("[data-sigil=count]").isEmpty())
continue // Header doesn't exist
val (feed, requests, messages, notifications) = listOf(
"feed",
"requests",
"messages",
"notifications"
)
.map { "[data-sigil*=$it] [data-sigil=count]" }
.map { doc.select(it) }
.map { e -> e?.getOrNull(0)?.ownText() }
L.v { "Badges $feed $requests $messages $notifications" }
withMainContext {
tabsForEachView { _, view ->
when (view.iicon) {
FbItem.FEED.icon -> view.badgeText = feed
FbItem.FRIENDS.icon -> view.badgeText = requests
FbItem.MESSAGES.icon -> view.badgeText = messages
FbItem.NOTIFICATIONS.icon -> view.badgeText = notifications
}
headerBadgeChannel.subscribeDuringJob(this, Dispatchers.IO) {
html->
try {
val doc = Jsoup.parse(html)
if (doc.select("[data-sigil=count]").isEmpty())
return@subscribeDuringJob // Header doesn't exist
val (feed, requests, messages, notifications) = listOf(
"feed",
"requests",
"messages",
"notifications"
)
.map { "[data-sigil*=$it] [data-sigil=count]" }
.map { doc.select(it) }
.map { e -> e?.getOrNull(0)?.ownText() }
L.v { "Badges $feed $requests $messages $notifications" }
withMainContext {
tabsForEachView { _, view ->
when (view.iicon) {
FbItem.FEED.icon -> view.badgeText = feed
FbItem.FRIENDS.icon -> view.badgeText = requests
FbItem.MESSAGES.icon -> view.badgeText = messages
FbItem.NOTIFICATIONS.icon -> view.badgeText = notifications
}
}
} catch (e: Exception) {
L.e(e) { "Header badge error" }
}
} catch (e: Exception) {
L.e(e) { "Header badge error" }
}
}
adapter.pages.forEach {

View File

@ -30,6 +30,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
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
@ -59,6 +60,7 @@ import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.kotlin.subscribeDuringJob
import com.pitchedapps.frost.services.FrostRunnable
import com.pitchedapps.frost.utils.ARG_URL
import com.pitchedapps.frost.utils.ARG_USER_ID
@ -204,12 +206,8 @@ open class WebOverlayActivityBase(private val forceBasicAgent: Boolean) : BaseAc
content.bind(this)
val titleReceiver = content.titleChannel.openSubscription().uniqueOnly(this)
launch {
for (t in titleReceiver) {
toolbar.title = t
}
content.titleChannel.subscribeDuringJob(this, ContextHelper.coroutineContext) {
toolbar.title = it
}
with(web) {

View File

@ -21,7 +21,6 @@ import com.pitchedapps.frost.activities.MainActivity
import com.pitchedapps.frost.fragments.BaseFragment
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
/**
* All the contracts for [MainActivity]
@ -31,7 +30,7 @@ interface ActivityContract : FileChooserActivityContract
@UseExperimental(ExperimentalCoroutinesApi::class)
interface MainActivityContract : ActivityContract, MainFabContract {
val fragmentChannel: BroadcastChannel<Int>
val headerBadgeChannel: Channel<String>
val headerBadgeChannel: BroadcastChannel<String>
fun setTitle(res: Int)
fun setTitle(text: CharSequence)
/**

View File

@ -0,0 +1,23 @@
package com.pitchedapps.frost.kotlin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
@UseExperimental(ExperimentalCoroutinesApi::class)
fun <T> BroadcastChannel<T>.subscribeDuringJob(
scope: CoroutineScope,
context: CoroutineContext,
onReceive: suspend (T) -> Unit
) {
val receiver = openSubscription()
scope.launch(context) {
for (r in receiver) {
onReceive(r)
}
}
scope.coroutineContext[Job]!!.invokeOnCompletion { receiver.cancel() }
}

View File

@ -142,6 +142,7 @@ fun SettingsActivity.sendDebug(url: String, html: String?) {
progressChannel.offer(it)
}
md.dismiss()
progressChannel.close()
if (success) {
val zipUri = frostUriFromFile(
File(downloader.baseDir, "$ZIP_NAME.zip")

View File

@ -23,6 +23,7 @@ import android.view.View
import android.widget.FrameLayout
import android.widget.ProgressBar
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import ca.allanwang.kau.utils.ContextHelper
import ca.allanwang.kau.utils.bindView
import ca.allanwang.kau.utils.circularReveal
import ca.allanwang.kau.utils.fadeIn
@ -38,6 +39,7 @@ import com.pitchedapps.frost.contracts.FrostContentCore
import com.pitchedapps.frost.contracts.FrostContentParent
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.WEB_LOAD_DELAY
import com.pitchedapps.frost.kotlin.subscribeDuringJob
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import kotlinx.coroutines.CoroutineScope
@ -45,7 +47,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.launch
class FrostContentWeb @JvmOverloads constructor(
context: Context,
@ -127,26 +128,18 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
reload(true)
}
}
// Begin subscription in the main thread
val refreshReceiver = refreshChannel.openSubscription()
val progressReceiver = progressChannel.openSubscription()
scope.launchMain {
launch {
for (r in refreshReceiver) {
refresh.isRefreshing = r
refresh.isEnabled = true
}
}
launch {
for (p in progressReceiver) {
progress.invisibleIf(p == 100)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
progress.setProgress(p, true)
else
progress.progress = p
}
}
refreshChannel.subscribeDuringJob(scope, ContextHelper.coroutineContext) { r ->
refresh.isRefreshing = r
refresh.isEnabled = true
}
progressChannel.subscribeDuringJob(scope, ContextHelper.coroutineContext) { p ->
progress.invisibleIf(p == 100)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
progress.setProgress(p, true)
else
progress.progress = p
}
}
@ -170,9 +163,6 @@ abstract class FrostContentView<out T> @JvmOverloads constructor(
}
override fun destroy() {
titleChannel.close()
progressChannel.close()
refreshChannel.close()
core.destroy()
}