Commit 24a9b0a5 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add check to whether app already a migration already happened

parent a43b7c89
...@@ -15,11 +15,11 @@ import chat.rocket.android.app.migration.model.RealmBasedServerInfo ...@@ -15,11 +15,11 @@ import chat.rocket.android.app.migration.model.RealmBasedServerInfo
import chat.rocket.android.app.migration.model.RealmPublicSetting import chat.rocket.android.app.migration.model.RealmPublicSetting
import chat.rocket.android.app.migration.model.RealmSession import chat.rocket.android.app.migration.model.RealmSession
import chat.rocket.android.app.migration.model.RealmUser import chat.rocket.android.app.migration.model.RealmUser
import chat.rocket.android.authentication.domain.model.TokenModel
import chat.rocket.android.authentication.domain.model.toToken import chat.rocket.android.authentication.domain.model.toToken
import chat.rocket.android.dagger.DaggerAppComponent import chat.rocket.android.dagger.DaggerAppComponent
import chat.rocket.android.helper.CrashlyticsTree import chat.rocket.android.helper.CrashlyticsTree
import chat.rocket.android.helper.UrlHelper import chat.rocket.android.helper.UrlHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.* import chat.rocket.android.server.domain.*
import chat.rocket.android.server.domain.model.Account import chat.rocket.android.server.domain.model.Account
import chat.rocket.android.widget.emoji.EmojiRepository import chat.rocket.android.widget.emoji.EmojiRepository
...@@ -76,6 +76,8 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -76,6 +76,8 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
lateinit var prefs: SharedPreferences lateinit var prefs: SharedPreferences
@Inject @Inject
lateinit var getAccountsInteractor: GetAccountsInteractor lateinit var getAccountsInteractor: GetAccountsInteractor
@Inject
lateinit var localRepository: LocalRepository
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
...@@ -94,10 +96,11 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -94,10 +96,11 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
// TODO - remove this and all realm stuff when we got to 80% in 2.0 // TODO - remove this and all realm stuff when we got to 80% in 2.0
try { try {
if (!localRepository.hasMigrated()) {
migrateFromLegacy() migrateFromLegacy()
}
} catch (ex: Exception) { } catch (ex: Exception) {
Timber.d(ex, "Error migrating old accounts") Timber.d(ex, "Error migrating old accounts")
ex.printStackTrace()
} }
} }
...@@ -138,6 +141,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -138,6 +141,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
} }
migrateCurrentServer(serversInfoList) migrateCurrentServer(serversInfoList)
serverRealm.close() serverRealm.close()
localRepository.setMigrated(true)
} }
private fun migrateServerInfo(url: String, authToken: String, settings: PublicSettings, user: RealmUser) { private fun migrateServerInfo(url: String, authToken: String, settings: PublicSettings, user: RealmUser) {
...@@ -151,7 +155,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -151,7 +155,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
} }
val account = Account(url, icon, logo, user.username!!, avatar) val account = Account(url, icon, logo, user.username!!, avatar)
launch(CommonPool) { launch(CommonPool) {
tokenRepository.save(url, Token(userId!!, authToken)) tokenRepository.save(Token(userId!!, authToken))
accountRepository.save(account) accountRepository.save(account)
} }
} }
...@@ -210,14 +214,14 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -210,14 +214,14 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
getCurrentServerInteractor.get()?.let { serverUrl -> getCurrentServerInteractor.get()?.let { serverUrl ->
multiServerRepository.get(serverUrl)?.let { token -> multiServerRepository.get(serverUrl)?.let { token ->
tokenRepository.save(serverUrl, Token(token.userId, token.authToken)) tokenRepository.save(Token(token.userId, token.authToken))
} }
} }
runBlocking { runBlocking {
getAccountsInteractor.get().forEach { account -> getAccountsInteractor.get().forEach { account ->
multiServerRepository.get(account.serverUrl)?.let { token -> multiServerRepository.get(account.serverUrl)?.let { token ->
tokenRepository.save(account.serverUrl, token.toToken()) tokenRepository.save(token.toToken())
} }
} }
} }
...@@ -255,4 +259,10 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -255,4 +259,10 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
} }
} }
private fun LocalRepository.setMigrated(migrated: Boolean) {
save(LocalRepository.MIGRATION_FINISHED_KEY, migrated)
}
private fun LocalRepository.hasMigrated() = getBoolean(LocalRepository.MIGRATION_FINISHED_KEY)
private const val INTERNAL_TOKEN_MIGRATION_NEEDED = "INTERNAL_TOKEN_MIGRATION_NEEDED" private const val INTERNAL_TOKEN_MIGRATION_NEEDED = "INTERNAL_TOKEN_MIGRATION_NEEDED"
\ No newline at end of file
...@@ -54,7 +54,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView, ...@@ -54,7 +54,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
client.login(usernameOrEmail, password, twoFactorAuthenticationCode) client.login(usernameOrEmail, password, twoFactorAuthenticationCode)
val me = client.me() val me = client.me()
saveAccount(me) saveAccount(me)
tokenRepository.save(server, token) tokenRepository.save(token)
registerPushToken() registerPushToken()
navigator.toChatList() navigator.toChatList()
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
......
...@@ -2,18 +2,24 @@ package chat.rocket.android.infrastructure ...@@ -2,18 +2,24 @@ package chat.rocket.android.infrastructure
interface LocalRepository { interface LocalRepository {
fun save(key: String, value: String?)
fun save(key: String, value: Boolean)
fun save(key: String, value: Int)
fun save(key: String, value: Long)
fun save(key: String, value: Float)
fun get(key: String): String?
fun getBoolean(key: String): Boolean
fun getFloat(key: String): Float
fun getInt(key: String): Int
fun getLong(key: String): Long
fun clear(key: String)
fun clearAllFromServer(server: String)
companion object { companion object {
const val KEY_PUSH_TOKEN = "KEY_PUSH_TOKEN" const val KEY_PUSH_TOKEN = "KEY_PUSH_TOKEN"
const val MIGRATION_FINISHED_KEY = "MIGRATION_FINISHED_KEY"
const val TOKEN_KEY = "token_" const val TOKEN_KEY = "token_"
const val SETTINGS_KEY = "settings_" const val SETTINGS_KEY = "settings_"
const val CURRENT_USERNAME_KEY = "username_" const val CURRENT_USERNAME_KEY = "username_"
} }
fun save(key: String, value: String?)
fun get(key: String): String?
fun clear(key: String)
fun clearAllFromServer(server: String)
} }
\ No newline at end of file
...@@ -3,18 +3,27 @@ package chat.rocket.android.infrastructure ...@@ -3,18 +3,27 @@ package chat.rocket.android.infrastructure
import android.content.SharedPreferences import android.content.SharedPreferences
class SharedPrefsLocalRepository(private val preferences: SharedPreferences) : LocalRepository { class SharedPrefsLocalRepository(private val preferences: SharedPreferences) : LocalRepository {
override fun getBoolean(key: String) = preferences.getBoolean(key, false)
override fun save(key: String, value: String?) { override fun getFloat(key: String) = preferences.getFloat(key, -1f)
preferences.edit().putString(key, value).apply()
}
override fun get(key: String): String? { override fun getInt(key: String) = preferences.getInt(key, -1)
return preferences.getString(key, null)
}
override fun clear(key: String) { override fun getLong(key: String) = preferences.getLong(key, -1L)
preferences.edit().remove(key).apply()
} override fun save(key: String, value: Int) = preferences.edit().putInt(key, value).apply()
override fun save(key: String, value: Float) = preferences.edit().putFloat(key, value).apply()
override fun save(key: String, value: Long) = preferences.edit().putLong(key, value).apply()
override fun save(key: String, value: Boolean) = preferences.edit().putBoolean(key, value).apply()
override fun save(key: String, value: String?) = preferences.edit().putString(key, value).apply()
override fun get(key: String): String? = preferences.getString(key, null)
override fun clear(key: String) = preferences.edit().remove(key).apply()
override fun clearAllFromServer(server: String) { override fun clearAllFromServer(server: String) {
clear(LocalRepository.KEY_PUSH_TOKEN) clear(LocalRepository.KEY_PUSH_TOKEN)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment