Skip to content

Commit

Permalink
fix: data dir preferences didn't show default value on init
Browse files Browse the repository at this point in the history
Use Android native API instead of utilcode method to get external storage dir path.
  • Loading branch information
WhiredPlanck committed May 3, 2024
1 parent 6199d30 commit 5dc3eb8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 56 deletions.
8 changes: 3 additions & 5 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package com.osfans.trime.data
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.blankj.utilcode.util.PathUtils
import com.osfans.trime.R
import com.osfans.trime.data.base.DataManager
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.keyboard.KeyboardPrefs
Expand Down Expand Up @@ -347,15 +347,13 @@ class AppPrefs(
const val TIMING_SYNC_TRIGGER_TIME = "profile_timing_sync_trigger_time"
const val LAST_SYNC_STATUS = "profile_last_sync_status"
const val LAST_BACKGROUND_SYNC = "profile_last_background_sync"

private fun getExternalPathPrefix() = PathUtils.getExternalStoragePath()
}

var sharedDataDir: String
get() = prefs.getPref(SHARED_DATA_DIR, "${getExternalPathPrefix()}/rime")
get() = prefs.getPref(SHARED_DATA_DIR, DataManager.defaultDataDirectory.path)
set(v) = prefs.setPref(SHARED_DATA_DIR, v)
var userDataDir: String
get() = prefs.getPref(USER_DATA_DIR, "${getExternalPathPrefix()}/rime")
get() = prefs.getPref(USER_DATA_DIR, DataManager.defaultDataDirectory.path)
set(v) = prefs.setPref(USER_DATA_DIR, v)
var syncBackgroundEnabled: Boolean
get() = prefs.getPref(SYNC_BACKGROUND_ENABLED, false)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/base/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package com.osfans.trime.data.base

import android.content.res.AssetManager
import android.os.Build
import com.blankj.utilcode.util.PathUtils
import android.os.Environment
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.util.FileUtils
import com.osfans.trime.util.ResourceUtils
Expand Down Expand Up @@ -50,7 +50,7 @@ object DataManager {

private val prefs get() = AppPrefs.defaultInstance()

val defaultDataDirectory = File(PathUtils.getExternalStoragePath(), "rime")
val defaultDataDirectory = File(Environment.getExternalStorageDirectory(), "rime")

private val onDataDirChangeListeners = WeakHashSet<OnDataDirChangeListener>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,80 @@ import com.osfans.trime.R
import com.osfans.trime.databinding.FolderPickerDialogBinding
import java.io.File

class FolderPickerPreference : Preference {
private var value = ""
lateinit var documentTreeLauncher: ActivityResultLauncher<Uri?>
lateinit var dialogView: FolderPickerDialogBinding
class FolderPickerPreference
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle,
) : Preference(context, attrs, defStyleAttr) {
private var value = ""
lateinit var documentTreeLauncher: ActivityResultLauncher<Uri?>
lateinit var dialogView: FolderPickerDialogBinding

@Suppress("unused")
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) :
this(context, attrs, androidx.preference.R.attr.preferenceStyle)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
context.theme.obtainStyledAttributes(attrs, R.styleable.FolderPickerPreferenceAttrs, 0, 0).run {
try {
if (getBoolean(R.styleable.FolderPickerPreferenceAttrs_useSimpleSummaryProvider, false)) {
summaryProvider = SummaryProvider<FolderPickerPreference> { it.currentValue }
var default = ""

init {
context.theme.obtainStyledAttributes(attrs, R.styleable.FolderPickerPreferenceAttrs, 0, 0).run {
try {
if (getBoolean(R.styleable.FolderPickerPreferenceAttrs_useSimpleSummaryProvider, false)) {
summaryProvider = SummaryProvider<FolderPickerPreference> { it.value }
}
} finally {
recycle()
}
} finally {
recycle()
}
}
}

private val currentValue: String
get() = getPersistedString(value)

override fun onGetDefaultValue(
a: TypedArray,
index: Int,
): Any {
return a.getString(index) ?: ""
}
override fun persistString(value: String): Boolean {
return super.persistString(value).also {
if (it) this.value = value
}
}

override fun onSetInitialValue(defaultValue: Any?) {
value = defaultValue as? String ?: getPersistedString("")
}
override fun setDefaultValue(defaultValue: Any?) {
super.setDefaultValue(defaultValue)
default = defaultValue as? String ?: ""
}

override fun onClick() = showPickerDialog()
override fun onGetDefaultValue(
a: TypedArray,
index: Int,
): Any {
return a.getString(index) ?: default
}

private fun showPickerDialog() {
val initValue = currentValue
dialogView = FolderPickerDialogBinding.inflate(LayoutInflater.from(context))
dialogView.editText.setText(initValue)
dialogView.button.setOnClickListener {
documentTreeLauncher.launch(UriUtils.file2Uri(File(initValue)))
override fun onSetInitialValue(defaultValue: Any?) {
value = getPersistedString(defaultValue as? String ?: default)
}
AlertDialog.Builder(context)
.setTitle(this@FolderPickerPreference.title)
.setView(dialogView.root)
.setPositiveButton(android.R.string.ok) { _, _ ->
val value = dialogView.editText.text.toString()
if (callChangeListener(value)) {
persistString(value)
notifyChanged()
}

override fun onClick() = showPickerDialog()

private fun showPickerDialog() {
val initValue = value
dialogView = FolderPickerDialogBinding.inflate(LayoutInflater.from(context))
dialogView.editText.setText(initValue)
dialogView.button.setOnClickListener {
documentTreeLauncher.launch(UriUtils.file2Uri(File(initValue)))
}
.setNeutralButton(R.string.pref__default) { _, _ ->
AlertDialog.Builder(context)
.setTitle(this@FolderPickerPreference.title)
.setView(dialogView.root)
.setPositiveButton(android.R.string.ok) { _, _ ->
val value = dialogView.editText.text.toString()
setValue(value)
}
.setNeutralButton(R.string.pref__default) { _, _ ->
setValue(default)
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}

private fun setValue(value: String) {
if (callChangeListener(value)) {
persistString(value)
notifyChanged()
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ SPDX-License-Identifier: GPL-3.0-or-later
<declare-styleable name="FolderPickerPreferenceAttrs">
<attr name="useSimpleSummaryProvider" />
</declare-styleable>
</resources>
</resources>

0 comments on commit 5dc3eb8

Please sign in to comment.