1
0
mirror of https://github.com/NekoX-Dev/NekoX.git synced 2024-12-16 14:19:59 +01:00

Migrate to nitrite 4

This commit is contained in:
世界 2020-12-24 21:56:50 +08:00
parent 261677c101
commit 6580fcbd1f
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
13 changed files with 75 additions and 107 deletions

View File

@ -79,17 +79,19 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:$okHttpVersion"
implementation 'dnsjava:dnsjava:3.3.1'
implementation 'org.dizitart:nitrite:3.4.3'
implementation 'cn.hutool:hutool-core:5.5.4'
implementation 'cn.hutool:hutool-crypto:5.5.4'
implementation(platform("org.dizitart:nitrite-bom:4.0.0-SNAPSHOT"))
implementation 'org.dizitart:nitrite'
implementation 'org.dizitart:nitrite-mvstore-adapter'
implementation ('cn.hutool:hutool-crypto:5.5.4') {
exclude group: "org.apache.poi"
}
implementation 'com.jakewharton:process-phoenix:2.0.0'
implementation project(":openpgp-api")
compileOnly files('libs/libv2ray.aar')
compileOnly "com.google.firebase:firebase-messaging:$fcmVersion"
compileOnly "com.google.firebase:firebase-crashlytics:$crashlyticsVersion"
compileOnly "com.google.android.play:core:$playCoreVersion"

View File

@ -38,6 +38,7 @@ import org.telegram.ui.Components.ForegroundDetector;
import java.io.File;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import tw.nekomimi.nekogram.ExternalGcm;

View File

@ -28,7 +28,6 @@ import com.v2ray.ang.V2RayConfig;
import com.v2ray.ang.dto.AngConfig;
import com.v2ray.ang.util.Utils;
import org.dizitart.no2.objects.filters.ObjectFilters;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -55,6 +54,7 @@ import tw.nekomimi.nekogram.utils.UIUtil;
import static com.v2ray.ang.V2RayConfig.SSR_PROTOCOL;
import static com.v2ray.ang.V2RayConfig.SS_PROTOCOL;
import static org.dizitart.no2.filters.FluentFilter.where;
public class SharedConfig {
@ -229,7 +229,7 @@ public class SharedConfig {
try {
builder.append(SubManager.getSubList().find(ObjectFilters.eq("id", subId)).firstOrDefault().displayName());
builder.append(SubManager.getSubList().find(where("id").eq(subId)).firstOrNull().displayName());
} catch (Exception e) {
@ -1903,7 +1903,7 @@ public class SharedConfig {
}
proxyList.remove(proxyInfo);
if (proxyInfo.subId != 0) {
SubInfo sub = SubManager.getSubList().find(ObjectFilters.eq("id", proxyInfo.subId)).firstOrDefault();
SubInfo sub = SubManager.getSubList().find(where("id").eq(proxyInfo.subId)).firstOrNull();
try {
if (sub.proxies.remove(proxyInfo.toUrl())) {
SubManager.getSubList().update(sub);

View File

@ -13,11 +13,8 @@ import com.google.android.play.core.install.InstallStateUpdatedListener;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.InstallStatus;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.google.firebase.iid.FirebaseInstanceId;
import org.h2.util.IOUtils;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.BuildConfig;
import org.telegram.messenger.BuildVars;
@ -29,8 +26,6 @@ import org.telegram.messenger.SharedConfig;
import org.telegram.messenger.Utilities;
import org.telegram.tgnet.ConnectionsManager;
import javax.validation.constraints.NotNull;
import kotlin.Unit;
import tw.nekomimi.nekogram.utils.UIUtil;
@ -102,16 +97,6 @@ public class ExternalGcm {
GcmPushListenerService.sendRegistrationToServer(SharedConfig.pushString);
}
public static void reportLog(@NotNull String report) {
if (!checkPlayServices()) return;
UIUtil.runOnIoDispatcher(() -> FirebaseCrashlytics.getInstance().log(report));
}
public static void recordException(@NotNull Throwable throwable) {
if (!checkPlayServices()) return;
UIUtil.runOnIoDispatcher(() -> FirebaseCrashlytics.getInstance().recordException(throwable));
}
public static void checkUpdate(Activity ctx) {
if (!checkPlayServices()) return;

View File

@ -2,7 +2,13 @@ package tw.nekomimi.nekogram.database
import android.content.SharedPreferences
import org.dizitart.no2.*
import org.dizitart.no2.filters.Filters
import org.dizitart.no2.collection.Document
import org.dizitart.no2.collection.NitriteCollection
import org.dizitart.no2.collection.UpdateOptions
import org.dizitart.no2.filters.Filter
import org.dizitart.no2.filters.FluentFilter.where
import org.dizitart.no2.index.IndexOptions
import org.dizitart.no2.index.IndexType
import org.telegram.messenger.FileLog
import tw.nekomimi.nekogram.utils.UIUtil
@ -16,10 +22,10 @@ class DbPref(val connection: NitriteCollection) : SharedPreferences {
val listeners = LinkedHashSet<SharedPreferences.OnSharedPreferenceChangeListener>()
val isEmpty get() = connection.find(FindOptions.limit(0, 1)).count() == 0
val isEmpty get() = connection.find().limit(1).count() == 0
private inline fun <reified T> getAs(key: String, defValue: T): T {
connection.find(Filters.eq("key", key)).apply {
connection.find(where("key").eq(key)).apply {
runCatching {
return first().get("value", T::class.java)
}
@ -28,7 +34,7 @@ class DbPref(val connection: NitriteCollection) : SharedPreferences {
}
override fun contains(key: String): Boolean {
return connection.find(Filters.eq("key", key)).count() > 0
return connection.find(where("key").eq(key)).limit(1).count() > 0
}
override fun getBoolean(key: String, defValue: Boolean) = getAs(key, defValue)
@ -113,20 +119,20 @@ class DbPref(val connection: NitriteCollection) : SharedPreferences {
override fun commit(): Boolean {
try {
if (clear) {
connection.remove(Filters.ALL)
connection.remove(Filter.ALL)
} else {
toRemove.forEach {
connection.remove(Filters.eq("key", it))
connection.remove(where("key").eq(it))
}
}
toApply.forEach { (key, value) ->
if (value == null) {
connection.remove(Filters.eq("key", key))
connection.remove(where("key").eq(key))
} else {
connection.update(Filters.eq("key", key), Document().apply {
put("key", key)
put("value", value)
}, UpdateOptions.updateOptions(true))
connection.update(where("key").eq(key), Document.createDocument(mapOf(
"key" to key,
"value" to value
)), UpdateOptions.updateOptions(true))
}
}
return true

View File

@ -1,59 +1,33 @@
package tw.nekomimi.nekogram.database
import org.dizitart.no2.Nitrite
import org.dizitart.no2.mvstore.MVStoreModule
import org.telegram.messenger.ApplicationLoader
import org.telegram.messenger.FileLog
import tw.nekomimi.nekogram.utils.FileUtil
import java.io.File
fun mkDatabase(name: String): Nitrite {
File("${ApplicationLoader.getDataDirFixed()}/database").apply {
if (exists()) deleteRecursively()
}
val file = File("${ApplicationLoader.getDataDirFixed()}/databases/$name.db")
FileUtil.initDir(file.parentFile!!)
fun create() = Nitrite.builder()
.loadModule(MVStoreModule.withConfig()
.filePath(file)
.build())
.openOrCreate()!!
runCatching {
return Nitrite.builder().compressed()
.filePath(file.path)
.openOrCreate()!!
return create()
}.onFailure {
FileLog.e(it)
file.deleteRecursively()
}
return Nitrite.builder().compressed()
.filePath(file.path)
.openOrCreate()!!
}
fun mkCacheDatabase(name: String): Nitrite {
val file = File("${ApplicationLoader.getDataDirFixed()}/cache/$name.db")
FileUtil.initDir(file.parentFile!!)
runCatching {
return Nitrite.builder().compressed()
.filePath(file.path)
.openOrCreate()!!
}
file.deleteRecursively()
return Nitrite.builder().compressed()
.filePath(file.path)
.openOrCreate()!!
return create()
}
@ -64,9 +38,7 @@ private lateinit var mainSharedPreferencesDatabase: Nitrite
fun openMainSharedPreference(name: String): DbPref {
if (!::mainSharedPreferencesDatabase.isInitialized) {
mainSharedPreferencesDatabase = mkDatabase("shared_preferences")
}
return mainSharedPreferencesDatabase.openSharedPreference(name)

View File

@ -2,11 +2,11 @@ package tw.nekomimi.nekogram.sub;
import androidx.annotation.NonNull;
import org.dizitart.no2.Document;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.mapper.Mappable;
import org.dizitart.no2.mapper.NitriteMapper;
import org.dizitart.no2.objects.Id;
import org.dizitart.no2.objects.Index;
import org.dizitart.no2.repository.annotations.Id;
import org.dizitart.no2.repository.annotations.Index;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.R;
@ -116,7 +116,7 @@ public class SubInfo implements Mappable {
@Override
public Document write(NitriteMapper mapper) {
Document document = new Document();
Document document = Document.createDocument();
document.put("id", id);
document.put("name", name);

View File

@ -1,6 +1,6 @@
package tw.nekomimi.nekogram.sub
import org.dizitart.no2.objects.filters.ObjectFilters
import org.dizitart.no2.filters.FluentFilter.where
import org.telegram.messenger.LocaleController
import org.telegram.messenger.R
import tw.nekomimi.nekogram.database.mkDatabase
@ -9,14 +9,16 @@ object SubManager {
val database by lazy { mkDatabase("proxy_sub") }
@JvmStatic val count get() = subList.find().totalCount()
@JvmStatic
val count
get() = subList.find().count()
@JvmStatic
val subList by lazy {
database.getRepository("proxy_sub", SubInfo::class.java).apply {
database.getRepository(SubInfo::class.java, "proxy_sub").apply {
val public = find(ObjectFilters.eq("id", 1L)).firstOrDefault()
val public = find(where("id").eq(1L)).firstOrNull()
update(SubInfo().apply {

View File

@ -1,10 +1,10 @@
package tw.nekomimi.nekogram.transtale;
import org.dizitart.no2.Document;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.mapper.Mappable;
import org.dizitart.no2.mapper.NitriteMapper;
import org.dizitart.no2.objects.Id;
import org.dizitart.no2.objects.Index;
import org.dizitart.no2.repository.annotations.Id;
import org.dizitart.no2.repository.annotations.Index;
@Index("chatId")
public class ChatCCTarget implements Mappable {
@ -23,7 +23,7 @@ public class ChatCCTarget implements Mappable {
@Override
public Document write(NitriteMapper mapper) {
Document document = new Document();
Document document = Document.createDocument();
document.put("chatId", chatId);
document.put("ccTarget", ccTarget);
return document;

View File

@ -1,10 +1,11 @@
package tw.nekomimi.nekogram.transtale;
import org.dizitart.no2.Document;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.mapper.Mappable;
import org.dizitart.no2.mapper.NitriteMapper;
import org.dizitart.no2.objects.Id;
import org.dizitart.no2.objects.Index;
import org.dizitart.no2.repository.annotations.Id;
import org.dizitart.no2.repository.annotations.Index;
@Index("chatId")
public class ChatLanguage implements Mappable {
@ -23,7 +24,7 @@ public class ChatLanguage implements Mappable {
}
@Override public Document write(NitriteMapper mapper) {
Document document = new Document();
Document document = Document.createDocument();
document.put("chatId",chatId);
document.put("language",language);
return document;

View File

@ -1,12 +1,10 @@
package tw.nekomimi.nekogram.transtale;
import org.dizitart.no2.Document;
import org.dizitart.no2.IndexType;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.mapper.Mappable;
import org.dizitart.no2.mapper.NitriteMapper;
import org.dizitart.no2.objects.Id;
import org.dizitart.no2.objects.Index;
import org.dizitart.no2.objects.Indices;
import org.dizitart.no2.repository.annotations.Id;
import org.dizitart.no2.repository.annotations.Index;
@Index(value = "text")
public class TransItem implements Mappable {
@ -25,7 +23,7 @@ public class TransItem implements Mappable {
@Override
public Document write(NitriteMapper mapper) {
Document document = new Document();
Document document = Document.createDocument();
document.put("text",text);
document.put("trans", trans);
return document;

View File

@ -1,7 +1,7 @@
package tw.nekomimi.nekogram.transtale
import org.dizitart.no2.objects.ObjectRepository
import org.dizitart.no2.objects.filters.ObjectFilters
import org.dizitart.no2.filters.FluentFilter.where
import org.dizitart.no2.repository.ObjectRepository
import org.telegram.messenger.LocaleController
import tw.nekomimi.nekogram.NekoConfig
import tw.nekomimi.nekogram.database.mkDatabase
@ -11,19 +11,20 @@ import kotlin.collections.HashMap
class TranslateDb(val code: String) {
var conn: ObjectRepository<TransItem> = db.getRepository(code, TransItem::class.java)
var conn: ObjectRepository<TransItem> = db.getRepository(TransItem::class.java, code)
companion object {
val db = mkDatabase("translate_caches")
val repo = HashMap<Locale, TranslateDb>()
val chat = db.getRepository("chat", ChatLanguage::class.java)
val ccTarget = db.getRepository("opencc", ChatCCTarget::class.java)
val chat = db.getRepository(ChatLanguage::class.java, "chat")
val ccTarget = db.getRepository(ChatCCTarget::class.java, "cc")
@JvmStatic fun getChatLanguage(chatId: Int, default: Locale): Locale {
@JvmStatic
fun getChatLanguage(chatId: Int, default: Locale): Locale {
return chat.find(ObjectFilters.eq("chatId", chatId)).firstOrDefault()?.language?.code2Locale
return chat.find(where("chatId").eq(chatId)).firstOrNull()?.language?.code2Locale
?: default
}
@ -38,7 +39,7 @@ class TranslateDb(val code: String) {
@JvmStatic
fun getChatCCTarget(chatId: Int, default: String?): String? {
return ccTarget.find(ObjectFilters.eq("chatId", chatId)).firstOrDefault()?.ccTarget
return ccTarget.find(where("chatId").eq(chatId)).firstOrNull()?.ccTarget
?: default
}
@ -64,7 +65,7 @@ class TranslateDb(val code: String) {
fun clearAll() {
db.listRepositories()
.filter { it != "chat" }
.filter { it != "chat" }
.map { db.getCollection(it) }
.forEach { it.drop() }
@ -80,7 +81,7 @@ class TranslateDb(val code: String) {
}
fun contains(text: String) = synchronized(this) { conn.find(ObjectFilters.eq("text", text)).count() > 0 }
fun contains(text: String) = synchronized(this) { conn.find(where("text").eq(text)).count() > 0 }
fun save(text: String, trans: String) = synchronized<Unit>(this) {
@ -90,7 +91,7 @@ class TranslateDb(val code: String) {
fun query(text: String) = synchronized(this) {
conn.find(ObjectFilters.eq("text", text)).firstOrDefault()?.trans
conn.find(where("text").eq(text)).firstOrNull()?.trans
}

View File

@ -3,7 +3,6 @@ package tw.nekomimi.nekogram.transtale
import android.view.View
import cn.hutool.core.util.ArrayUtil
import cn.hutool.core.util.StrUtil
import org.apache.commons.lang3.LocaleUtils
import org.telegram.messenger.LocaleController
import org.telegram.messenger.R
import tw.nekomimi.nekogram.NekoConfig
@ -105,7 +104,8 @@ interface Translator {
val builder = PopupBuilder(anchor)
var locales = (if (full) LocaleUtils.availableLocaleList()
var locales = (if (full) Locale.getISOLanguages()
.map { Locale(it) }
.filter { it.variant.isBlank() } else LocaleController.getInstance()
.languages
.map { it.pluralLangCode }