1
0
Fork 0

Fix rest of issues

master
Yeon Taek Jeong 2019-08-08 17:14:03 -07:00 committed by Jeff Boek
parent 69434a765f
commit 2a6069bfb7
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.os.Bundle
import android.text.InputFilter import android.text.InputFilter
import android.text.format.DateUtils import android.text.format.DateUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
@ -72,6 +73,22 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
requireComponents.analytics.metrics.track(Event.SyncAccountClosed) 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?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey) setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
@ -79,31 +96,18 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
AccountSettingsStore( AccountSettingsStore(
AccountSettingsState( AccountSettingsState(
lastSyncedDate = lastSyncedDate =
if (getLastSynced(requireContext()) == 0L) if (getLastSynced(requireContext()) == 0L)
LastSyncTime.Never LastSyncTime.Never
else else
LastSyncTime.Success(getLastSynced(requireContext())), LastSyncTime.Success(getLastSynced(requireContext())),
deviceName = requireComponents.backgroundServices.defaultDeviceName(requireContext()) deviceName = requireComponents.backgroundServices.defaultDeviceName(requireContext())
) )
) )
} }
consumeFrom(accountSettingsStore) {
updateLastSyncTimePref(it)
updateDeviceName(it)
}
accountManager = requireComponents.backgroundServices.accountManager accountManager = requireComponents.backgroundServices.accountManager
accountManager.register(accountStateObserver, this, true) accountManager.register(accountStateObserver, this, true)
accountSettingsInteractor = AccountSettingsInteractor(
findNavController(),
::syncNow,
::checkValidName,
::syncDeviceName,
accountSettingsStore
)
// Sign out // Sign out
val signOut = context!!.getPreferenceKey(R.string.pref_key_sign_out) val signOut = context!!.getPreferenceKey(R.string.pref_key_sign_out)
val preferenceSignOut = findPreference<Preference>(signOut) val preferenceSignOut = findPreference<Preference>(signOut)
@ -161,18 +165,10 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
} }
} }
private fun checkValidName(newValue: String): Boolean { private fun syncDeviceName(newValue: String): Boolean {
// The network request requires a nonempty string, so don't persist any changes if the user inputs one.
if (newValue.trim().isEmpty()) { if (newValue.trim().isEmpty()) {
FenixSnackbar.make(view!!, FenixSnackbar.LENGTH_LONG)
.setText(getString(R.string.empty_device_name_error))
.show()
return false 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. // This may fail, and we'll have a disparity in the UI until `updateDeviceName` is called.
lifecycleScope.launch(Main) { lifecycleScope.launch(Main) {
accountManager.authenticatedAccount() accountManager.authenticatedAccount()
@ -180,6 +176,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
?.setDeviceNameAsync(newValue) ?.setDeviceNameAsync(newValue)
?.await() ?.await()
} }
return true
} }
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener { private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
@ -198,7 +195,11 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
private fun getChangeListenerForDeviceName(): Preference.OnPreferenceChangeListener { private fun getChangeListenerForDeviceName(): Preference.OnPreferenceChangeListener {
return Preference.OnPreferenceChangeListener { _, newValue -> 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 * @param newDeviceName the device name to change to
* @return Boolean indicating whether the new device name has been accepted or not * @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 * Called whenever the "Sign out" button is tapped
@ -31,8 +31,7 @@ interface AccountSettingsUserActions {
class AccountSettingsInteractor( class AccountSettingsInteractor(
private val navController: NavController, private val navController: NavController,
private val syncNow: () -> Unit, private val syncNow: () -> Unit,
private val checkValidName: (String) -> Boolean, private val syncDeviceName: (String) -> Boolean,
private val setDeviceName: (String) -> Unit,
private val store: AccountSettingsStore private val store: AccountSettingsStore
) : AccountSettingsUserActions { ) : AccountSettingsUserActions {
@ -40,9 +39,9 @@ class AccountSettingsInteractor(
syncNow.invoke() syncNow.invoke()
} }
override fun onChangeDeviceName(newDeviceName: String): Boolean { override fun onChangeDeviceName(newDeviceName: String, invalidNameResponse: () -> Unit): Boolean {
val isValidName = checkValidName.invoke(newDeviceName) if (!syncDeviceName(newDeviceName)) {
if (!isValidName) { invalidNameResponse.invoke()
return false return false
} }
// Our "change the device name on the server" operation may fail. // 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. // So, when user presses "sync now", we'll fetch the old value, and reset the UI.
store.dispatch(AccountSettingsAction.UpdateDeviceName(newDeviceName)) store.dispatch(AccountSettingsAction.UpdateDeviceName(newDeviceName))
setDeviceName.invoke(newDeviceName)
return true return true
} }