Skip to content

Commit

Permalink
Merge pull request #948 from radixdlt/fixes
Browse files Browse the repository at this point in the history
Fixes regarding showing the claim screen for existing users
  • Loading branch information
micbakos-rdx authored May 30, 2024
2 parents 415cbe8 + f5a0219 commit 9499568
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/babylon/wallet/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.babylon.wallet.android.presentation.ui.composables.DevelopmentPreview
import com.babylon.wallet.android.presentation.ui.composables.actionableaddress.ActionableAddressViewEntryPoint
import dagger.hilt.android.AndroidEntryPoint
import rdx.works.profile.cloudbackup.CloudBackupSyncExecutor
import timber.log.Timber
import javax.inject.Inject

// Extending from FragmentActivity because of Biometric
Expand Down Expand Up @@ -60,7 +59,6 @@ class MainActivity : FragmentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)

Timber.tag("CloudBackup").d("⌛ Start periodic checks for cloud backups")
cloudBackupSyncExecutor.startPeriodicChecks(lifecycleOwner = this)

setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import rdx.works.profile.cloudbackup.data.GoogleSignInManager
import rdx.works.profile.cloudbackup.domain.CheckMigrationToNewBackupSystemUseCase
import rdx.works.profile.cloudbackup.model.BackupServiceException
import rdx.works.profile.cloudbackup.model.GoogleAccount
import rdx.works.profile.domain.backup.ChangeBackupSettingUseCase
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -25,6 +26,7 @@ class ConnectCloudBackupViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val googleSignInManager: GoogleSignInManager,
private val checkMigrationToNewBackupSystemUseCase: CheckMigrationToNewBackupSystemUseCase,
private val changeBackupSettingUseCase: ChangeBackupSettingUseCase
) : StateViewModel<ConnectCloudBackupViewModel.State>(),
CanSignInToGoogle,
OneOffEventHandler<ConnectCloudBackupViewModel.Event> by OneOffEventHandlerImpl() {
Expand All @@ -40,6 +42,14 @@ class ConnectCloudBackupViewModel @Inject constructor(
result.onSuccess { googleAccount ->
Timber.tag("CloudBackup").d("\uD83D\uDD11 Authorized for email: ${googleAccount.email}")
_state.update { it.copy(isConnecting = false) }

if (state.value.mode == ConnectMode.ExistingWallet) {
// For existing users only who migrate to the new backup system, request an eager backup as soon as they opt in
// We do that by "updating" the profile, thus updating the newly introduced device id.
// See also in ProfileRepositoryImpl.saveProfile() the related comment.
changeBackupSettingUseCase(isChecked = true)
}

sendEvent(Event.Proceed(mode = state.value.mode, isCloudBackupEnabled = true))
}.onFailure { exception ->
_state.update { state -> state.copy(isConnecting = false) }
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/rdx/works/core/domain/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ data class DeviceInfo(
"$name $commercialName"
}

fun toSargonDeviceInfo(): com.radixdlt.sargon.DeviceInfo = com.radixdlt.sargon.DeviceInfo(
id = id,
date = date,
description = displayName
)

companion object {

fun factory(context: Context) = DeviceInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,7 @@ fun Profile.prettyPrinted(): String = profileToDebugString(profile = this)
fun Profile.claim(
deviceInfo: rdx.works.core.domain.DeviceInfo
): Profile = copy(
header = header.copy(
lastUsedOnDevice = DeviceInfo(
id = deviceInfo.id,
date = deviceInfo.date,
description = deviceInfo.displayName
)
)
header = header.copy(lastUsedOnDevice = deviceInfo.toSargonDeviceInfo())
)

fun Profile.addAccounts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CloudBackupSyncExecutor @Inject constructor(
.setConstraints(constraints)
.build()

Timber.tag("CloudBackup").d("⏰ Enqueued")
Timber.tag("CloudBackup").d("⌛ Register periodic checks for cloud backups")
workManager.enqueueUniquePeriodicWork(
CHECK_CLOUD_STATUS_WORK,
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
Expand All @@ -103,7 +103,7 @@ class CloudBackupSyncExecutor @Inject constructor(
}

override fun onStop(owner: LifecycleOwner) {
Timber.tag("CloudBackup").d("Stop periodic checks for cloud backups")
Timber.tag("CloudBackup").d("Unregister periodic checks for cloud backups")
workManager.cancelUniqueWork(CHECK_CLOUD_STATUS_WORK)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CheckBackupStatusUseCase @AssistedInject constructor(
if (!profile.canBackupToCloud) return Result.success()

val lastBackupEvent = preferencesManager.lastCloudBackupEvent.firstOrNull()
// TODO add check for migration modal?

return if (lastBackupEvent == null || profile.header.lastModified > lastBackupEvent.profileModifiedTime) {
cloudBackupSyncExecutor.requestCloudBackup()
Result.success()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ProfileRepositoryImpl @Inject constructor(
private val encryptedPreferencesManager: EncryptedPreferencesManager,
private val preferencesManager: PreferencesManager,
private val cloudBackupSyncExecutor: CloudBackupSyncExecutor,
private val deviceInfoRepository: DeviceInfoRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@ApplicationScope applicationScope: CoroutineScope
) : ProfileRepository {
Expand Down Expand Up @@ -89,8 +90,19 @@ class ProfileRepositoryImpl @Inject constructor(
}

override suspend fun saveProfile(profile: Profile) {
val deviceInfo = deviceInfoRepository.getDeviceInfo()
val profileToSave = profile.copy(
header = profile.header.copy(lastModified = TimestampGenerator())
header = profile.header.copy(
lastModified = TimestampGenerator(),
// In general this would be unnecessary. Meaning that a new user will generate a device id and store it in profile
// or restore from backup, generated a new device id and claim the profile. In both situations the device info should
// have the correct up-to-date device id.
// The problem lies to existing users: Those who updated to version 1.6.0. In this version the android app introduced
// the concept of device id, which is stored in preferences. These users need to silently update their lastUsedOnDevice
// to have the current information about the device id. Profiles prior to this, which were generated by the android app
// had device id == profile id
lastUsedOnDevice = deviceInfo.toSargonDeviceInfo()
)
)
withContext(ioDispatcher) {
val profileContent = profileToSave.toJson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rdx.works.profile.domain

import com.radixdlt.sargon.AppPreferences
import com.radixdlt.sargon.ContentHint
import com.radixdlt.sargon.DeviceInfo
import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.Header
import com.radixdlt.sargon.MnemonicWithPassphrase
Expand Down Expand Up @@ -40,11 +39,7 @@ class GenerateProfileUseCase @Inject constructor(
suspend operator fun invoke(mnemonicWithPassphrase: MnemonicWithPassphrase): Profile {
val device = deviceInfoRepository.getDeviceInfo()

val creatingDevice = DeviceInfo(
id = device.id,
date = device.date,
description = device.displayName
)
val creatingDevice = device.toSargonDeviceInfo()

val date = TimestampGenerator()
val header = Header(
Expand Down Expand Up @@ -91,11 +86,7 @@ class GenerateProfileUseCase @Inject constructor(
else -> withContext(defaultDispatcher) {
val device = deviceInfoRepository.getDeviceInfo()

val creatingDevice = DeviceInfo(
id = device.id,
date = device.date,
description = device.displayName
)
val creatingDevice = device.toSargonDeviceInfo()

val header = Header(
snapshotVersion = ProfileSnapshotVersion.V100,
Expand Down

0 comments on commit 9499568

Please sign in to comment.