2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] Fix rest of issues

This commit is contained in:
Yeon Taek Jeong 2019-08-08 17:14:03 -07:00 committed by Jeff Boek
parent b3ef78f3cc
commit b190e9b80d
2 changed files with 33 additions and 34 deletions

View File

@ -7,6 +7,7 @@ package org.mozilla.fenix.settings.account
import android.os.Bundle
import android.text.InputFilter
import android.text.format.DateUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
@ -72,6 +73,22 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
requireComponents.analytics.metrics.track(Event.SyncAccountClosed)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
consumeFrom(accountSettingsStore) {
updateLastSyncTimePref(it)
updateDeviceName(it)
}
accountSettingsInteractor = AccountSettingsInteractor(
findNavController(),
::syncNow,
::syncDeviceName,
accountSettingsStore
)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
@ -79,31 +96,18 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
AccountSettingsStore(
AccountSettingsState(
lastSyncedDate =
if (getLastSynced(requireContext()) == 0L)
LastSyncTime.Never
else
LastSyncTime.Success(getLastSynced(requireContext())),
if (getLastSynced(requireContext()) == 0L)
LastSyncTime.Never
else
LastSyncTime.Success(getLastSynced(requireContext())),
deviceName = requireComponents.backgroundServices.defaultDeviceName(requireContext())
)
)
}
consumeFrom(accountSettingsStore) {
updateLastSyncTimePref(it)
updateDeviceName(it)
}
accountManager = requireComponents.backgroundServices.accountManager
accountManager.register(accountStateObserver, this, true)
accountSettingsInteractor = AccountSettingsInteractor(
findNavController(),
::syncNow,
::checkValidName,
::syncDeviceName,
accountSettingsStore
)
// Sign out
val signOut = context!!.getPreferenceKey(R.string.pref_key_sign_out)
val preferenceSignOut = findPreference<Preference>(signOut)
@ -161,18 +165,10 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
}
}
private fun checkValidName(newValue: String): Boolean {
// The network request requires a nonempty string, so don't persist any changes if the user inputs one.
private fun syncDeviceName(newValue: String): Boolean {
if (newValue.trim().isEmpty()) {
FenixSnackbar.make(view!!, FenixSnackbar.LENGTH_LONG)
.setText(getString(R.string.empty_device_name_error))
.show()
return false
}
return true
}
private fun syncDeviceName(newValue: String) {
// This may fail, and we'll have a disparity in the UI until `updateDeviceName` is called.
lifecycleScope.launch(Main) {
accountManager.authenticatedAccount()
@ -180,6 +176,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
?.setDeviceNameAsync(newValue)
?.await()
}
return true
}
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
@ -198,7 +195,11 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
private fun getChangeListenerForDeviceName(): Preference.OnPreferenceChangeListener {
return Preference.OnPreferenceChangeListener { _, newValue ->
accountSettingsInteractor.onChangeDeviceName(newValue as String)
accountSettingsInteractor.onChangeDeviceName(newValue as String) {
FenixSnackbar.make(view!!, FenixSnackbar.LENGTH_LONG)
.setText(getString(R.string.empty_device_name_error))
.show()
}
}
}

View File

@ -20,7 +20,7 @@ interface AccountSettingsUserActions {
* @param newDeviceName the device name to change to
* @return Boolean indicating whether the new device name has been accepted or not
*/
fun onChangeDeviceName(newDeviceName: String): Boolean
fun onChangeDeviceName(newDeviceName: String, invalidNameResponse: () -> Unit): Boolean
/**
* Called whenever the "Sign out" button is tapped
@ -31,8 +31,7 @@ interface AccountSettingsUserActions {
class AccountSettingsInteractor(
private val navController: NavController,
private val syncNow: () -> Unit,
private val checkValidName: (String) -> Boolean,
private val setDeviceName: (String) -> Unit,
private val syncDeviceName: (String) -> Boolean,
private val store: AccountSettingsStore
) : AccountSettingsUserActions {
@ -40,9 +39,9 @@ class AccountSettingsInteractor(
syncNow.invoke()
}
override fun onChangeDeviceName(newDeviceName: String): Boolean {
val isValidName = checkValidName.invoke(newDeviceName)
if (!isValidName) {
override fun onChangeDeviceName(newDeviceName: String, invalidNameResponse: () -> Unit): Boolean {
if (!syncDeviceName(newDeviceName)) {
invalidNameResponse.invoke()
return false
}
// Our "change the device name on the server" operation may fail.
@ -53,7 +52,6 @@ class AccountSettingsInteractor(
// So, when user presses "sync now", we'll fetch the old value, and reset the UI.
store.dispatch(AccountSettingsAction.UpdateDeviceName(newDeviceName))
setDeviceName.invoke(newDeviceName)
return true
}