-
Notifications
You must be signed in to change notification settings - Fork 104
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
8aa281e
commit d2f4e4f
Showing
10 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
70 changes: 70 additions & 0 deletions
70
app/src/main/java/one/mixin/android/db/property/Web3PropertyHelper.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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package one.mixin.android.db.property | ||
|
||
import one.mixin.android.MixinApplication | ||
import one.mixin.android.db.WalletDatabase | ||
import one.mixin.android.extension.nowInUtc | ||
import one.mixin.android.vo.Property | ||
|
||
object Web3PropertyHelper { | ||
|
||
suspend fun updateKeyValue( | ||
key: String, | ||
value: String, | ||
) { | ||
val propertyDao = WalletDatabase.getDatabase(MixinApplication.appContext).web3PropertyDao() | ||
propertyDao.insertSuspend(Property(key, value, nowInUtc())) | ||
} | ||
|
||
suspend fun updateKeyValue( | ||
key: String, | ||
value: Long, | ||
) { | ||
updateKeyValue(key, value.toString()) | ||
} | ||
|
||
suspend fun updateKeyValue( | ||
key: String, | ||
value: Int, | ||
) { | ||
updateKeyValue(key, value.toString()) | ||
} | ||
|
||
suspend fun updateKeyValue( | ||
key: String, | ||
value: Boolean, | ||
) { | ||
updateKeyValue(key, value.toString()) | ||
} | ||
|
||
suspend fun deleteKeyValue(key: String) { | ||
val propertyDao = WalletDatabase.getDatabase(MixinApplication.appContext).web3PropertyDao() | ||
propertyDao.deletePropertyByKey(key) | ||
} | ||
|
||
suspend fun <T> findValueByKey( | ||
key: String, | ||
default: T, | ||
): T { | ||
val propertyDao = WalletDatabase.getDatabase(MixinApplication.appContext).web3PropertyDao() | ||
val value = propertyDao.findValueByKey(key) ?: return default | ||
return try { | ||
when (default) { | ||
is Int -> { | ||
value.toIntOrNull() ?: default | ||
} | ||
is Long -> { | ||
value.toIntOrNull() ?: default | ||
} | ||
is Boolean -> { | ||
value.toBooleanStrictOrNull() ?: default | ||
} | ||
else -> { | ||
value | ||
} | ||
} as T | ||
} catch (e: Exception) { | ||
default | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/one/mixin/android/db/web3/Web3PropertyDao.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package one.mixin.android.db | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Query | ||
import one.mixin.android.extension.nowInUtc | ||
import one.mixin.android.vo.Property | ||
|
||
@Dao | ||
interface Web3PropertyDao : BaseDao<Property> { | ||
@Query("SELECT * FROM properties WHERE `key` = :key") | ||
suspend fun findByKey(key: String): Property? | ||
|
||
@Query("SELECT value FROM properties WHERE `key` = :key") | ||
suspend fun findValueByKey(key: String): String? | ||
|
||
@Query("UPDATE properties SET value = :value, updated_at = :updatedAt WHERE `key` = :key") | ||
suspend fun updateValueByKey( | ||
key: String, | ||
value: String, | ||
updatedAt: String = nowInUtc(), | ||
) | ||
|
||
@Query("DELETE FROM properties WHERE `key` = :key") | ||
suspend fun deletePropertyByKey(key: String) | ||
} |
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
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
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
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
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