Skip to content

Commit a73a5f9

Browse files
committed
Add setLocalRulesVersion and getLocalRulesVersion .
1 parent 7525ee8 commit a73a5f9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
0 Bytes
Binary file not shown.

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,30 @@ import java.io.File
88
object Repositories {
99

1010
const val RULES_DATABASE_NAME = "rules_database"
11+
private const val LOCAL_RULES_VERSION_FILE = "lcrules/version"
1112

1213
fun checkRulesDatabase(context: Context) {
1314
if (!checkMd5(context) && !checkVersion(context)) {
1415
deleteRulesDatabase(context)
1516
}
1617
}
1718

19+
fun getLocalRulesVersion(context: Context): Int {
20+
val localCloudVersionFile = File(context.filesDir, LOCAL_RULES_VERSION_FILE)
21+
if (localCloudVersionFile.exists().not()) return LCRules.getVersion()
22+
if (localCloudVersionFile.isDirectory.not()) return LCRules.getVersion()
23+
localCloudVersionFile.listFiles()?.takeIf { it.isNotEmpty() }?.let {
24+
return runCatching { it[0].name.toInt() }.getOrDefault(LCRules.getVersion())
25+
} ?: return LCRules.getVersion()
26+
}
27+
28+
fun setLocalRulesVersion(context: Context, version: Int): Boolean {
29+
val localCloudVersionFile = File(context.filesDir, LOCAL_RULES_VERSION_FILE)
30+
if (localCloudVersionFile.isDirectory.not()) localCloudVersionFile.delete()
31+
if (localCloudVersionFile.exists().not()) localCloudVersionFile.mkdirs()
32+
return File(localCloudVersionFile, version.toString()).createNewFile()
33+
}
34+
1835
private fun deleteRulesDatabase(context: Context) {
1936
val databaseDir = context.getDatabasePath(RULES_DATABASE_NAME).parent
2037
FileUtils.delete(File(databaseDir, RULES_DATABASE_NAME))
@@ -34,7 +51,7 @@ object Repositories {
3451
}
3552

3653
private fun checkVersion(context: Context): Boolean {
37-
val versionFile = File(context.filesDir, "lcrules/version")
54+
val versionFile = File(context.filesDir, LOCAL_RULES_VERSION_FILE)
3855
if (versionFile.exists().not()) return false
3956
if (versionFile.isDirectory.not()) return false
4057
versionFile.listFiles()?.takeIf { it.isNotEmpty() }?.let {

0 commit comments

Comments
 (0)