Skip to content

Commit

Permalink
Perform peer DB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanilcenko committed May 7, 2024
1 parent a9f55ff commit cae9bd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package com.tari.android.wallet.application

import androidx.lifecycle.viewModelScope
import com.tari.android.wallet.BuildConfig
import com.tari.android.wallet.ui.common.SimpleViewModel
import com.tari.android.wallet.data.sharedPrefs.network.NetworkRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.apache.maven.artifact.versioning.DefaultArtifactVersion
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MigrationManager @Inject constructor(private val manager: WalletManager) {
class MigrationManager @Inject constructor(
private val walletManager: WalletManager,
private val application: TariWalletApplication,
private val networkRepository: NetworkRepository,
) {

private val simpleViewModel = SimpleViewModel()

fun validateVersion(onValid: () -> Unit, onError: () -> Unit) {
suspend fun validateVersion(onValid: () -> Unit, onError: () -> Unit) {
val walletVersion = getCurrentWalletVersion()

if (walletVersion.isEmpty() || DefaultArtifactVersion(walletVersion) < DefaultArtifactVersion(BuildConfig.LIB_WALLET_MIN_VALID_VERSION)) {
simpleViewModel.viewModelScope.launch(Dispatchers.Main) { onError() }
onError()
} else {
simpleViewModel.viewModelScope.launch(Dispatchers.Main) { onValid() }
onValid()
}

if (walletVersion.isNotEmpty() && DefaultArtifactVersion(walletVersion) >= DefaultArtifactVersion(PEER_DB_MIGRATION_MIN_VERSION)) {
performPeerDbMigration()
}
}

private suspend fun getCurrentWalletVersion(): String = withContext(Dispatchers.IO) { walletManager.getCommsConfig().getLastVersion() }

private suspend fun performPeerDbMigration() = withContext(Dispatchers.IO) {
application.applicationInfo.dataDir?.let { appDir ->
val peerDbFile = File(appDir, "files/${networkRepository.currentNetwork.network.uriComponent}/data.mdb")
if (peerDbFile.exists()) {
peerDbFile.delete()
}
}
}

private fun getCurrentWalletVersion(): String = manager.getCommsConfig().getLastVersion()
}
companion object {
const val PEER_DB_MIGRATION_MIN_VERSION = "v1.0.0-rc.8"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ class AuthViewModel : CommonViewModel() {
init {
component.inject(this)

migrationManager.validateVersion({ goAuth.postValue(Unit) }, { showIncompatibleVersionDialog() })
viewModelScope.launch(Dispatchers.IO) {
migrationManager.validateVersion(
onValid = {
viewModelScope.launch(Dispatchers.Main) {
goAuth.postValue(Unit)
}
},
onError = {
viewModelScope.launch(Dispatchers.Main) {
showIncompatibleVersionDialog()
}
}
)
}
}

private fun showIncompatibleVersionDialog() {
Expand Down

0 comments on commit cae9bd0

Please sign in to comment.