1
0
Fork 0

Fix most issues

master
Yeon Taek Jeong 2019-08-08 16:39:23 -07:00 committed by Jeff Boek
parent faf0ecbcc0
commit 69434a765f
4 changed files with 25 additions and 31 deletions

View File

@ -55,7 +55,7 @@ class BackgroundServices(
const val REDIRECT_URL = "https://accounts.firefox.com/oauth/success/$CLIENT_ID" const val REDIRECT_URL = "https://accounts.firefox.com/oauth/success/$CLIENT_ID"
} }
private val defaultDeviceName = context.getString( fun defaultDeviceName(context: Context): String = context.getString(
R.string.default_device_name, R.string.default_device_name,
context.getString(R.string.app_name), context.getString(R.string.app_name),
Build.MANUFACTURER, Build.MANUFACTURER,
@ -64,7 +64,7 @@ class BackgroundServices(
private val serverConfig = ServerConfig.release(CLIENT_ID, REDIRECT_URL) private val serverConfig = ServerConfig.release(CLIENT_ID, REDIRECT_URL)
private val deviceConfig = DeviceConfig( private val deviceConfig = DeviceConfig(
name = defaultDeviceName, name = defaultDeviceName(context),
type = DeviceType.MOBILE, type = DeviceType.MOBILE,
// NB: flipping this flag back and worth is currently not well supported and may need hand-holding. // NB: flipping this flag back and worth is currently not well supported and may need hand-holding.

View File

@ -13,18 +13,15 @@ import androidx.navigation.fragment.findNavController
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import mozilla.components.concept.sync.AccountObserver import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.ConstellationState import mozilla.components.concept.sync.ConstellationState
import mozilla.components.concept.sync.DeviceConstellationObserver import mozilla.components.concept.sync.DeviceConstellationObserver
import mozilla.components.lib.state.ext.observe import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.service.fxa.FxaException
import mozilla.components.service.fxa.FxaPanicException
import mozilla.components.service.fxa.manager.FxaAccountManager import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.service.fxa.sync.SyncStatusObserver import mozilla.components.service.fxa.sync.SyncStatusObserver
import mozilla.components.service.fxa.sync.getLastSynced import mozilla.components.service.fxa.sync.getLastSynced
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.StoreProvider
@ -86,16 +83,14 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
LastSyncTime.Never LastSyncTime.Never
else else
LastSyncTime.Success(getLastSynced(requireContext())), LastSyncTime.Success(getLastSynced(requireContext())),
deviceName = "" deviceName = requireComponents.backgroundServices.defaultDeviceName(requireContext())
) )
) )
} }
accountSettingsStore.observe(this) { consumeFrom(accountSettingsStore) {
viewLifecycleOwner.lifecycleScope.launch { updateLastSyncTimePref(it)
updateLastSyncTimePref(it) updateDeviceName(it)
updateDeviceName(it)
}
} }
accountManager = requireComponents.backgroundServices.accountManager accountManager = requireComponents.backgroundServices.accountManager
@ -103,8 +98,8 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
accountSettingsInteractor = AccountSettingsInteractor( accountSettingsInteractor = AccountSettingsInteractor(
findNavController(), findNavController(),
::onSyncNow, ::syncNow,
::makeSnackbar, ::checkValidName,
::syncDeviceName, ::syncDeviceName,
accountSettingsStore accountSettingsStore
) )
@ -153,7 +148,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
) )
} }
private fun onSyncNow() { private fun syncNow() {
lifecycleScope.launch { lifecycleScope.launch {
requireComponents.analytics.metrics.track(Event.SyncAccountSyncNow) requireComponents.analytics.metrics.track(Event.SyncAccountSyncNow)
// Trigger a sync. // Trigger a sync.
@ -166,7 +161,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
} }
} }
private fun makeSnackbar(newValue: String): Boolean { private fun checkValidName(newValue: String): Boolean {
// The network request requires a nonempty string, so don't persist any changes if the user inputs one. // 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) FenixSnackbar.make(view!!, FenixSnackbar.LENGTH_LONG)
@ -179,17 +174,11 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
private fun syncDeviceName(newValue: String) { 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(IO) { lifecycleScope.launch(Main) {
try { accountManager.authenticatedAccount()
accountManager.authenticatedAccount() ?.deviceConstellation()
?.deviceConstellation() ?.setDeviceNameAsync(newValue)
?.setDeviceNameAsync(newValue) ?.await()
?.await()
} catch (e: FxaPanicException) {
throw e
} catch (e: FxaException) {
Logger.error("Setting device name failed.", e)
}
} }
} }

View File

@ -45,7 +45,12 @@ class AccountSettingsInteractor(
if (!isValidName) { if (!isValidName) {
return false return false
} }
// Optimistically set the device name to what user requested. // Our "change the device name on the server" operation may fail.
// Currently, we disregard this failure and pretend we succeeded.
// At the same time, when user changes the device name, we immediately update the UI to display the new name.
// So, in case of a network (or other) failure when talking to the server,
// we'll have a discrepancy - the UI will reflect new value, but actually the value never changed.
// 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) setDeviceName.invoke(newDeviceName)

View File

@ -28,8 +28,8 @@ sealed class LastSyncTime {
* The state for the Account Settings Screen * The state for the Account Settings Screen
*/ */
data class AccountSettingsState( data class AccountSettingsState(
val lastSyncedDate: LastSyncTime, val lastSyncedDate: LastSyncTime = LastSyncTime.Never,
val deviceName: String val deviceName: String = ""
) : State ) : State
/** /**