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.annotation.StringRes
|
|
|
|
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
|
|
|
|
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
|
|
|
|
import org.mozilla.fenix.R
|
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
|
|
|
|
|
|
|
|
private val adapter = SyncedTabsAdapter { listener?.onTabClicked(it) }
|
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() }
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-25 09:49:35 +02:00
|
|
|
val descriptionResId = stringResourceForError(error)
|
|
|
|
val errorItem = getErrorItem(navController, error, descriptionResId)
|
|
|
|
|
|
|
|
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-07-23 20:16:11 +02:00
|
|
|
|
2020-07-31 19:12:53 +02:00
|
|
|
internal fun stringResourceForError(error: SyncedTabsView.ErrorType) = when (error) {
|
2020-07-23 20:16:11 +02:00
|
|
|
SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE -> R.string.synced_tabs_connect_another_device
|
|
|
|
SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE -> R.string.synced_tabs_enable_tab_syncing
|
2020-06-16 10:41:15 +02:00
|
|
|
SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> R.string.synced_tabs_sign_in_message
|
2020-07-23 20:16:11 +02:00
|
|
|
SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION -> R.string.synced_tabs_reauth
|
|
|
|
SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> R.string.synced_tabs_no_tabs
|
|
|
|
}
|
2020-06-25 09:49:35 +02:00
|
|
|
|
2020-07-31 19:12:53 +02:00
|
|
|
internal fun getErrorItem(
|
2020-06-25 09:49:35 +02:00
|
|
|
navController: NavController?,
|
|
|
|
error: SyncedTabsView.ErrorType,
|
|
|
|
@StringRes stringResId: Int
|
|
|
|
): SyncedTabsAdapter.AdapterItem = when (error) {
|
|
|
|
SyncedTabsView.ErrorType.MULTIPLE_DEVICES_UNAVAILABLE,
|
|
|
|
SyncedTabsView.ErrorType.SYNC_ENGINE_UNAVAILABLE,
|
|
|
|
SyncedTabsView.ErrorType.SYNC_NEEDS_REAUTHENTICATION,
|
|
|
|
SyncedTabsView.ErrorType.NO_TABS_AVAILABLE -> SyncedTabsAdapter.AdapterItem
|
|
|
|
.Error(descriptionResId = stringResId)
|
|
|
|
SyncedTabsView.ErrorType.SYNC_UNAVAILABLE -> SyncedTabsAdapter.AdapterItem
|
|
|
|
.Error(descriptionResId = stringResId, navController = navController)
|
|
|
|
}
|
2020-07-09 23:49:56 +02:00
|
|
|
}
|
2020-05-26 22:25:52 +02:00
|
|
|
}
|