Skip to content

Commit c4b8ddd

Browse files
committed
Remove Room.
1 parent b927ad3 commit c4b8ddd

File tree

11 files changed

+232
-650
lines changed

11 files changed

+232
-650
lines changed

build.gradle

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
buildscript {
3-
repositories {
4-
google()
5-
mavenCentral()
6-
}
7-
dependencies {
8-
classpath "com.android.tools.build:gradle:7.3.1"
9-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23"
10-
11-
// NOTE: Do not place your application dependencies here; they belong
12-
// in the individual module build.gradle files
13-
}
1+
// Top-level build file using plugins DSL
2+
plugins {
3+
id 'com.android.library' version '8.11.0' apply false
4+
id 'org.jetbrains.kotlin.android' version '2.2.0' apply false
145
}
156

167
task clean(type: Delete) {
17-
delete rootProject.buildDir
8+
delete rootProject.buildDir
189
}

library/build.gradle

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
apply plugin: 'com.android.library'
2-
apply plugin: 'kotlin-android'
3-
apply plugin: 'kotlin-parcelize'
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
id 'kotlin-parcelize'
5+
}
46

57
apply from: "${rootDir.path}/gradle/publish.gradle"
68

79
android {
8-
compileSdk 34
9-
namespace "com.absinthe.lc.rulesbundle"
10-
11-
defaultConfig {
12-
minSdk 21
13-
targetSdk 33
14-
versionCode 1
15-
versionName "1.0"
16-
}
17-
18-
buildTypes {
19-
release {
20-
minifyEnabled false
21-
shrinkResources false
22-
proguardFiles 'proguard-rules.pro'
10+
namespace "com.absinthe.lc.rulesbundle"
11+
compileSdk = 36
12+
13+
defaultConfig {
14+
minSdk = 21
15+
//noinspection OldTargetApi,EditedTargetSdkVersion
16+
targetSdk = 34
17+
versionCode = 1
18+
versionName = "1.0"
2319
}
24-
}
2520

26-
compileOptions {
27-
sourceCompatibility JavaVersion.VERSION_11
28-
targetCompatibility JavaVersion.VERSION_11
29-
}
21+
buildTypes {
22+
release {
23+
minifyEnabled = false
24+
shrinkResources = false
25+
proguardFiles 'proguard-rules.pro'
26+
}
27+
}
3028

31-
kotlinOptions {
32-
jvmTarget = '11'
33-
}
29+
compileOptions {
30+
sourceCompatibility = JavaVersion.VERSION_11
31+
targetCompatibility = JavaVersion.VERSION_11
32+
}
33+
34+
kotlinOptions {
35+
jvmTarget = '11'
36+
}
3437
}
3538

3639
dependencies {
37-
implementation 'androidx.appcompat:appcompat:1.6.1'
38-
implementation 'androidx.core:core-ktx:1.12.0'
39-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0'
40+
implementation 'androidx.appcompat:appcompat:1.7.1'
41+
implementation 'androidx.core:core-ktx:1.16.0'
42+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2'
4043

41-
implementation 'androidx.room:room-runtime:2.6.1'
42-
implementation 'androidx.room:room-ktx:2.6.1'
43-
compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
44+
compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
4445
}

library/src/main/java/com/absinthe/rulesbundle/LCRules.kt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ object LCRules : IAPI {
1515

1616
fun init(context: Context) {
1717
Repositories.checkRulesDatabase(context)
18-
contextRef = WeakReference(context)
19-
ruleRepo = RuleRepository(RuleDatabase.getDatabase(context).ruleDao())
18+
contextRef = WeakReference(context.applicationContext)
19+
ruleRepo = RuleRepository(context.applicationContext)
20+
}
21+
22+
fun closeDb() {
23+
ruleRepo?.closeDb()
2024
}
2125

2226
override fun getVersion(): Int {
@@ -50,17 +54,21 @@ object LCRules : IAPI {
5054
override fun getRulesAssetPath(): String = "lcrules/rules.db"
5155

5256
override suspend fun getRule(libName: String, @LibType type: Int, useRegex: Boolean): Rule? {
53-
ruleRepo?.getRule(libName)?.let {
57+
val repo = ruleRepo ?: return null
58+
val entity = repo.getRule(libName)
59+
if (entity != null) {
5460
return Rule(
55-
it.label,
56-
IconResMap.getIconRes(it.iconIndex),
57-
getDescriptionUrl(it),
58-
it.regexName,
59-
IconResMap.isSingleColorIcon(it.iconIndex)
61+
entity.label,
62+
IconResMap.getIconRes(entity.iconIndex),
63+
getDescriptionUrl(entity),
64+
entity.regexName,
65+
IconResMap.isSingleColorIcon(entity.iconIndex)
6066
)
6167
}
6268
if (useRegex) {
63-
findRuleRegex(libName, type)?.let {
69+
val regexMap = repo.getRegexRules()
70+
val match = regexMap.entries.firstOrNull { it.key.matcher(libName).matches() && it.value.type == type }
71+
match?.value?.let {
6472
return Rule(
6573
it.label,
6674
IconResMap.getIconRes(it.iconIndex),
@@ -99,17 +107,4 @@ object LCRules : IAPI {
99107
val dir = dirMap[entity.type] ?: return null
100108
return "$baseUrl$dir/${entity.name}.json"
101109
}
102-
103-
private fun findRuleRegex(string: String, @LibType type: Int): RuleEntity? {
104-
RuleRepository.regexRules?.let {
105-
val iterator = it.entries.iterator()
106-
while (iterator.hasNext()) {
107-
val entry = iterator.next()
108-
if (entry.key.matcher(string).matches() && entry.value.type == type) {
109-
return entry.value
110-
}
111-
}
112-
}
113-
return null
114-
}
115110
}

library/src/main/java/com/absinthe/rulesbundle/Repositories.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ import java.io.File
77

88
object Repositories {
99

10-
const val RULES_DATABASE_NAME = "rules_database"
10+
const val RULES_DATABASE_NAME = "lcrules_database"
1111
private const val LOCAL_RULES_VERSION_FILE = "lcrules/version"
1212

1313
fun checkRulesDatabase(context: Context) {
14+
val dbFile = context.getDatabasePath(RULES_DATABASE_NAME)
15+
if (!dbFile.exists()) {
16+
// On first launch, copy the prebuilt database from assets
17+
context.assets.open(LCRules.getRulesAssetPath()).use { input ->
18+
dbFile.parentFile?.mkdirs()
19+
dbFile.outputStream().use { output ->
20+
input.copyTo(output)
21+
}
22+
}
23+
}
1424
if (!checkMd5(context) && !checkVersion(context)) {
1525
deleteRulesDatabase(context)
1626
}
Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,116 @@
11
package com.absinthe.rulesbundle
22

3+
import android.content.ContentValues
4+
import android.content.Context
35
import android.database.Cursor
4-
import androidx.room.Dao
5-
import androidx.room.Insert
6-
import androidx.room.OnConflictStrategy
7-
import androidx.room.Query
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.withContext
88

9-
@Dao
10-
interface RuleDao {
11-
// Rules
12-
@Insert(onConflict = OnConflictStrategy.REPLACE)
13-
suspend fun insertRules(items: List<RuleEntity>)
9+
class RuleDao(context: Context) {
10+
private val dbHelper = RuleDatabaseHelper(context)
1411

15-
@Query("DELETE FROM rules_table")
16-
fun deleteAllRules()
12+
fun closeDb() {
13+
dbHelper.close()
14+
}
1715

18-
@Query("SELECT * from rules_table WHERE name LIKE :name")
19-
suspend fun getRule(name: String): RuleEntity?
16+
suspend fun insertRules(items: List<RuleEntity>) = withContext(Dispatchers.IO) {
17+
val db = dbHelper.writableDatabase
18+
db.beginTransaction()
19+
try {
20+
for (item in items) {
21+
val values = ContentValues().apply {
22+
put("_id", item.id)
23+
put("name", item.name)
24+
put("label", item.label)
25+
put("type", item.type)
26+
put("iconIndex", item.iconIndex)
27+
put("isRegexRule", if (item.isRegexRule) 1 else 0)
28+
put("regexName", item.regexName)
29+
}
30+
db.insertWithOnConflict("rules_table", null, values, android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE)
31+
}
32+
db.setTransactionSuccessful()
33+
} finally {
34+
db.endTransaction()
35+
db.close()
36+
}
37+
}
2038

21-
@Query("SELECT * from rules_table")
22-
suspend fun getAllRules(): List<RuleEntity>
39+
fun deleteAllRules() {
40+
val db = dbHelper.writableDatabase
41+
db.delete("rules_table", null, null)
42+
db.close()
43+
}
2344

24-
@Query("SELECT * from rules_table WHERE isRegexRule = 1")
25-
suspend fun getRegexRules(): List<RuleEntity>
45+
suspend fun getRule(name: String): RuleEntity? = withContext(Dispatchers.IO) {
46+
val db = dbHelper.readableDatabase
47+
db.query(
48+
"rules_table",
49+
null,
50+
"name LIKE ?",
51+
arrayOf(name),
52+
null, null, null
53+
).use { cursor ->
54+
val entity = if (cursor.moveToFirst()) cursorToEntity(cursor) else null
55+
db.close()
56+
entity
57+
}
58+
}
2659

27-
@Query("SELECT * FROM rules_table")
28-
fun selectAllRules(): Cursor?
60+
suspend fun getAllRules(): List<RuleEntity> = withContext(Dispatchers.IO) {
61+
val db = dbHelper.readableDatabase
62+
db.query("rules_table", null, null, null, null, null, null).use { cursor ->
63+
val list = mutableListOf<RuleEntity>()
64+
while (cursor.moveToNext()) {
65+
list.add(cursorToEntity(cursor))
66+
}
67+
db.close()
68+
list
69+
}
70+
}
2971

30-
@Query("SELECT * FROM rules_table WHERE name LIKE :name")
31-
fun selectRuleByName(name: String): Cursor?
72+
suspend fun getRegexRules(): List<RuleEntity> = withContext(Dispatchers.IO) {
73+
val db = dbHelper.readableDatabase
74+
db.query(
75+
"rules_table",
76+
null,
77+
"isRegexRule = 1",
78+
null, null, null, null
79+
).use { cursor ->
80+
val list = mutableListOf<RuleEntity>()
81+
while (cursor.moveToNext()) {
82+
list.add(cursorToEntity(cursor))
83+
}
84+
db.close()
85+
list
86+
}
87+
}
88+
89+
fun selectAllRules(): Cursor? {
90+
val db = dbHelper.readableDatabase
91+
return db.query("rules_table", null, null, null, null, null, null)
92+
}
93+
94+
fun selectRuleByName(name: String): Cursor? {
95+
val db = dbHelper.readableDatabase
96+
return db.query(
97+
"rules_table",
98+
null,
99+
"name LIKE ?",
100+
arrayOf(name),
101+
null, null, null
102+
)
103+
}
104+
105+
private fun cursorToEntity(cursor: Cursor): RuleEntity {
106+
return RuleEntity(
107+
id = cursor.getInt(cursor.getColumnIndexOrThrow("_id")),
108+
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
109+
label = cursor.getString(cursor.getColumnIndexOrThrow("label")),
110+
type = cursor.getInt(cursor.getColumnIndexOrThrow("type")),
111+
iconIndex = cursor.getInt(cursor.getColumnIndexOrThrow("iconIndex")),
112+
isRegexRule = cursor.getInt(cursor.getColumnIndexOrThrow("isRegexRule")) == 1,
113+
regexName = cursor.getString(cursor.getColumnIndexOrThrow("regexName"))
114+
)
115+
}
32116
}

0 commit comments

Comments
 (0)