Fix duplicate unique_id (#834)

* feat: fix duplicate unique id

* feat: fix duplicate unique id issue
This commit is contained in:
Yogesh Choudhary Paliyal
2024-04-21 11:41:25 +05:30
committed by GitHub
parent b8488bafeb
commit 52446c5cc1
7 changed files with 25 additions and 10 deletions

View File

@@ -52,7 +52,7 @@ import java.util.Locale
@Composable
fun AccountDetailPage(
uniqueId: String?,
id: Long?,
viewModel: DetailViewModel = hiltViewModel()
) {
val dispatchAction = rememberDispatcher()
@@ -67,8 +67,8 @@ fun AccountDetailPage(
}
// Set initial object
LaunchedEffect(key1 = uniqueId) {
viewModel.loadAccount(uniqueId) {
LaunchedEffect(key1 = id) {
viewModel.loadAccount(id) {
setAccountModel(it.copy())
}
}

View File

@@ -36,9 +36,9 @@ class DetailViewModel @Inject constructor(
private val _accountModel by lazy { MutableLiveData<AccountModel>() }
val accountModel: LiveData<AccountModel> = _accountModel
fun loadAccount(uniqueId: String?, getAccount: (AccountModel) -> Unit) {
fun loadAccount(id: Long?, getAccount: (AccountModel) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
getAccount(appDb.getDao().getAccount(uniqueId) ?: AccountModel())
getAccount(appDb.getDao().getAccount(id) ?: AccountModel())
}
}

View File

@@ -74,7 +74,7 @@ fun AccountsList(accounts: List<AccountModel>? = null) {
modifier = Modifier.animateItemPlacement(),
account,
onClick = {
dispatch(NavigationAction(AccountDetailState(it.uniqueId)))
dispatch(NavigationAction(AccountDetailState(it.id)))
}
)
}

View File

@@ -1,3 +1,3 @@
package com.yogeshpaliyal.keypass.ui.redux.states
data class AccountDetailState(val accountId: String? = null) : ScreenState()
data class AccountDetailState(val accountId: Long? = null) : ScreenState()

View File

@@ -17,10 +17,11 @@ const val DB_VERSION_4 = 4
const val DB_VERSION_5 = 5
const val DB_VERSION_6 = 6
const val DB_VERSION_7 = 7
const val DB_VERSION_8 = 8
@Database(
entities = [AccountModel::class],
version = DB_VERSION_7,
version = DB_VERSION_8,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {

View File

@@ -75,8 +75,8 @@ interface DbDao {
sortingField: String?
): List<AccountModel>
@Query("SELECT * FROM account WHERE unique_id = :uniqueId")
suspend fun getAccount(uniqueId: String?): AccountModel?
@Query("SELECT * FROM account WHERE id = :id")
suspend fun getAccount(id: Long?): AccountModel?
@Query("SELECT DISTINCT tags FROM account WHERE tags IS NOT NULL AND tags <> ''")
fun getTags(): Flow<List<String>>

View File

@@ -11,6 +11,7 @@ import com.yogeshpaliyal.common.DB_VERSION_4
import com.yogeshpaliyal.common.DB_VERSION_5
import com.yogeshpaliyal.common.DB_VERSION_6
import com.yogeshpaliyal.common.DB_VERSION_7
import com.yogeshpaliyal.common.DB_VERSION_8
import com.yogeshpaliyal.common.R
import com.yogeshpaliyal.common.utils.getRandomString
import com.yogeshpaliyal.common.utils.getUserSettingsOrNull
@@ -92,6 +93,19 @@ object AppModule {
database.execSQL("UPDATE `account` SET `unique_id` = '${getRandomString()}' WHERE `unique_id` IS NULL")
}
})
builder.addMigrations(object : Migration(DB_VERSION_7, DB_VERSION_8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.query("select id from `account` where unique_id IN (SELECT unique_id FROM `account` GROUP BY unique_id HAVING COUNT(unique_id) > 1)")
.use {
while (it.moveToNext()) {
val id = it.getInt(0)
val query =
"update `account` set `unique_id` = '${getRandomString()}' where `id` = '$id'"
database.execSQL(query)
}
}
}
})
return builder.build()
}