For #12201 - forced callbacks to be on the main thread (#12543)

releases/v79.1.0
Mihai Branescu 4 years ago committed by GitHub
parent 4fe1fb8f4a
commit 9373e7fcf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,10 @@ import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.component_sync_tabs.view.* import kotlinx.android.synthetic.main.component_sync_tabs.view.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import mozilla.components.browser.storage.sync.SyncedDeviceTabs import mozilla.components.browser.storage.sync.SyncedDeviceTabs
import mozilla.components.feature.syncedtabs.view.SyncedTabsView import mozilla.components.feature.syncedtabs.view.SyncedTabsView
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -23,6 +27,7 @@ class SyncedTabsLayout @JvmOverloads constructor(
override var listener: SyncedTabsView.Listener? = null override var listener: SyncedTabsView.Listener? = null
private val adapter = SyncedTabsAdapter { listener?.onTabClicked(it) } private val adapter = SyncedTabsAdapter { listener?.onTabClicked(it) }
private val coroutineScope = CoroutineScope(Dispatchers.Main)
init { init {
inflate(getContext(), R.layout.component_sync_tabs, this) inflate(getContext(), R.layout.component_sync_tabs, this)
@ -34,42 +39,46 @@ class SyncedTabsLayout @JvmOverloads constructor(
} }
override fun onError(error: SyncedTabsView.ErrorType) { override fun onError(error: SyncedTabsView.ErrorType) {
// We may still be displaying a "loading" spinner, hide it. coroutineScope.launch {
stopLoading() // We may still be displaying a "loading" spinner, hide it.
stopLoading()
val stringResId = when (error) {
SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE -> R.string.synced_tabs_connect_another_device val stringResId = when (error) {
SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE -> R.string.synced_tabs_enable_tab_syncing SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE -> R.string.synced_tabs_connect_another_device
SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> R.string.synced_tabs_connect_to_sync_account SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE -> R.string.synced_tabs_enable_tab_syncing
SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> R.string.synced_tabs_reauth SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> R.string.synced_tabs_connect_to_sync_account
SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> R.string.synced_tabs_no_tabs SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> R.string.synced_tabs_reauth
} SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> R.string.synced_tabs_no_tabs
}
sync_tabs_status.text = context.getText(stringResId) sync_tabs_status.text = context.getText(stringResId)
synced_tabs_list.visibility = View.GONE synced_tabs_list.visibility = View.GONE
sync_tabs_status.visibility = View.VISIBLE sync_tabs_status.visibility = View.VISIBLE
synced_tabs_pull_to_refresh.isEnabled = pullToRefreshEnableState(error) synced_tabs_pull_to_refresh.isEnabled = pullToRefreshEnableState(error)
}
} }
override fun displaySyncedTabs(syncedTabs: List<SyncedDeviceTabs>) { override fun displaySyncedTabs(syncedTabs: List<SyncedDeviceTabs>) {
synced_tabs_list.visibility = View.VISIBLE coroutineScope.launch {
sync_tabs_status.visibility = View.GONE synced_tabs_list.visibility = View.VISIBLE
sync_tabs_status.visibility = View.GONE
val allDeviceTabs = emptyList<SyncedTabsAdapter.AdapterItem>().toMutableList() val allDeviceTabs = emptyList<SyncedTabsAdapter.AdapterItem>().toMutableList()
syncedTabs.forEach { (device, tabs) -> syncedTabs.forEach { (device, tabs) ->
if (tabs.isEmpty()) { if (tabs.isEmpty()) {
return@forEach return@forEach
} }
val deviceTabs = tabs.map { SyncedTabsAdapter.AdapterItem.Tab(it) } val deviceTabs = tabs.map { SyncedTabsAdapter.AdapterItem.Tab(it) }
allDeviceTabs += listOf(SyncedTabsAdapter.AdapterItem.Device(device)) + deviceTabs allDeviceTabs += listOf(SyncedTabsAdapter.AdapterItem.Device(device)) + deviceTabs
} }
adapter.submitList(allDeviceTabs) adapter.submitList(allDeviceTabs)
}
} }
override fun startLoading() { override fun startLoading() {
@ -83,6 +92,11 @@ class SyncedTabsLayout @JvmOverloads constructor(
synced_tabs_pull_to_refresh.isRefreshing = false synced_tabs_pull_to_refresh.isRefreshing = false
} }
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
coroutineScope.cancel()
}
companion object { companion object {
internal fun pullToRefreshEnableState(error: SyncedTabsView.ErrorType) = when (error) { internal fun pullToRefreshEnableState(error: SyncedTabsView.ErrorType) = when (error) {
// Disable "pull-to-refresh" when we clearly can't sync tabs, and user needs to take an // Disable "pull-to-refresh" when we clearly can't sync tabs, and user needs to take an

Loading…
Cancel
Save