[fenix] Issue https://github.com/mozilla-mobile/fenix/issues/14117: Add Synced Tabs as a page in the tabs tray
parent
e4614fd8a0
commit
9feca460c1
@ -0,0 +1,30 @@
|
||||
/* 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.tabstray.syncedtabs
|
||||
|
||||
import mozilla.components.browser.storage.sync.Tab
|
||||
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
|
||||
import org.mozilla.fenix.BrowserDirection
|
||||
import org.mozilla.fenix.HomeActivity
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.components.metrics.MetricController
|
||||
import org.mozilla.fenix.tabstray.TabsTrayInteractor
|
||||
|
||||
internal class SyncedTabsInteractor(
|
||||
private val metrics: MetricController,
|
||||
private val activity: HomeActivity,
|
||||
private val trayInteractor: TabsTrayInteractor
|
||||
) : SyncedTabsView.Listener {
|
||||
override fun onRefresh() = Unit
|
||||
override fun onTabClicked(tab: Tab) {
|
||||
metrics.track(Event.SyncedTabOpened)
|
||||
activity.openToBrowserAndLoad(
|
||||
searchTermOrURL = tab.active().url,
|
||||
newTab = true,
|
||||
from = BrowserDirection.FromTabTray
|
||||
)
|
||||
trayInteractor.navigateToBrowser()
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/* 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.tabstray.syncedtabs
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.fragment.app.findFragment
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import kotlinx.android.synthetic.main.component_sync_tabs_tray_layout.view.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import mozilla.components.browser.storage.sync.SyncedDeviceTabs
|
||||
import mozilla.components.feature.syncedtabs.SyncedTabsFeature
|
||||
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
|
||||
import mozilla.components.support.base.observer.Observable
|
||||
import mozilla.components.support.base.observer.ObserverRegistry
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.sync.SyncedTabsAdapter
|
||||
import org.mozilla.fenix.sync.ext.toAdapterItem
|
||||
import org.mozilla.fenix.sync.ext.toStringRes
|
||||
import org.mozilla.fenix.tabstray.TabsTrayFragment
|
||||
import org.mozilla.fenix.tabstray.TrayItem
|
||||
import org.mozilla.fenix.utils.view.LifecycleViewProvider
|
||||
|
||||
class SyncedTabsTrayLayout @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : ConstraintLayout(context, attrs, defStyleAttr), SyncedTabsView, TrayItem,
|
||||
Observable<SyncedTabsView.Listener> by ObserverRegistry() {
|
||||
|
||||
private val lifecycleProvider = LifecycleViewProvider(this)
|
||||
private val coroutineScope = CoroutineScope(Dispatchers.Main)
|
||||
private val syncedTabsFeature by lazy {
|
||||
SyncedTabsFeature(
|
||||
context = context,
|
||||
storage = context.components.backgroundServices.syncedTabsStorage,
|
||||
accountManager = context.components.backgroundServices.accountManager,
|
||||
view = this,
|
||||
lifecycleOwner = lifecycleProvider,
|
||||
onTabClicked = {
|
||||
// We can ignore this callback here because we're not connecting the adapter
|
||||
// back to the feature. This works fine in other features, but passing the listener
|
||||
// to other components in this case is annoying.
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
override var listener: SyncedTabsView.Listener? = null
|
||||
|
||||
override fun displaySyncedTabs(syncedTabs: List<SyncedDeviceTabs>) {
|
||||
coroutineScope.launch {
|
||||
(synced_tabs_list.adapter as SyncedTabsAdapter).updateData(syncedTabs)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(error: SyncedTabsView.ErrorType) {
|
||||
coroutineScope.launch {
|
||||
// We may still be displaying a "loading" spinner, hide it.
|
||||
stopLoading()
|
||||
|
||||
val navController: NavController? = try {
|
||||
findFragment<TabsTrayFragment>().findNavController()
|
||||
} catch (exception: IllegalStateException) {
|
||||
null
|
||||
}
|
||||
|
||||
val descriptionResId = error.toStringRes()
|
||||
val errorItem = error.toAdapterItem(descriptionResId, navController)
|
||||
|
||||
val errorList: List<SyncedTabsAdapter.AdapterItem> = listOf(errorItem)
|
||||
(synced_tabs_list.adapter as SyncedTabsAdapter).submitList(errorList)
|
||||
}
|
||||
}
|
||||
|
||||
override fun startLoading() {
|
||||
// TODO implement with https://github.com/mozilla-mobile/fenix/issues/18515
|
||||
// start animating FAB
|
||||
// interactor.syncingTabs(loading = true)
|
||||
}
|
||||
|
||||
override fun stopLoading() {
|
||||
// TODO implement with https://github.com/mozilla-mobile/fenix/issues/18515
|
||||
// stop animating FAB
|
||||
// interactor.syncingTabs(loading = false)
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
|
||||
syncedTabsFeature.start()
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
|
||||
syncedTabsFeature.stop()
|
||||
coroutineScope.cancel()
|
||||
}
|
||||
|
||||
fun onRefresh() {
|
||||
// TODO implement with https://github.com/mozilla-mobile/fenix/issues/18515
|
||||
// syncedTabsFeature.interactor.onRefresh()
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/* 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.tabstray.viewholders
|
||||
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.android.synthetic.main.component_sync_tabs_tray_layout.*
|
||||
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
class SyncedTabViewHolder(
|
||||
containerView: View,
|
||||
private val listener: SyncedTabsView.Listener
|
||||
) : AbstractTrayViewHolder(containerView) {
|
||||
|
||||
override fun bind(
|
||||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
|
||||
layoutManager: RecyclerView.LayoutManager
|
||||
) {
|
||||
synced_tabs_list.layoutManager = layoutManager
|
||||
synced_tabs_list.adapter = adapter
|
||||
synced_tabs_tray_layout.listener = listener
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LAYOUT_ID = R.layout.component_sync_tabs_tray_layout
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsTrayLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/synced_tabs_tray_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<ProgressBar
|
||||
android:id="@+id/sync_tabs_progress_bar"
|
||||
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
|
||||
android:indeterminate="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="8dp"
|
||||
android:translationY="-3dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/synced_tabs_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/sync_tabs_list_item"/>
|
||||
|
||||
</org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsTrayLayout>
|
Loading…
Reference in New Issue