Skip to content

Commit

Permalink
feat(data): implement data synchronization in DataManager
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Aug 6, 2022
1 parent 5b48bc2 commit 1781a59
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ android {
setProperty("archivesBaseName", "trime-$versionName")
resValue "string", "trime_url", getGitUrl()
buildConfigField 'String', 'BUILD_INFO' , '"' + getBuildInfo() + '"'
buildConfigField 'String', 'BUILD_VERSION' , '"' + "$versionName" + '-g'+ getGitVersion()+ '-' + getBuildDate() + '"'
buildConfigField 'String', 'BUILD_VERSION' , '"' + "$versionName" + '-g'+ gitHashShort() + '-' + getBuildDate() + '"'
buildConfigField 'String', 'BUILD_GIT_HASH', '"' + gitHashShort() + '"'
}

signingConfigs {
Expand Down Expand Up @@ -134,7 +135,7 @@ def getGitBranch(){
return text
}

def getGitVersion() {
static def gitHashShort() {
return 'git rev-parse --short HEAD'.execute().text.trim()
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ class AppPrefs(
companion object {
const val LAST_VERSION_NAME = "general__last_version_name"
const val PID = "general__pid"
const val LAST_BUILD_GIT_HASH = "general__last_build_git_hash"
}
var lastVersionName: String
get() = prefs.getPref(LAST_VERSION_NAME, "")
set(v) = prefs.setPref(LAST_VERSION_NAME, v)
var pid: Int
get() = prefs.getPref(PID, 0)
set(v) = prefs.setPref(PID, v)
var lastBuildGitHash: String
get() = prefs.getPref(LAST_BUILD_GIT_HASH, "")
set(v) = prefs.setPref(LAST_BUILD_GIT_HASH, v)
}

/**
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/com/osfans/trime/data/DataManager.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.osfans.trime.data

import com.blankj.utilcode.util.ResourceUtils
import com.osfans.trime.util.Const
import timber.log.Timber
import java.io.File

object DataManager {
private val prefs get() = AppPrefs.defaultInstance()

val sharedDataDir = File(prefs.conf.sharedDataDir)
val customDefault = File(sharedDataDir, "default.custom.yaml")

sealed class Diff {
object New : Diff()
object Update : Diff()
object Keep : Diff()
}

@JvmStatic
fun getDataDir(child: String = ""): String {
return if (File(prefs.conf.sharedDataDir, child).exists()) {
Expand All @@ -13,4 +25,39 @@ object DataManager {
File(prefs.conf.userDataDir, child).absolutePath
}
}

private fun diff(old: String, new: String): Diff {
return when {
old.isBlank() -> Diff.New
!new.contentEquals(old) -> Diff.Update
else -> Diff.Keep
}
}

@JvmStatic
fun sync() {
val newHash = Const.buildGitHash
val oldHash = prefs.general.lastBuildGitHash

diff(oldHash, newHash).run {
Timber.d("Diff: $this")
when (this) {
is Diff.New -> ResourceUtils.copyFileFromAssets(
"rime", sharedDataDir.absolutePath
)
is Diff.Update -> ResourceUtils.copyFileFromAssets(
"rime", sharedDataDir.absolutePath
)
is Diff.Keep -> {}
}
}

// FIXME:缺失 default.custom.yaml 会导致方案列表为空
if (!customDefault.exists()) {
Timber.d("Creating empty default.custom.yaml ...")
customDefault.createNewFile()
}

Timber.i("Synced!")
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/osfans/trime/util/Const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.osfans.trime.util

import com.osfans.trime.BuildConfig

object Const {
const val buildGitHash = BuildConfig.BUILD_GIT_HASH
}

0 comments on commit 1781a59

Please sign in to comment.