Fixed Default Address showing wrong value.

This commit is contained in:
Neil Marietta 2021-04-27 15:00:50 +02:00
parent 359bcdc1ca
commit c7af0d69b7
4 changed files with 44 additions and 20 deletions

View File

@ -140,25 +140,22 @@ class DefaultAddressActivity : BaseActivity() {
}
private fun renderAddresses() {
mSelectedAddress = addresses!![0]
val sortedAddresses = addresses.orEmpty()
.sortedBy { it.order }
.sortedBy { it.status == 1 && it.receive == 1 }
mSelectedAddress = sortedAddresses[0]
defaultAddress.text = mSelectedAddress!!.email
Collections.sort(addresses) { lhs, rhs ->
val lhsEnabled = lhs.status == 1 && lhs.receive == 1
val rhsEnabled = rhs.status == 1 && rhs.receive == 1
rhsEnabled.compareTo(lhsEnabled)
}
var mNoAvailableAddresses = true
var mNoInactiveAddresses = true
for (i in addresses!!.indices) {
val address = addresses!![i]
sortedAddresses.forEachIndexed { index, address ->
val aliasAvailable = address.status == 1 && address.receive == 1
var addressRadio: RadioButton? = null
var inactiveAddress: TextView? = null
if (aliasAvailable) {
addressRadio = mInflater!!.inflate(R.layout.alias_list_item, availableAddresses, false) as RadioButton
addressRadio.text = address.email
addressRadio.isChecked = i == 0
if (i == 0) {
addressRadio.isChecked = index == 0
if (index == 0) {
mCurrentSelectedRadioButton = addressRadio
}
mAllRadioButtons.add(addressRadio)

View File

@ -40,6 +40,9 @@ import ch.protonmail.android.utils.crypto.OpenPGP
import ch.protonmail.android.utils.extensions.app
import ch.protonmail.android.utils.extensions.obfuscateUsername
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -98,6 +101,8 @@ class UserManager @Inject constructor(
) {
private val app: ProtonMailApplication = context.app
private var refreshPrimary = MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
var primaryUserId: StateFlow<UserId?>
var primaryId: StateFlow<Id?>
var primaryLegacyUser: StateFlow<User?>
@ -106,10 +111,11 @@ class UserManager @Inject constructor(
init {
// Workaround to make sure we have fresh value and get them from main thread without impacting performances.
runBlocking {
refreshPrimary.emit(Unit)
primaryUserId = coreAccountManager.primaryUserId(scope)
primaryId = coreAccountManager.primaryId(scope)
primaryLegacyUser = coreAccountManager.primaryLegacyUser(scope) { getLegacyUser(it) }
primaryUser = coreAccountManager.primaryUser(scope) { getUser(it) }
primaryLegacyUser = coreAccountManager.primaryLegacyUser(scope, refreshPrimary) { getLegacyUser(it) }
primaryUser = coreAccountManager.primaryUser(scope, refreshPrimary) { getUser(it) }
}
}
@ -124,6 +130,13 @@ class UserManager @Inject constructor(
val currentUser: NewUser?
get() = primaryUser.value
/**
* Force Refresh for [currentUserId], [currentLegacyUser] and [currentUser].
*
* Note: This is a workaround to use when User/Address/Key are updated.
*/
fun refreshCurrent() = refreshPrimary.tryEmit(Unit)
fun requireCurrentUserId(): Id = checkNotNull(currentUserId)
fun requireCurrentLegacyUser(): User = checkNotNull(currentLegacyUser)

View File

@ -22,7 +22,9 @@ package ch.protonmail.android.feature.account
import ch.protonmail.android.api.models.User
import ch.protonmail.android.domain.entity.Id
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.stateIn
@ -44,14 +46,24 @@ suspend fun AccountManager.allLoggedIn() =
suspend fun AccountManager.allSaved() =
getAccounts(AccountState.Disabled).firstOrNull()?.map { Id(it.userId.id) }.orEmpty().toSet()
suspend fun AccountManager.primaryUserId(scope: CoroutineScope): StateFlow<UserId?> =
getPrimaryUserId().stateIn(scope)
suspend fun AccountManager.primaryUserId(
scope: CoroutineScope
): StateFlow<UserId?> = getPrimaryUserId().stateIn(scope)
suspend fun AccountManager.primaryId(scope: CoroutineScope): StateFlow<Id?> =
primaryUserId(scope).mapLatest { it?.let { Id(it.id) } }.stateIn(scope)
suspend fun AccountManager.primaryId(
scope: CoroutineScope
): StateFlow<Id?> = primaryUserId(scope).mapLatest { it?.let { Id(it.id) } }.stateIn(scope)
suspend fun AccountManager.primaryLegacyUser(scope: CoroutineScope, getUser: suspend (Id) -> User): StateFlow<User?> =
primaryId(scope).mapLatest { it?.let { getUser(it) } }.stateIn(scope)
suspend fun AccountManager.primaryLegacyUser(
scope: CoroutineScope,
refresh: Flow<Unit>,
getUser: suspend (Id) -> User
): StateFlow<User?> = primaryId(scope).combine(refresh) { id: Id?, _ -> id }
.mapLatest { it?.let { getUser(it) } }.stateIn(scope)
suspend fun AccountManager.primaryUser(scope: CoroutineScope, getNewUser: suspend (Id) -> NewUser): StateFlow<NewUser?> =
primaryId(scope).mapLatest { it?.let { getNewUser(it) } }.stateIn(scope)
suspend fun AccountManager.primaryUser(
scope: CoroutineScope,
refresh: Flow<Unit>,
getNewUser: suspend (Id) -> NewUser
): StateFlow<NewUser?> = primaryId(scope).combine(refresh) { id: Id?, _ -> id }
.mapLatest { it?.let { getNewUser(it) } }.stateIn(scope)

View File

@ -45,9 +45,11 @@ class UpdateSettingsJob(
val userId = Id(user.id)
if (addressIds != null) {
getUserAddressManager().updateOrderBlocking(userId, addressIds.map { Id(it) })
getUserManager().refreshCurrent()
}
if ((newDisplayName != null || newSignature != null) && addressId != null) {
getUserAddressManager().updateAddressBlocking(userId, addressId, newDisplayName, newSignature)
getUserManager().refreshCurrent()
}
val mailSettings = getUserManager().getCurrentUserMailSettingsBlocking()!!
if (actionLeftSwipeChanged) {