@ -2,10 +2,9 @@
* 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.settings .logins
package org.mozilla.fenix.settings
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.NavController
import androidx.preference.Preference
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
@ -16,16 +15,33 @@ import mozilla.components.service.fxa.SyncEngine
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.service.fxa.manager.SyncEnginesStorage
import org.mozilla.fenix.R
import org.mozilla.fenix.settings.logins.fragment.SavedLoginsAuthFragmentDirections
/ * *
* Helper to manage the [ R . string . pref _key _password _sync _logins ] preference .
* A view to help manage the sync preference in the " Logins and passwords " and " Credit cards "
* settings . The provided [ syncPreference ] is used to navigate to the different fragments
* that manages the sync account authentication . A summary status will be also added
* depending on the sync account status .
*
* @param syncPreference The sync [ Preference ] to update and handle navigation .
* @param lifecycleOwner View lifecycle owner used to determine when to cancel UI jobs .
* @param accountManager An instance of [ FxaAccountManager ] .
* @param syncEngine The sync engine that will be used for the sync status lookup .
* @param onSignInToSyncClicked A callback executed when the [ syncPreference ] is clicked with a
* preference status of " Sign in to Sync " .
* @param onSyncStatusClicked A callback executed when the [ syncPreference ] is clicked with a
* preference status of " On " or " Off " .
* @param onReconnectClicked A callback executed when the [ syncPreference ] is clicked with a
* preference status of " Reconnect " .
* /
class SyncLoginsPreferenceView (
private val syncLoginsPreference : Preference ,
@Suppress ( " LongParameterList " )
class SyncPreferenceView (
private val syncPreference : Preference ,
lifecycleOwner : LifecycleOwner ,
accountManager : FxaAccountManager ,
private val navController : NavController
private val syncEngine : SyncEngine ,
private val onSignInToSyncClicked : ( ) -> Unit = { } ,
private val onSyncStatusClicked : ( ) -> Unit = { } ,
private val onReconnectClicked : ( ) -> Unit = { }
) {
init {
@ -33,9 +49,11 @@ class SyncLoginsPreferenceView(
override fun onAuthenticated ( account : OAuthAccount , authType : AuthType ) {
MainScope ( ) . launch { updateSyncPreferenceStatus ( ) }
}
override fun onLoggedOut ( ) {
MainScope ( ) . launch { updateSyncPreferenceNeedsLogin ( ) }
}
override fun onAuthenticationProblems ( ) {
MainScope ( ) . launch { updateSyncPreferenceNeedsReauth ( ) }
}
@ -43,6 +61,7 @@ class SyncLoginsPreferenceView(
val accountExists = accountManager . authenticatedAccount ( ) != null
val needsReauth = accountManager . accountNeedsReauth ( )
when {
needsReauth -> updateSyncPreferenceNeedsReauth ( )
accountExists -> updateSyncPreferenceStatus ( )
@ -51,62 +70,50 @@ class SyncLoginsPreferenceView(
}
/ * *
* Show the current status of the sync preference ( on / off ) for the logged in user .
* Show s the current status of the sync preference ( " On " / " Off " ) for the logged in user .
* /
private fun updateSyncPreferenceStatus ( ) {
sync Logins Preference. apply {
sync Preference. apply {
val syncEnginesStatus = SyncEnginesStorage ( context ) . getStatus ( )
val loginsSyncStatus = syncEnginesStatus . getOrElse ( SyncEngine . Passwords ) { false }
val loginsSyncStatus = syncEnginesStatus . getOrElse ( syncEngine ) { false }
summary = context . getString (
if ( loginsSyncStatus ) R . string . preferences _passwords _sync _logins _on
else R . string . preferences _passwords _sync _logins _off
)
setOnPreferenceClickListener {
navigateToAccountSettingsFragment ( )
onSyncStatusClicked ( )
true
}
}
}
/ * *
* Indicate that the user can sign in to turn on sync .
* Display that the user can " Sign in to Sync " when the user is logged off .
* /
private fun updateSyncPreferenceNeedsLogin ( ) {
sync Logins Preference. apply {
sync Preference. apply {
summary = context . getString ( R . string . preferences _passwords _sync _logins _sign _in )
setOnPreferenceClickListener {
navigateToTurnOnSyncFragment ( )
onSignInToSyncClicked ( )
true
}
}
}
/ * *
* Indicate that the user can fix their account problems to turn on sync .
* Displays that the user needs to " Reconnect " to fix their account problems with sync .
* /
private fun updateSyncPreferenceNeedsReauth ( ) {
sync Logins Preference. apply {
sync Preference. apply {
summary = context . getString ( R . string . preferences _passwords _sync _logins _reconnect )
setOnPreferenceClickListener {
navigateToAccountProblemFragment ( )
onReconnectClicked ( )
true
}
}
}
private fun navigateToAccountSettingsFragment ( ) {
val directions =
SavedLoginsAuthFragmentDirections . actionGlobalAccountSettingsFragment ( )
navController . navigate ( directions )
}
private fun navigateToAccountProblemFragment ( ) {
val directions = SavedLoginsAuthFragmentDirections . actionGlobalAccountProblemFragment ( )
navController . navigate ( directions )
}
private fun navigateToTurnOnSyncFragment ( ) {
val directions = SavedLoginsAuthFragmentDirections . actionSavedLoginsAuthFragmentToTurnOnSyncFragment ( )
navController . navigate ( directions )
}
}