No issue: Sets up observers for tab collection changes (#3248)
parent
ada0c4da28
commit
7368a95d29
|
@ -5,14 +5,12 @@ package org.mozilla.fenix.collections
|
|||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.fragment_create_collection.view.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
@ -20,8 +18,6 @@ import kotlinx.coroutines.Job
|
|||
import kotlinx.coroutines.launch
|
||||
import org.mozilla.fenix.FenixViewModelProvider
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.FenixSnackbar
|
||||
import org.mozilla.fenix.ext.getRootView
|
||||
import org.mozilla.fenix.ext.requireComponents
|
||||
import org.mozilla.fenix.home.sessioncontrol.toSessionBundle
|
||||
import org.mozilla.fenix.mvi.ActionBusFactory
|
||||
|
@ -129,7 +125,6 @@ class CreateCollectionFragment : DialogFragment(), CoroutineScope {
|
|||
)
|
||||
is CollectionCreationAction.BackPressed -> handleBackPress(backPressFrom = it.backPressFrom)
|
||||
is CollectionCreationAction.SaveCollectionName -> {
|
||||
showSavedSnackbar(it.tabs.size)
|
||||
dismiss()
|
||||
|
||||
context?.let { context ->
|
||||
|
@ -140,7 +135,6 @@ class CreateCollectionFragment : DialogFragment(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
is CollectionCreationAction.SelectCollection -> {
|
||||
showSavedSnackbar(it.tabs.size)
|
||||
dismiss()
|
||||
context?.let { context ->
|
||||
val sessionBundle = it.tabs.toList().toSessionBundle(context)
|
||||
|
@ -151,7 +145,6 @@ class CreateCollectionFragment : DialogFragment(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
is CollectionCreationAction.RenameCollection -> {
|
||||
showRenamedSnackbar()
|
||||
dismiss()
|
||||
launch(Dispatchers.IO) {
|
||||
requireComponents.core.tabCollectionStorage.renameCollection(it.collection, it.name)
|
||||
|
@ -161,33 +154,6 @@ class CreateCollectionFragment : DialogFragment(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
|
||||
private fun showRenamedSnackbar() {
|
||||
context?.let { context: Context ->
|
||||
val rootView = context.getRootView()
|
||||
rootView?.let { view: View ->
|
||||
val string = context.getString(R.string.snackbar_collection_renamed)
|
||||
FenixSnackbar.make(view, Snackbar.LENGTH_LONG).setText(string)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showSavedSnackbar(tabSize: Int) {
|
||||
context?.let { context: Context ->
|
||||
val rootView = context.getRootView()
|
||||
rootView?.let { view: View ->
|
||||
val string =
|
||||
if (tabSize > 1) context.getString(R.string.create_collection_tabs_saved) else
|
||||
context.getString(R.string.create_collection_tab_saved)
|
||||
val snackbar = FenixSnackbar.make(view, Snackbar.LENGTH_LONG).setText(string)
|
||||
viewModel.snackbarAnchorView?.let {
|
||||
snackbar.setAnchorView(it)
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleBackPress(backPressFrom: SaveCollectionStep) {
|
||||
when (backPressFrom) {
|
||||
SaveCollectionStep.SelectTabs -> dismiss()
|
||||
|
|
|
@ -12,10 +12,36 @@ import mozilla.components.browser.session.SessionManager
|
|||
import mozilla.components.feature.tab.collections.Tab
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.feature.tab.collections.TabCollectionStorage
|
||||
import mozilla.components.support.base.observer.Observable
|
||||
import mozilla.components.support.base.observer.ObserverRegistry
|
||||
import org.mozilla.fenix.test.Mockable
|
||||
|
||||
@Mockable
|
||||
class TabCollectionStorage(private val context: Context, private val sessionManager: SessionManager) {
|
||||
class TabCollectionStorage(
|
||||
private val context: Context,
|
||||
private val sessionManager: SessionManager,
|
||||
private val delegate: Observable<Observer> = ObserverRegistry()
|
||||
) : Observable<org.mozilla.fenix.components.TabCollectionStorage.Observer> by delegate {
|
||||
|
||||
/**
|
||||
* Interface to be implemented by classes that want to observe the storage
|
||||
*/
|
||||
interface Observer {
|
||||
/**
|
||||
* A collection has been created
|
||||
*/
|
||||
fun onCollectionCreated(title: String, sessions: List<Session>) = Unit
|
||||
|
||||
/**
|
||||
* Tab(s) have been added to collection
|
||||
*/
|
||||
fun onTabsAdded(tabCollection: TabCollection, sessions: List<Session>) = Unit
|
||||
|
||||
/**
|
||||
* Collection has been renamed
|
||||
*/
|
||||
fun onCollectionRenamed(tabCollection: TabCollection, title: String) = Unit
|
||||
}
|
||||
|
||||
var cachedTabCollections = listOf<TabCollection>()
|
||||
|
||||
|
@ -25,10 +51,12 @@ class TabCollectionStorage(private val context: Context, private val sessionMana
|
|||
|
||||
fun createCollection(title: String, sessions: List<Session>) {
|
||||
collectionStorage.createCollection(title, sessions)
|
||||
notifyObservers { onCollectionCreated(title, sessions) }
|
||||
}
|
||||
|
||||
fun addTabsToCollection(tabCollection: TabCollection, sessions: List<Session>) {
|
||||
collectionStorage.addTabsToCollection(tabCollection, sessions)
|
||||
notifyObservers { onTabsAdded(tabCollection, sessions) }
|
||||
}
|
||||
|
||||
fun getTabCollectionsCount(): Int {
|
||||
|
@ -57,5 +85,6 @@ class TabCollectionStorage(private val context: Context, private val sessionMana
|
|||
|
||||
fun renameCollection(tabCollection: TabCollection, title: String) {
|
||||
collectionStorage.renameCollection(tabCollection, title)
|
||||
notifyObservers { onCollectionRenamed(tabCollection, title) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package org.mozilla.fenix.home
|
||||
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.res.Resources
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
|
@ -21,6 +22,7 @@ import androidx.lifecycle.ViewModelProviders
|
|||
import androidx.navigation.fragment.FragmentNavigator
|
||||
import androidx.navigation.fragment.NavHostFragment.findNavController
|
||||
import androidx.transition.TransitionInflater
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.fragment_home.*
|
||||
import kotlinx.android.synthetic.main.fragment_home.view.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
@ -47,6 +49,8 @@ import org.mozilla.fenix.R
|
|||
import org.mozilla.fenix.ThemeManager
|
||||
import org.mozilla.fenix.collections.CreateCollectionViewModel
|
||||
import org.mozilla.fenix.collections.SaveCollectionStep
|
||||
import org.mozilla.fenix.components.FenixSnackbar
|
||||
import org.mozilla.fenix.components.TabCollectionStorage
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.ext.nav
|
||||
|
@ -284,6 +288,7 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver {
|
|||
)
|
||||
)
|
||||
|
||||
requireComponents.core.tabCollectionStorage.register(collectionStorageObserver, this)
|
||||
sessionObserver.onStart()
|
||||
tabCollectionObserver = subscribeToTabCollections()
|
||||
}
|
||||
|
@ -718,6 +723,51 @@ class HomeFragment : Fragment(), CoroutineScope, AccountObserver {
|
|||
emitAccountChanges()
|
||||
}
|
||||
|
||||
private val collectionStorageObserver = object : TabCollectionStorage.Observer {
|
||||
override fun onCollectionCreated(title: String, sessions: List<Session>) {
|
||||
super.onCollectionCreated(title, sessions)
|
||||
showSavedSnackbar(sessions.size)
|
||||
}
|
||||
|
||||
override fun onTabsAdded(
|
||||
tabCollection: mozilla.components.feature.tab.collections.TabCollection,
|
||||
sessions: List<Session>
|
||||
) {
|
||||
super.onTabsAdded(tabCollection, sessions)
|
||||
showSavedSnackbar(sessions.size)
|
||||
}
|
||||
|
||||
override fun onCollectionRenamed(
|
||||
tabCollection: mozilla.components.feature.tab.collections.TabCollection,
|
||||
title: String
|
||||
) {
|
||||
super.onCollectionRenamed(tabCollection, title)
|
||||
showRenamedSnackbar()
|
||||
}
|
||||
}
|
||||
|
||||
private fun showSavedSnackbar(tabSize: Int) {
|
||||
context?.let { context: Context ->
|
||||
view?.let { view: View ->
|
||||
val string =
|
||||
if (tabSize > 1) context.getString(R.string.create_collection_tabs_saved) else
|
||||
context.getString(R.string.create_collection_tab_saved)
|
||||
val snackbar = FenixSnackbar.make(view, Snackbar.LENGTH_LONG).setText(string)
|
||||
snackbar.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showRenamedSnackbar() {
|
||||
context?.let { context: Context ->
|
||||
view?.let { view: View ->
|
||||
val string = context.getString(R.string.snackbar_collection_renamed)
|
||||
FenixSnackbar.make(view, Snackbar.LENGTH_LONG).setText(string)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val SHARED_TRANSITION_MS = 200L
|
||||
private const val TAB_ITEM_TRANSITION_NAME = "tab_item"
|
||||
|
|
Loading…
Reference in New Issue