-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9f55ff
commit cae9bd0
Showing
3 changed files
with
44 additions
and
15 deletions.
There are no files selected for viewing
41 changes: 30 additions & 11 deletions
41
app/src/main/java/com/tari/android/wallet/application/MigrationManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
app/src/main/java/com/tari/android/wallet/ui/common/SimpleViewModel.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters