From 91b7d53fb37b9939943c16b0bfd7a947d9f2363e Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 6 Feb 2019 12:12:10 -0500 Subject: Fix/open broadcast (#1345) * Convert jsi related channels to broadcasts * Close channel in debug activity --- .../pitchedapps/frost/activities/MainActivity.kt | 54 +++++++++++----------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.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 044a1f37..c53e9055 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -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(10) - override val headerBadgeChannel = Channel(Channel.CONFLATED) + override val headerBadgeChannel = BroadcastChannel(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 { -- cgit v1.2.3