mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-05 21:20:45 +00:00
Issue #418: Add account settings page and sign out functionality
This commit is contained in:
parent
2df5fa2e52
commit
1b1a9e0d11
@ -0,0 +1,55 @@
|
||||
/* 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.settings
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.navigation.Navigation
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.getPreferenceKey
|
||||
import org.mozilla.fenix.ext.requireComponents
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
||||
private lateinit var job: Job
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.Main + job
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
job = Job()
|
||||
(activity as AppCompatActivity).title = getString(R.string.preferences_account_settings)
|
||||
(activity as AppCompatActivity).supportActionBar?.show()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
|
||||
|
||||
val signIn = context?.getPreferenceKey(R.string.pref_key_sign_out)
|
||||
val preferenceSignOut = findPreference<Preference>(signIn)
|
||||
preferenceSignOut.onPreferenceClickListener = getClickListenerForSignOut()
|
||||
}
|
||||
|
||||
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
|
||||
return Preference.OnPreferenceClickListener {
|
||||
launch {
|
||||
requireComponents.backgroundServices.accountManager.logout().await()
|
||||
Navigation.findNavController(view!!).popBackStack()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.navigation.Navigation
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.Preference.OnPreferenceClickListener
|
||||
import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -44,6 +45,7 @@ import org.mozilla.fenix.R.string.pref_key_data_choices
|
||||
import org.mozilla.fenix.R.string.pref_key_about
|
||||
import org.mozilla.fenix.R.string.pref_key_sign_in
|
||||
import org.mozilla.fenix.R.string.pref_key_account
|
||||
import org.mozilla.fenix.R.string.pref_key_account_category
|
||||
|
||||
@SuppressWarnings("TooManyFunctions")
|
||||
class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObserver {
|
||||
@ -54,7 +56,6 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
job = Job()
|
||||
setupAccountUI()
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
@ -67,6 +68,7 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
(activity as AppCompatActivity).supportActionBar?.show()
|
||||
generateWordmark()
|
||||
setupPreferences()
|
||||
setupAccountUI()
|
||||
}
|
||||
|
||||
@Suppress("ComplexMethod")
|
||||
@ -100,6 +102,9 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
requireComponents.useCases.tabsUseCases.addTab.invoke(aboutURL, true)
|
||||
navigateToSettingsArticle()
|
||||
}
|
||||
resources.getString(pref_key_account) -> {
|
||||
navigateToAccountSettings()
|
||||
}
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
@ -110,16 +115,6 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
}
|
||||
|
||||
private fun setupAccountUI() {
|
||||
val signIn = context?.getPreferenceKey(pref_key_sign_in)
|
||||
val firefoxAccountKey = context?.getPreferenceKey(pref_key_account)
|
||||
|
||||
val preferenceSignIn = findPreference<Preference>(signIn)
|
||||
val preferenceFirefoxAccount = findPreference<Preference>(firefoxAccountKey)
|
||||
|
||||
preferenceSignIn.isVisible = true
|
||||
preferenceFirefoxAccount.isVisible = false
|
||||
preferenceSignIn.onPreferenceClickListener = getClickListenerForSignIn()
|
||||
|
||||
val accountManager = requireComponents.backgroundServices.accountManager
|
||||
// Observe account changes to keep the UI up-to-date.
|
||||
accountManager.register(this, owner = this)
|
||||
@ -127,7 +122,7 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
// TODO an authenticated state will mark 'preferenceSignIn' as invisible; currently that behaviour is non-ideal:
|
||||
// a "sign in" UI will be displayed at first, and then quickly animate away.
|
||||
// Ideally we don't want it to be displayed at all.
|
||||
accountManager.authenticatedAccount()?.let { setIsAuthenticated(it) }
|
||||
updateAuthState(accountManager.authenticatedAccount())
|
||||
accountManager.accountProfile()?.let { updateAccountProfile(it) }
|
||||
}
|
||||
|
||||
@ -235,9 +230,14 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
}
|
||||
}
|
||||
|
||||
private fun navigateToAccountSettings() {
|
||||
val directions = SettingsFragmentDirections.actionSettingsFragmentToAccountSettingsFragment()
|
||||
Navigation.findNavController(view!!).navigate(directions)
|
||||
}
|
||||
|
||||
// --- AccountObserver interfaces ---
|
||||
override fun onAuthenticated(account: FirefoxAccountShaped) {
|
||||
setIsAuthenticated(account)
|
||||
updateAuthState(account)
|
||||
}
|
||||
|
||||
override fun onError(error: Exception) {
|
||||
@ -248,7 +248,7 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
}
|
||||
|
||||
override fun onLoggedOut() {
|
||||
setIsLoggedOut()
|
||||
updateAuthState()
|
||||
}
|
||||
|
||||
override fun onProfileUpdated(profile: Profile) {
|
||||
@ -256,30 +256,31 @@ class SettingsFragment : PreferenceFragmentCompat(), CoroutineScope, AccountObse
|
||||
}
|
||||
|
||||
// --- Account UI helpers ---
|
||||
private fun setIsAuthenticated(account: FirefoxAccountShaped) {
|
||||
private fun updateAuthState(account: FirefoxAccountShaped? = null) {
|
||||
val preferenceSignIn = findPreference<Preference>(context?.getPreferenceKey(pref_key_sign_in))
|
||||
val preferenceFirefoxAccount = findPreference<Preference>(context?.getPreferenceKey(pref_key_account))
|
||||
val accountPreferenceCategory =
|
||||
findPreference<PreferenceCategory>(context?.getPreferenceKey(pref_key_account_category))
|
||||
|
||||
preferenceSignIn.isVisible = false
|
||||
preferenceSignIn.onPreferenceClickListener = null
|
||||
preferenceFirefoxAccount.isVisible = true
|
||||
}
|
||||
|
||||
private fun setIsLoggedOut() {
|
||||
val preferenceSignIn = findPreference<Preference>(context?.getPreferenceKey(pref_key_sign_in))
|
||||
val preferenceFirefoxAccount = findPreference<Preference>(context?.getPreferenceKey(pref_key_account))
|
||||
|
||||
preferenceSignIn.isVisible = true
|
||||
|
||||
// TODO this isn't quite right, as we'll have an "Account" preference category title still visible on the screen
|
||||
preferenceFirefoxAccount.isVisible = false
|
||||
preferenceSignIn.onPreferenceClickListener = getClickListenerForSignIn()
|
||||
if (account != null) {
|
||||
preferenceSignIn.isVisible = false
|
||||
preferenceSignIn.onPreferenceClickListener = null
|
||||
preferenceFirefoxAccount.isVisible = true
|
||||
accountPreferenceCategory.isVisible = true
|
||||
} else {
|
||||
preferenceSignIn.isVisible = true
|
||||
preferenceSignIn.onPreferenceClickListener = getClickListenerForSignIn()
|
||||
preferenceFirefoxAccount.isVisible = false
|
||||
accountPreferenceCategory.isVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateAccountProfile(profile: Profile) {
|
||||
val preferenceFirefoxAccount = findPreference<Preference>(context?.getPreferenceKey(pref_key_account))
|
||||
preferenceFirefoxAccount.title = profile.displayName.orEmpty()
|
||||
preferenceFirefoxAccount.summary = profile.email.orEmpty()
|
||||
launch {
|
||||
val preferenceFirefoxAccount = findPreference<Preference>(context?.getPreferenceKey(pref_key_account))
|
||||
preferenceFirefoxAccount.title = profile.displayName.orEmpty()
|
||||
preferenceFirefoxAccount.summary = profile.email.orEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -98,6 +98,8 @@
|
||||
app:destination="@id/sitePermissionsFragment"/>
|
||||
<action android:id="@+id/action_settingsFragment_to_accessibilityFragment"
|
||||
app:destination="@id/accessibilityFragment"/>
|
||||
<action android:id="@+id/action_settingsFragment_to_accountSettingsFragment"
|
||||
app:destination="@id/accountSettingsFragment"/>
|
||||
</fragment>
|
||||
<fragment android:id="@+id/dataChoicesFragment" android:name="org.mozilla.fenix.settings.DataChoicesFragment"
|
||||
android:label="DataChoicesFragment"/>
|
||||
@ -106,4 +108,6 @@
|
||||
android:label="SitePermissionsFragment"/>
|
||||
<fragment android:id="@+id/accessibilityFragment" android:name="org.mozilla.fenix.settings.AccessibilityFragment"
|
||||
android:label="AccessibilityFragment"/>
|
||||
<fragment android:id="@+id/accountSettingsFragment" android:name="org.mozilla.fenix.settings.AccountSettingsFragment"
|
||||
android:label="AccountSettingsFragment"/>
|
||||
</navigation>
|
@ -26,5 +26,10 @@
|
||||
<string name="pref_key_mozilla_location_service" translatable="false">pref_key_mozilla_location_service</string>
|
||||
<string name="pref_key_fenix_health_report" translatable="false">pref_key_fenix_health_report</string>
|
||||
|
||||
<!-- Account Settings -->
|
||||
<string name="pref_key_account_category" translatable="false">pref_key_account_category</string>
|
||||
<string name="pref_key_sync_now" translatable="false">pref_key_sync_now</string>
|
||||
<string name="pref_key_sync_history" translatable="false">pref_key_sync_history</string>
|
||||
<string name="pref_key_sign_out" translatable="false">pref_key_sign_out</string>
|
||||
|
||||
</resources>
|
||||
|
@ -108,6 +108,18 @@
|
||||
<string name="preferences_data_choices">Data choices</string>
|
||||
<!-- Preference for developers -->
|
||||
<string name="preference_leakcanary">Leak Canary</string>
|
||||
<!-- Preference for account settings -->
|
||||
<string name="preferences_account_settings">Account Settings</string>
|
||||
|
||||
<!-- Account Preferences -->
|
||||
<!-- Preference for triggering sync -->
|
||||
<string name="preferences_sync_now">Sync now</string>
|
||||
<!-- Preference category for sync -->
|
||||
<string name="preferences_sync_category">Choose what to sync</string>
|
||||
<!-- Preference for syncing history -->
|
||||
<string name="preferences_sync_history">History</string>
|
||||
<!-- Preference for signing out -->
|
||||
<string name="preferences_sign_out">Sign out</string>
|
||||
|
||||
<!-- Advanced Preferences -->
|
||||
<!-- Preference switch for Telemetry -->
|
||||
|
25
app/src/main/res/xml/account_settings_preferences.xml
Normal file
25
app/src/main/res/xml/account_settings_preferences.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?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/. -->
|
||||
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<androidx.preference.Preference
|
||||
android:key="@string/pref_key_sync_now"
|
||||
android:title="@string/preferences_sync_now"
|
||||
android:enabled = "false" />
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/preferences_sync_category">
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="@string/pref_key_sync_history"
|
||||
android:defaultValue="true"
|
||||
android:enabled="false"
|
||||
android:title="@string/preferences_sync_history" />
|
||||
</PreferenceCategory>
|
||||
|
||||
<androidx.preference.Preference
|
||||
android:key="@string/pref_key_sign_out"
|
||||
android:title="@string/preferences_sign_out" />
|
||||
</PreferenceScreen>
|
@ -13,8 +13,11 @@
|
||||
android:title="@string/preferences_sign_in" />
|
||||
|
||||
<androidx.preference.PreferenceCategory
|
||||
android:key = "@string/pref_key_account_category"
|
||||
android:title="@string/preferences_category_account"
|
||||
app:iconSpaceReserved="false">
|
||||
app:iconSpaceReserved="false"
|
||||
app:isPreferenceVisible="false">
|
||||
|
||||
<androidx.preference.Preference
|
||||
android:icon="@drawable/ic_shortcuts"
|
||||
android:key="@string/pref_key_account"
|
||||
|
Loading…
Reference in New Issue
Block a user