Skip to content

Commit ef78335

Browse files
committed
fix: error on access to user data dir especially on app first run
1 parent 0555fe2 commit ef78335

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

app/src/main/java/com/osfans/trime/data/base/DataManager.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ object DataManager {
7070
onDataDirChangeListeners.forEach { it.onDataDirChange() }
7171
}
7272

73-
@JvmStatic
7473
val sharedDataDir = File(appContext.getExternalFilesDir(null), "shared").also { it.mkdirs() }
7574

76-
@JvmStatic
7775
val userDataDir
78-
get() = File(prefs.profile.userDataDir)
76+
get() =
77+
File(prefs.profile.userDataDir).also {
78+
if (!it.exists()) it.mkdirs()
79+
}
7980

8081
/**
8182
* Return the absolute path of the compiled config file
@@ -122,12 +123,11 @@ object DataManager {
122123
ResourceUtils.copyFile(DATA_CHECKSUMS_NAME, dataDir)
123124

124125
// FIXME:缺失 default.custom.yaml 会导致方案列表为空
125-
File(userDataDir, DEFAULT_CUSTOM_FILE_NAME).let {
126-
if (!it.exists()) {
126+
runCatching {
127+
if (File(userDataDir, DEFAULT_CUSTOM_FILE_NAME).createNewFile()) {
127128
Timber.d("Creating empty default.custom.yaml")
128-
it.bufferedWriter().use { w -> w.write("") }
129129
}
130-
}
130+
}.getOrElse { Timber.e(it, "Failed to create default.custom.yaml") }
131131

132132
Timber.d("Synced!")
133133
}

0 commit comments

Comments
 (0)