Skip to content

Commit

Permalink
Merge pull request #1309 from radixdlt/feature/ABW-4050-change-main-d…
Browse files Browse the repository at this point in the history
…evice-factor-source

[ABW-4050] Change main device factor source
  • Loading branch information
giannis-rdx authored Jan 22, 2025
2 parents bd89e23 + fc7a454 commit a051b42
Show file tree
Hide file tree
Showing 18 changed files with 566 additions and 192 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package com.babylon.wallet.android.domain.usecases.factorsources

import com.radixdlt.sargon.FactorSource
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.map
import rdx.works.profile.domain.GetProfileUseCase
import javax.inject.Inject

class GetFactorSourcesOfTypeUseCase @Inject constructor(
val getProfileUseCase: GetProfileUseCase
) {

@OptIn(ExperimentalCoroutinesApi::class)
inline operator fun <reified T : FactorSource> invoke(): Flow<T> {
inline operator fun <reified T : FactorSource> invoke(): Flow<List<T>> {
return getProfileUseCase.flow
.flatMapConcat { profile ->
profile.factorSources.asFlow()
.map { profile ->
profile.factorSources.filterIsInstance<T>()
}
.filterIsInstance<T>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import com.babylon.wallet.android.presentation.common.UiMessage
import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.utils.AppEvent
import com.babylon.wallet.android.utils.AppEventBus
import com.babylon.wallet.android.utils.Constants.DISPLAY_NAME_MAX_LENGTH
import com.radixdlt.sargon.Account
import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.CommonException
import com.radixdlt.sargon.DisplayName
import com.radixdlt.sargon.EntityKind
import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.extensions.SharedConstants.entityNameMaxLength
import com.radixdlt.sargon.extensions.asGeneral
import com.radixdlt.sargon.extensions.isManualCancellation
import com.radixdlt.sargon.extensions.kind
Expand Down Expand Up @@ -77,8 +77,8 @@ class CreateAccountViewModel @Inject constructor(

fun onAccountNameChange(accountName: String) {
savedStateHandle[ACCOUNT_NAME] = accountName
savedStateHandle[IS_ACCOUNT_NAME_LENGTH_MORE_THAN_THE_MAX] = accountName.count() > DISPLAY_NAME_MAX_LENGTH
savedStateHandle[CREATE_ACCOUNT_BUTTON_ENABLED] = accountName.trim().isNotEmpty() && accountName.count() <= DISPLAY_NAME_MAX_LENGTH
savedStateHandle[IS_ACCOUNT_NAME_LENGTH_MORE_THAN_THE_MAX] = accountName.count() > entityNameMaxLength
savedStateHandle[CREATE_ACCOUNT_BUTTON_ENABLED] = accountName.trim().isNotEmpty() && accountName.count() <= entityNameMaxLength
}

fun onAccountCreateClick(isWithLedger: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import com.babylon.wallet.android.presentation.model.PersonaFieldWrapper
import com.babylon.wallet.android.presentation.model.empty
import com.babylon.wallet.android.presentation.model.isValid
import com.babylon.wallet.android.presentation.model.sortOrderInt
import com.babylon.wallet.android.utils.Constants
import com.radixdlt.sargon.Persona
import com.radixdlt.sargon.PersonaDataEntryId
import com.radixdlt.sargon.extensions.SharedConstants.entityNameMaxLength
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
Expand Down Expand Up @@ -57,7 +57,7 @@ class PersonaEditableImpl : PersonaEditable {
currentFields = currentFields,
fieldsToAdd = fieldsToAdd,
personaDisplayName = PersonaDisplayNameFieldWrapper(
persona?.displayName?.value.orEmpty().take(Constants.DISPLAY_NAME_MAX_LENGTH)
persona?.displayName?.value.orEmpty().take(entityNameMaxLength.toInt())
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.babylon.wallet.android.presentation.model

import com.babylon.wallet.android.utils.Constants
import com.radixdlt.sargon.extensions.SharedConstants.entityNameMaxLength

data class PersonaDisplayNameFieldWrapper(
val value: String = "",
Expand All @@ -12,7 +12,7 @@ data class PersonaDisplayNameFieldWrapper(
val valueTrimmed = value.trim()
return when {
valueTrimmed.isEmpty() -> ValidationState.Empty
valueTrimmed.length > Constants.DISPLAY_NAME_MAX_LENGTH -> ValidationState.TooLong
valueTrimmed.length > entityNameMaxLength -> ValidationState.TooLong
else -> ValidationState.Valid
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.babylon.wallet.android.presentation.common.StateViewModel
import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.presentation.ui.model.factors.FactorSourceCard
import com.babylon.wallet.android.presentation.ui.model.factors.FactorSourceStatusMessage
import com.babylon.wallet.android.utils.callSafely
import com.babylon.wallet.android.utils.relativeTimeFormatted
import com.radixdlt.sargon.Account
import com.radixdlt.sargon.ArculusCardFactorSource
Expand All @@ -27,9 +28,10 @@ import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
Expand All @@ -43,34 +45,46 @@ class ArculusCardsViewModel @Inject constructor(
override fun initialState(): State = State()

init {
@Suppress("OPT_IN_USAGE")
getFactorSourcesOfTypeUseCase<FactorSource.ArculusCard>()
.map { arculusFactorSource ->
val entitiesLinkedToDeviceFactorSource = sargonOsManager.sargonOs.entitiesLinkedToFactorSource(
factorSource = FactorSource.ArculusCard(arculusFactorSource.value),
profileToCheck = ProfileToCheck.Current
)
.mapLatest { arculusFactorSources ->
resetArculusFactorSourceList()

val factorSourceCard = arculusFactorSource.value.toFactorSourceCard(
messages = persistentListOf(),
accounts = entitiesLinkedToDeviceFactorSource.accounts.toPersistentList(),
personas = entitiesLinkedToDeviceFactorSource.personas.toPersistentList(),
hasHiddenEntities = entitiesLinkedToDeviceFactorSource.hiddenAccounts.isNotEmpty() ||
entitiesLinkedToDeviceFactorSource.hiddenPersonas.isNotEmpty()
)

// avoid duplication when a factor source is updated in the Factor Source Details screen
val updatedArculusFactorSources = _state.value.arculusFactorSources
.filterNot { it.id == factorSourceCard.id }
.toMutableList()
updatedArculusFactorSources.add(factorSourceCard)
_state.update { state ->
state.copy(arculusFactorSources = updatedArculusFactorSources.toPersistentList())
arculusFactorSources.map { arculusFactorSource ->
sargonOsManager.callSafely(dispatcher = defaultDispatcher) {
entitiesLinkedToFactorSource(
factorSource = FactorSource.ArculusCard(arculusFactorSource.value),
profileToCheck = ProfileToCheck.Current
)
}.onSuccess { entitiesLinkedToArculusFactorSource ->
val factorSourceCard = arculusFactorSource.value.toFactorSourceCard(
messages = persistentListOf(),
accounts = entitiesLinkedToArculusFactorSource.accounts.toPersistentList(),
personas = entitiesLinkedToArculusFactorSource.personas.toPersistentList(),
hasHiddenEntities = entitiesLinkedToArculusFactorSource.hiddenAccounts.isNotEmpty() ||
entitiesLinkedToArculusFactorSource.hiddenPersonas.isNotEmpty()
)
// avoid duplication when a factor source is updated in the Factor Source Details screen
val updatedArculusFactorSources = _state.value.arculusFactorSources
.filterNot { it.id == factorSourceCard.id }
.toMutableList()
updatedArculusFactorSources.add(factorSourceCard)
_state.update { state ->
state.copy(arculusFactorSources = updatedArculusFactorSources.toPersistentList())
}
}.onFailure { error ->
Timber.e("Failed to find linked entities: $error")
}
}
}
.flowOn(defaultDispatcher)
.launchIn(viewModelScope)
}

private fun resetArculusFactorSourceList() {
_state.update { state -> state.copy(arculusFactorSources = persistentListOf()) }
}

private fun ArculusCardFactorSource.toFactorSourceCard(
includeDescription: Boolean = false,
messages: PersistentList<FactorSourceStatusMessage> = persistentListOf(),
Expand Down
Loading

0 comments on commit a051b42

Please sign in to comment.