1
0
mirror of https://github.com/NekoX-Dev/NekoX.git synced 2024-12-13 22:40:07 +01:00

H2 failsafe

This commit is contained in:
世界 2021-03-31 10:27:07 +08:00
parent 204364d787
commit a418baea30
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4

View File

@ -5,11 +5,14 @@ import org.telegram.messenger.ApplicationLoader
import tw.nekomimi.nekogram.utils.FileUtil import tw.nekomimi.nekogram.utils.FileUtil
import java.io.File import java.io.File
fun mkDatabase(name: String): Nitrite { @JvmOverloads
fun mkDatabase(name: String, delete: Boolean = true): Nitrite {
val file = File("${ApplicationLoader.getDataDirFixed()}/databases/$name.db") val file = File("${ApplicationLoader.getDataDirFixed()}/databases/$name.db")
FileUtil.initDir(file.parentFile!!) FileUtil.initDir(file.parentFile!!)
if (delete) {
file.deleteRecursively()
}
fun create(): Nitrite { fun create(): Nitrite {
val nitrite = Nitrite.builder() val nitrite = Nitrite.builder()
@ -36,14 +39,23 @@ fun Nitrite.openSharedPreference(name: String) = DbPref(getCollection(name))
private lateinit var mainSharedPreferencesDatabase: Nitrite private lateinit var mainSharedPreferencesDatabase: Nitrite
fun openMainSharedPreference(name: String): DbPref { @JvmOverloads
fun openMainSharedPreference(name: String, delete: Boolean = false): DbPref {
if (!::mainSharedPreferencesDatabase.isInitialized) { if (!::mainSharedPreferencesDatabase.isInitialized || delete) {
mainSharedPreferencesDatabase = mkDatabase("shared_preferences") mainSharedPreferencesDatabase = mkDatabase("shared_preferences", delete)
} }
return mainSharedPreferencesDatabase.openSharedPreference(name) return try {
mainSharedPreferencesDatabase.openSharedPreference(name)
} catch (e: IllegalStateException) {
openMainSharedPreference(name, true)
}
} }