-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate UI creation for PreferenceDelegate
- Loading branch information
1 parent
b3406b5
commit 8a0fea7
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateEnum.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.data.prefs | ||
|
||
import androidx.annotation.StringRes | ||
|
||
interface PreferenceDelegateEnum { | ||
@get:StringRes | ||
val stringRes: Int | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
package com.osfans.trime.data.prefs | ||
|
||
import android.os.Bundle | ||
import androidx.annotation.CallSuper | ||
import androidx.preference.PreferenceScreen | ||
import com.osfans.trime.ui.components.PaddingPreferenceFragment | ||
|
||
abstract class PreferenceDelegateFragment( | ||
private val preferenceProvider: PreferenceDelegateProvider, | ||
) : PaddingPreferenceFragment() { | ||
open fun onPreferenceUiCreated(screen: PreferenceScreen) {} | ||
|
||
@CallSuper | ||
override fun onCreatePreferences( | ||
savedInstanceState: Bundle?, | ||
rootKey: String?, | ||
) { | ||
preferenceScreen = | ||
preferenceManager.createPreferenceScreen(preferenceManager.context).also { screen -> | ||
preferenceProvider.createUi(screen) | ||
onPreferenceUiCreated(screen) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
package com.osfans.trime.data.prefs | ||
|
||
import androidx.preference.PreferenceScreen | ||
|
||
abstract class PreferenceDelegateProvider { | ||
private val _preferenceDelegates: MutableMap<String, PreferenceDelegate<*>> = mutableMapOf() | ||
|
||
private val _preferenceDelegatesUi: MutableList<PreferenceDelegateUi<*>> = mutableListOf() | ||
|
||
val preferenceDelegates: Map<String, PreferenceDelegate<*>> | ||
get() = _preferenceDelegates | ||
|
||
val preferenceDelegatesUi: List<PreferenceDelegateUi<*>> | ||
get() = _preferenceDelegatesUi | ||
|
||
open fun createUi(screen: PreferenceScreen) { | ||
} | ||
|
||
fun PreferenceDelegateUi<*>.registerUi() { | ||
_preferenceDelegatesUi.add(this) | ||
} | ||
|
||
fun PreferenceDelegate<*>.register() { | ||
_preferenceDelegates[key] = this | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateUi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
package com.osfans.trime.data.prefs | ||
|
||
import android.content.Context | ||
import androidx.annotation.StringRes | ||
import androidx.preference.ListPreference | ||
import androidx.preference.Preference | ||
|
||
abstract class PreferenceDelegateUi<T : Preference>( | ||
val key: String, | ||
private val enableUiOn: (() -> Boolean)? = null, | ||
) { | ||
abstract fun createUi(context: Context): T | ||
|
||
fun isEnabled() = enableUiOn?.invoke() ?: true | ||
|
||
class StringList<T : Any>( | ||
@StringRes | ||
val title: Int, | ||
key: String, | ||
val defaultValue: T, | ||
val serializer: PreferenceDelegate.Serializer<T>, | ||
val entryValues: List<T>, | ||
@StringRes | ||
val entryLabels: List<Int>, | ||
enableUiOn: (() -> Boolean)? = null, | ||
) : PreferenceDelegateUi<ListPreference>(key, enableUiOn) { | ||
override fun createUi(context: Context) = | ||
ListPreference(context).apply { | ||
key = this@StringList.key | ||
isIconSpaceReserved = false | ||
isSingleLineTitle = false | ||
entryValues = this@StringList.entryValues.map { serializer.serialize(it) }.toTypedArray() | ||
summaryProvider = ListPreference.SimpleSummaryProvider.getInstance() | ||
setDefaultValue(serializer.serialize(defaultValue)) | ||
setTitle(this@StringList.title) | ||
entries = this@StringList.entryLabels.map { context.getString(it) }.toTypedArray() | ||
setDialogTitle(this@StringList.title) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters