From 066cdb7146601e66d5893e38af8f3be69bf5885d Mon Sep 17 00:00:00 2001 From: Mugurell Date: Tue, 28 Mar 2023 09:44:03 +0300 Subject: [PATCH] Bug 1824379 - Ensure logins fragments share the same Store We'll cache the same LoginsFragmentStore instance to a new nested navigation graphs and in that allow multiple related screen to use the same data. --- .../mozilla/fenix/components/StoreProvider.kt | 6 +- .../logins/fragment/AddLoginFragment.kt | 11 +- .../logins/fragment/EditLoginFragment.kt | 11 +- .../logins/fragment/LoginDetailFragment.kt | 13 +- .../logins/fragment/SavedLoginsFragment.kt | 13 +- app/src/main/res/navigation/nav_graph.xml | 130 +++++++++--------- detekt-baseline.xml | 2 +- 7 files changed, 96 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/StoreProvider.kt b/app/src/main/java/org/mozilla/fenix/components/StoreProvider.kt index 39b0f3424a..447ad403b9 100644 --- a/app/src/main/java/org/mozilla/fenix/components/StoreProvider.kt +++ b/app/src/main/java/org/mozilla/fenix/components/StoreProvider.kt @@ -4,9 +4,9 @@ package org.mozilla.fenix.components -import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.ViewModelStoreOwner import androidx.lifecycle.get import mozilla.components.lib.state.Store @@ -20,9 +20,9 @@ class StoreProvider>( ) : ViewModel() { companion object { - fun > get(fragment: Fragment, createStore: () -> T): T { + fun > get(owner: ViewModelStoreOwner, createStore: () -> T): T { val factory = StoreProviderFactory(createStore) - val viewModel: StoreProvider = ViewModelProvider(fragment, factory).get() + val viewModel: StoreProvider = ViewModelProvider(owner, factory).get() return viewModel.store } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/AddLoginFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/AddLoginFragment.kt index 8f25326d53..dcb0fb44b1 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/AddLoginFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/AddLoginFragment.kt @@ -63,11 +63,12 @@ class AddLoginFragment : Fragment(R.layout.fragment_add_login), MenuProvider { _binding = FragmentAddLoginBinding.bind(view) - loginsFragmentStore = StoreProvider.get(this) { - LoginsFragmentStore( - createInitialLoginsListState(requireContext().settings()), - ) - } + loginsFragmentStore = + StoreProvider.get(findNavController().getBackStackEntry(R.id.savedLogins)) { + LoginsFragmentStore( + createInitialLoginsListState(requireContext().settings()), + ) + } interactor = AddLoginInteractor( SavedLoginsStorageController( diff --git a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/EditLoginFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/EditLoginFragment.kt index c0b6b44d3b..9d6d8c2d0f 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/EditLoginFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/EditLoginFragment.kt @@ -70,11 +70,12 @@ class EditLoginFragment : Fragment(R.layout.fragment_edit_login), MenuProvider { oldLogin = args.savedLoginItem - loginsFragmentStore = StoreProvider.get(this) { - LoginsFragmentStore( - createInitialLoginsListState(requireContext().settings()), - ) - } + loginsFragmentStore = + StoreProvider.get(findNavController().getBackStackEntry(R.id.savedLogins)) { + LoginsFragmentStore( + createInitialLoginsListState(requireContext().settings()), + ) + } interactor = EditLoginInteractor( SavedLoginsStorageController( diff --git a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/LoginDetailFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/LoginDetailFragment.kt index 6681a00d4c..1fdf814a85 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/LoginDetailFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/LoginDetailFragment.kt @@ -73,11 +73,12 @@ class LoginDetailFragment : SecureFragment(R.layout.fragment_login_detail), Menu ): View? { val view = inflater.inflate(R.layout.fragment_login_detail, container, false) _binding = FragmentLoginDetailBinding.bind(view) - savedLoginsStore = StoreProvider.get(this) { - LoginsFragmentStore( - createInitialLoginsListState(requireContext().settings()), - ) - } + savedLoginsStore = + StoreProvider.get(findNavController().getBackStackEntry(R.id.savedLogins)) { + LoginsFragmentStore( + createInitialLoginsListState(requireContext().settings()), + ) + } loginDetailsBindingDelegate = LoginDetailsBindingDelegate(binding) return view @@ -130,7 +131,7 @@ class LoginDetailFragment : SecureFragment(R.layout.fragment_login_detail), Menu override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requireActivity().onBackPressedDispatcher.addCallback(this) { - val directions = LoginDetailFragmentDirections.actionLoginDetailFragmentToSavedLoginsFragment() + val directions = LoginDetailFragmentDirections.actionLoginDetailFragmentToSavedLogins() findNavController().navigate(directions) } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/SavedLoginsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/SavedLoginsFragment.kt index a3bd98b61c..698ae4e073 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/SavedLoginsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/logins/fragment/SavedLoginsFragment.kt @@ -22,6 +22,7 @@ import androidx.fragment.app.setFragmentResultListener import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController +import androidx.navigation.navGraphViewModels import mozilla.components.concept.menu.MenuController import mozilla.components.concept.menu.Orientation import mozilla.components.lib.state.ext.consumeFrom @@ -35,6 +36,7 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.redirectToReAuth import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar +import org.mozilla.fenix.home.SharedViewModel import org.mozilla.fenix.settings.logins.LoginsAction import org.mozilla.fenix.settings.logins.LoginsFragmentStore import org.mozilla.fenix.settings.logins.LoginsListState @@ -74,11 +76,12 @@ class SavedLoginsFragment : SecureFragment(), MenuProvider { val view = inflater.inflate(R.layout.fragment_saved_logins, container, false) val binding = FragmentSavedLoginsBinding.bind(view) - savedLoginsStore = StoreProvider.get(this) { - LoginsFragmentStore( - createInitialLoginsListState(requireContext().settings()), - ) - } + savedLoginsStore = + StoreProvider.get(findNavController().getBackStackEntry(R.id.savedLogins)) { + LoginsFragmentStore( + createInitialLoginsListState(requireContext().settings()), + ) + } loginsListController = LoginsListController( diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 265fe4e626..5e7e213d43 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -447,7 +447,7 @@ android:label="@string/preferences_passwords_logins_and_passwords"> - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/detekt-baseline.xml b/detekt-baseline.xml index 18e7950aca..bb43073519 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -904,7 +904,7 @@ UndocumentedPublicFunction:StartupTypeTelemetry.kt$StartupTypeTelemetry$fun attachOnHomeActivityOnCreate(lifecycle: Lifecycle) UndocumentedPublicFunction:StorageStatsMetrics.kt$StorageStatsMetrics$// I couldn't get runBlockingTest to work correctly so I moved the functionality under test to // a synchronous function. @VisibleForTesting(otherwise = PRIVATE) @WorkerThread // queryStatsForUid fun reportSync(context: Context) UndocumentedPublicFunction:StorageStatsMetrics.kt$StorageStatsMetrics$@OptIn(DelicateCoroutinesApi::class) // GlobalScope usage fun report(context: Context) - UndocumentedPublicFunction:StoreProvider.kt$StoreProvider.Companion$fun <T : Store<*, *>> get(fragment: Fragment, createStore: () -> T): T + UndocumentedPublicFunction:StoreProvider.kt$StoreProvider.Companion$fun <T : Store<*, *>> get(owner: ViewModelStoreOwner, createStore: () -> T): T UndocumentedPublicFunction:StudiesView.kt$StudiesView$@Suppress("TooGenericExceptionCaught", "ApplySharedPref") fun bind() UndocumentedPublicFunction:SupportUtils.kt$SupportUtils$fun createAuthCustomTabIntent(context: Context, url: String): Intent UndocumentedPublicFunction:SupportUtils.kt$SupportUtils$fun createCustomTabIntent(context: Context, url: String): Intent