2020-05-26 22:25:52 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.sync
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.widget.FrameLayout
|
2020-06-16 09:42:16 +02:00
|
|
|
import androidx.fragment.app.findFragment
|
2020-06-25 09:49:35 +02:00
|
|
|
import androidx.navigation.NavController
|
2020-06-16 09:42:16 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
2020-05-26 22:25:52 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import kotlinx.android.synthetic.main.component_sync_tabs.view.*
|
2020-07-16 20:39:31 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.cancel
|
2020-07-23 20:16:11 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2020-05-26 22:25:52 +02:00
|
|
|
import mozilla.components.browser.storage.sync.SyncedDeviceTabs
|
2020-08-18 06:09:27 +02:00
|
|
|
import mozilla.components.browser.storage.sync.Tab
|
2020-05-26 22:25:52 +02:00
|
|
|
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
|
2020-08-18 07:45:48 +02:00
|
|
|
import org.mozilla.fenix.FeatureFlags
|
2020-05-26 22:25:52 +02:00
|
|
|
import org.mozilla.fenix.R
|
2020-08-18 06:09:27 +02:00
|
|
|
import org.mozilla.fenix.sync.ext.toAdapterItem
|
|
|
|
import org.mozilla.fenix.sync.ext.toStringRes
|
2020-06-25 09:49:35 +02:00
|
|
|
import java.lang.IllegalStateException
|
2020-05-26 22:25:52 +02:00
|
|
|
|
|
|
|
class SyncedTabsLayout @JvmOverloads constructor(
|
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null,
|
|
|
|
defStyleAttr: Int = 0
|
|
|
|
) : FrameLayout(context, attrs, defStyleAttr), SyncedTabsView {
|
|
|
|
|
|
|
|
override var listener: SyncedTabsView.Listener? = null
|
|
|
|
|
2020-08-18 06:09:27 +02:00
|
|
|
private val adapter = SyncedTabsAdapter(ListenerDelegate { listener })
|
2020-07-16 20:39:31 +02:00
|
|
|
private val coroutineScope = CoroutineScope(Dispatchers.Main)
|
2020-05-26 22:25:52 +02:00
|
|
|
|
|
|
|
init {
|
|
|
|
inflate(getContext(), R.layout.component_sync_tabs, this)
|
|
|
|
|
|
|
|
synced_tabs_list.layoutManager = LinearLayoutManager(context)
|
|
|
|
synced_tabs_list.adapter = adapter
|
|
|
|
|
|
|
|
synced_tabs_pull_to_refresh.setOnRefreshListener { listener?.onRefresh() }
|
2020-08-18 07:45:48 +02:00
|
|
|
|
|
|
|
// Sanity-check: Remove this class when the feature flag is always enabled.
|
|
|
|
FeatureFlags.syncedTabsInTabsTray
|
2020-05-26 22:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onError(error: SyncedTabsView.ErrorType) {
|
2020-07-16 20:39:31 +02:00
|
|
|
coroutineScope.launch {
|
|
|
|
// We may still be displaying a "loading" spinner, hide it.
|
|
|
|
stopLoading()
|
|
|
|
|
2020-06-25 09:49:35 +02:00
|
|
|
val navController: NavController? = try {
|
|
|
|
findFragment<SyncedTabsFragment>().findNavController()
|
|
|
|
} catch (exception: IllegalStateException) {
|
|
|
|
null
|
|
|
|
}
|
2020-05-26 22:25:52 +02:00
|
|
|
|
2020-08-18 06:09:27 +02:00
|
|
|
val descriptionResId = error.toStringRes()
|
|
|
|
val errorItem = error.toAdapterItem(descriptionResId, navController)
|
2020-06-25 09:49:35 +02:00
|
|
|
|
|
|
|
val errorList: List<SyncedTabsAdapter.AdapterItem> = listOf(errorItem)
|
|
|
|
adapter.submitList(errorList)
|
2020-07-09 23:49:56 +02:00
|
|
|
|
2020-07-16 20:39:31 +02:00
|
|
|
synced_tabs_pull_to_refresh.isEnabled = pullToRefreshEnableState(error)
|
|
|
|
}
|
2020-05-26 22:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun displaySyncedTabs(syncedTabs: List<SyncedDeviceTabs>) {
|
2020-07-16 20:39:31 +02:00
|
|
|
coroutineScope.launch {
|
2020-07-23 20:16:11 +02:00
|
|
|
adapter.updateData(syncedTabs)
|
2020-07-16 20:39:31 +02:00
|
|
|
}
|
2020-05-26 22:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun startLoading() {
|
|
|
|
synced_tabs_pull_to_refresh.isRefreshing = true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun stopLoading() {
|
|
|
|
synced_tabs_pull_to_refresh.isRefreshing = false
|
|
|
|
}
|
2020-07-09 23:49:56 +02:00
|
|
|
|
2020-07-16 20:39:31 +02:00
|
|
|
override fun onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow()
|
|
|
|
coroutineScope.cancel()
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:49:56 +02:00
|
|
|
companion object {
|
2020-06-25 09:49:35 +02:00
|
|
|
|
2020-07-31 19:12:53 +02:00
|
|
|
internal fun pullToRefreshEnableState(error: SyncedTabsView.ErrorType) = when (error) {
|
2020-07-09 23:49:56 +02:00
|
|
|
// Disable "pull-to-refresh" when we clearly can't sync tabs, and user needs to take an
|
|
|
|
// action within the app.
|
|
|
|
SyncedTabsView.ErrorType.SYNC_UNAVAILABLE,
|
|
|
|
SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> false
|
|
|
|
|
|
|
|
// Enable "pull-to-refresh" when an external event (e.g. connecting a desktop client,
|
|
|
|
// or enabling tabs sync, or connecting to a network) may resolve our problem.
|
|
|
|
SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE,
|
|
|
|
SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE,
|
|
|
|
SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> true
|
|
|
|
}
|
2020-08-18 06:09:27 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 20:16:11 +02:00
|
|
|
|
2020-08-18 06:09:27 +02:00
|
|
|
/**
|
|
|
|
* We have to do this weird daisy-chaining of callbacks because the listener is nullable and
|
|
|
|
* when we get a null reference, we never get a new binding to the non-null listener.
|
|
|
|
*/
|
|
|
|
class ListenerDelegate(
|
|
|
|
private val listener: (() -> SyncedTabsView.Listener?)
|
|
|
|
) : SyncedTabsView.Listener {
|
|
|
|
override fun onRefresh() {
|
|
|
|
listener.invoke()?.onRefresh()
|
|
|
|
}
|
2020-06-25 09:49:35 +02:00
|
|
|
|
2020-08-18 06:09:27 +02:00
|
|
|
override fun onTabClicked(tab: Tab) {
|
|
|
|
listener.invoke()?.onTabClicked(tab)
|
2020-07-09 23:49:56 +02:00
|
|
|
}
|
2020-05-26 22:25:52 +02:00
|
|
|
}
|