Skip to content

Commit

Permalink
fix(core,data): build opencc dictionaries in the user data dir
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 10, 2023
1 parent 11b59b8 commit e17d5d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/core/Rime.java
Expand Up @@ -356,7 +356,7 @@ public static boolean selectSchema(String schemaId) {
public static Rime get(boolean full_check) {
if (self == null) {
if (full_check) {
OpenCCDictManager.internalDeploy();
OpenCCDictManager.buildOpenCCDict();
}
self = new Rime(full_check);
}
Expand Down
28 changes: 17 additions & 11 deletions app/src/main/java/com/osfans/trime/data/opencc/OpenCCDictManager.kt
Expand Up @@ -16,17 +16,23 @@ object OpenCCDictManager {
System.loadLibrary("rime_jni")
}

private val openccDictDir = File(
DataManager.getDataDir("opencc")
).also { it.mkdirs() }
val sharedDir = File(DataManager.sharedDataDir, "opencc").also { it.mkdirs() }
val userDir = File(DataManager.userDataDir, "opencc").also { it.mkdirs() }

fun dictionaries(): List<Dictionary> = openccDictDir
fun sharedDictionaries(): List<Dictionary> = sharedDir
.listFiles()
?.mapNotNull { Dictionary.new(it) }
?.toList() ?: listOf()
?.mapNotNull { Dictionary.new(it) } ?: listOf()

fun openccDictionaries(): List<OpenCCDictionary> =
dictionaries().mapNotNull { it as? OpenCCDictionary }
fun userDictionaries(): List<Dictionary> = userDir
.listFiles()
?.mapNotNull { Dictionary.new(it) } ?: listOf()

fun getAllDictionaries(): List<Dictionary> =
if (sharedDir.path == userDir.path) userDictionaries()
else (sharedDictionaries() + userDictionaries())

fun openCCDictionaries(): List<OpenCCDictionary> =
getAllDictionaries().mapNotNull { it as? OpenCCDictionary }

fun importFromFile(file: File): OpenCCDictionary {
val raw = Dictionary.new(file)
Expand All @@ -35,7 +41,7 @@ object OpenCCDictManager {
// preserve original file name
val new = raw.toOpenCCDictionary(
File(
openccDictDir,
userDir,
file.nameWithoutExtension + ".${Dictionary.Type.OPENCC.ext}"
)
)
Expand All @@ -47,8 +53,8 @@ object OpenCCDictManager {
* Convert internal text dict to opencc format
*/
@JvmStatic
fun internalDeploy() {
for (d in dictionaries()) {
fun buildOpenCCDict() {
for (d in getAllDictionaries()) {
if (d is TextDictionary) {
val result: OpenCCDictionary
measureTimeMillis {
Expand Down

0 comments on commit e17d5d0

Please sign in to comment.