@@ -3,6 +3,66 @@ package me.ash.reader.infrastructure.preference
33import android.content.Context
44import androidx.datastore.preferences.core.Preferences
55import kotlinx.coroutines.CoroutineScope
6+ import kotlinx.coroutines.launch
7+ import me.ash.reader.ui.ext.PreferenceKey
8+ import org.json.JSONObject
9+
10+ sealed interface AppPreference {
11+ val value: Any
12+ val key: PreferenceKey
13+
14+ interface IntPreference : AppPreference {
15+ override val value: Int
16+ override val key: PreferenceKey .IntKey
17+ }
18+
19+ interface LongPreference : AppPreference {
20+ override val value: Long
21+ override val key: PreferenceKey .LongKey
22+ }
23+
24+ interface StringPreference : AppPreference {
25+ override val value: String
26+ override val key: PreferenceKey .StringKey
27+ }
28+
29+ interface BooleanPreference : AppPreference {
30+ override val value: Boolean
31+ override val key: PreferenceKey .BooleanKey
32+ }
33+
34+ interface FloatPreference : AppPreference {
35+ override val value: Float
36+ override val key: PreferenceKey .FloatKey
37+ }
38+
39+ interface PreferenceCompanion {
40+ val key: PreferenceKey
41+ val default: AppPreference
42+
43+ fun fromPreferences (preferences : Preferences ): AppPreference
44+
45+ val values: List <AppPreference >
46+ }
47+
48+ sealed interface Editable {
49+ suspend fun put (context : Context )
50+
51+ @Deprecated(" Use the suspend function instead" )
52+ fun put (context : Context , coroutineScope : CoroutineScope ) =
53+ coroutineScope.launch { put(context) }
54+ }
55+ }
56+
57+ fun JSONObject.put (preference : AppPreference ) {
58+ when (preference) {
59+ is AppPreference .BooleanPreference -> put(preference.key.name, preference.value)
60+ is AppPreference .FloatPreference -> put(preference.key.name, preference.value)
61+ is AppPreference .IntPreference -> put(preference.key.name, preference.value)
62+ is AppPreference .LongPreference -> put(preference.key.name, preference.value)
63+ is AppPreference .StringPreference -> put(preference.key.name, preference.value)
64+ }
65+ }
666
767sealed class Preference {
868
@@ -44,11 +104,11 @@ fun Preferences.toSettings(): Settings {
44104 flowArticleListImage = FlowArticleListImagePreference .fromPreferences(this ),
45105 flowArticleListDesc = FlowArticleListDescPreference .fromPreferences(this ),
46106 flowArticleListTime = FlowArticleListTimePreference .fromPreferences(this ),
47- flowArticleListDateStickyHeader = FlowArticleListDateStickyHeaderPreference .fromPreferences(
48- this
49- ),
107+ flowArticleListDateStickyHeader =
108+ FlowArticleListDateStickyHeaderPreference .fromPreferences(this ),
50109 flowArticleListReadIndicator = FlowArticleReadIndicatorPreference .fromPreferences(this ),
51- flowArticleListTonalElevation = FlowArticleListTonalElevationPreference .fromPreferences(this ),
110+ flowArticleListTonalElevation =
111+ FlowArticleListTonalElevationPreference .fromPreferences(this ),
52112 flowSortUnreadArticles = SortUnreadArticlesPreference .fromPreferences(this ),
53113
54114 // Reading page
@@ -70,7 +130,8 @@ fun Preferences.toSettings(): Settings {
70130 readingSubheadBold = ReadingSubheadBoldPreference .fromPreferences(this ),
71131 readingTitleUpperCase = ReadingTitleUpperCasePreference .fromPreferences(this ),
72132 readingSubheadUpperCase = ReadingSubheadUpperCasePreference .fromPreferences(this ),
73- readingImageHorizontalPadding = ReadingImageHorizontalPaddingPreference .fromPreferences(this ),
133+ readingImageHorizontalPadding =
134+ ReadingImageHorizontalPaddingPreference .fromPreferences(this ),
74135 readingImageRoundedCorners = ReadingImageRoundedCornersPreference .fromPreferences(this ),
75136 readingImageMaximize = ReadingImageMaximizePreference .fromPreferences(this ),
76137
0 commit comments