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:
parent
204364d787
commit
a418baea30
@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user