Remove preferences delegates

pull/107/head
Koitharu 4 years ago
parent 14be8d4936
commit 94e9fa35e2
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.prefs
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.Settings import android.provider.Settings
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
@ -13,106 +14,113 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import org.koitharu.kotatsu.core.model.ZoomMode import org.koitharu.kotatsu.core.model.ZoomMode
import org.koitharu.kotatsu.utils.delegates.prefs.* import org.koitharu.kotatsu.utils.ext.toUriOrNull
import java.io.File import java.io.File
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
class AppSettings private constructor(private val prefs: SharedPreferences) : class AppSettings(context: Context) {
SharedPreferences by prefs {
constructor(context: Context) : this( private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
PreferenceManager.getDefaultSharedPreferences(context)
)
var listMode by EnumPreferenceDelegate( var listMode: ListMode
ListMode::class.java, get() = prefs.getString(KEY_LIST_MODE, null)?.findEnumValue(ListMode.values()) ?: ListMode.DETAILED_LIST
KEY_LIST_MODE, set(value) = prefs.edit { putString(KEY_LIST_MODE, value.name) }
ListMode.DETAILED_LIST
)
var defaultSection by IntEnumPreferenceDelegate( var defaultSection: AppSection
AppSection::class.java, get() = prefs.getString(KEY_APP_SECTION, null)?.findEnumValue(AppSection.values()) ?: AppSection.HISTORY
KEY_APP_SECTION, set(value) = prefs.edit { putString(KEY_APP_SECTION, value.name) }
AppSection.HISTORY
)
val theme by StringIntPreferenceDelegate( val theme: Int
KEY_THEME, get() = prefs.getString(KEY_THEME, null)?.toIntOrNull() ?: AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
)
val isDynamicTheme by BoolPreferenceDelegate(KEY_DYNAMIC_THEME, defaultValue = false) val isDynamicTheme: Boolean
get() = prefs.getBoolean(KEY_DYNAMIC_THEME, false)
val isAmoledTheme by BoolPreferenceDelegate(KEY_THEME_AMOLED, defaultValue = false) val isAmoledTheme: Boolean
get() = prefs.getBoolean(KEY_THEME_AMOLED, false)
val isToolbarHideWhenScrolling by BoolPreferenceDelegate(KEY_HIDE_TOOLBAR, defaultValue = true) val isToolbarHideWhenScrolling: Boolean
get() = prefs.getBoolean(KEY_HIDE_TOOLBAR, true)
var gridSize by IntPreferenceDelegate(KEY_GRID_SIZE, defaultValue = 100) var gridSize: Int
get() = prefs.getInt(KEY_GRID_SIZE, 100)
set(value) = prefs.edit { putInt(KEY_GRID_SIZE, value) }
val readerPageSwitch by StringSetPreferenceDelegate( val readerPageSwitch: Set<String>
KEY_READER_SWITCHERS, get() = prefs.getStringSet(KEY_READER_SWITCHERS, null) ?: setOf(PAGE_SWITCH_TAPS)
arraySetOf(PAGE_SWITCH_TAPS)
)
var isTrafficWarningEnabled by BoolPreferenceDelegate(KEY_TRAFFIC_WARNING, defaultValue = true) var isTrafficWarningEnabled: Boolean
get() = prefs.getBoolean(KEY_TRAFFIC_WARNING, true)
set(value) = prefs.edit { putBoolean(KEY_TRAFFIC_WARNING, value) }
val appUpdateAuto by BoolPreferenceDelegate(KEY_APP_UPDATE_AUTO, defaultValue = true) val appUpdateAuto: Boolean
get() = prefs.getBoolean(KEY_APP_UPDATE_AUTO, true)
var appUpdate by LongPreferenceDelegate(KEY_APP_UPDATE, defaultValue = 0L) var appUpdate: Long
get() = prefs.getLong(KEY_APP_UPDATE, 0L)
set(value) = prefs.edit { putLong(KEY_APP_UPDATE, value) }
val trackerNotifications by BoolPreferenceDelegate( val trackerNotifications: Boolean
KEY_TRACKER_NOTIFICATIONS, get() = prefs.getBoolean(KEY_TRACKER_NOTIFICATIONS, true)
defaultValue = true
)
var notificationSound by StringPreferenceDelegate( var notificationSound: Uri
KEY_NOTIFICATIONS_SOUND, get() = prefs.getString(KEY_NOTIFICATIONS_SOUND, null)?.toUriOrNull()
Settings.System.DEFAULT_NOTIFICATION_URI.toString() ?: Settings.System.DEFAULT_NOTIFICATION_URI
) set(value) = prefs.edit { putString(KEY_NOTIFICATIONS_SOUND, value.toString()) }
val notificationVibrate by BoolPreferenceDelegate(KEY_NOTIFICATIONS_VIBRATE, false) val notificationVibrate: Boolean
get() = prefs.getBoolean(KEY_NOTIFICATIONS_VIBRATE, false)
val notificationLight by BoolPreferenceDelegate(KEY_NOTIFICATIONS_LIGHT, true) val notificationLight: Boolean
get() = prefs.getBoolean(KEY_NOTIFICATIONS_LIGHT, true)
val readerAnimation by BoolPreferenceDelegate(KEY_READER_ANIMATION, false) val readerAnimation: Boolean
get() = prefs.getBoolean(KEY_READER_ANIMATION, false)
val isPreferRtlReader by BoolPreferenceDelegate(KEY_READER_PREFER_RTL, false) val isPreferRtlReader: Boolean
get() = prefs.getBoolean(KEY_READER_PREFER_RTL, false)
var historyGrouping by BoolPreferenceDelegate(KEY_HISTORY_GROUPING, true) var historyGrouping: Boolean
get() = prefs.getBoolean(KEY_HISTORY_GROUPING, true)
set(value) = prefs.edit { putBoolean(KEY_HISTORY_GROUPING, value) }
var isHistoryExcludeNsfw by BoolPreferenceDelegate(KEY_HISTORY_EXCLUDE_NSFW, false) val isHistoryExcludeNsfw: Boolean
get() = prefs.getBoolean(KEY_HISTORY_EXCLUDE_NSFW, false)
var chaptersReverse by BoolPreferenceDelegate(KEY_REVERSE_CHAPTERS, false) var chaptersReverse: Boolean
get() = prefs.getBoolean(KEY_REVERSE_CHAPTERS, false)
set(value) = prefs.edit { putBoolean(KEY_REVERSE_CHAPTERS, value) }
val zoomMode by EnumPreferenceDelegate( val zoomMode: ZoomMode
ZoomMode::class.java, get() = prefs.getString(KEY_ZOOM_MODE, null)?.findEnumValue(ZoomMode.values()) ?: ZoomMode.FIT_CENTER
KEY_ZOOM_MODE,
ZoomMode.FIT_CENTER
)
val trackSources by StringSetPreferenceDelegate( val trackSources: Set<String>
KEY_TRACK_SOURCES, get() = prefs.getStringSet(KEY_TRACK_SOURCES, null) ?: arraySetOf(TRACK_FAVOURITES, TRACK_HISTORY)
arraySetOf(TRACK_FAVOURITES, TRACK_HISTORY)
)
var appPassword by NullableStringPreferenceDelegate(KEY_APP_PASSWORD) var appPassword: String?
get() = prefs.getString(KEY_APP_PASSWORD, null)
private var sourcesOrderStr by NullableStringPreferenceDelegate(KEY_SOURCES_ORDER) set(value) = prefs.edit { if (value != null) putString(KEY_APP_PASSWORD, value) else remove(KEY_APP_PASSWORD) }
var sourcesOrder: List<Int> var sourcesOrder: List<Int>
get() = sourcesOrderStr?.split('|')?.mapNotNull(String::toIntOrNull).orEmpty() get() = prefs.getString(KEY_SOURCES_ORDER, null)
set(value) { ?.split('|')
sourcesOrderStr = value.joinToString("|") ?.mapNotNull(String::toIntOrNull)
.orEmpty()
set(value) = prefs.edit {
putString(KEY_SOURCES_ORDER, value.joinToString("|"))
} }
var hiddenSources by StringSetPreferenceDelegate(KEY_SOURCES_HIDDEN) var hiddenSources: Set<String>
get() = prefs.getStringSet(KEY_SOURCES_HIDDEN, null) ?: emptySet()
set(value) = prefs.edit { putStringSet(KEY_SOURCES_HIDDEN, value) }
val isSourcesSelected: Boolean val isSourcesSelected: Boolean
get() = KEY_SOURCES_HIDDEN in prefs get() = KEY_SOURCES_HIDDEN in prefs
val isPagesNumbersEnabled by BoolPreferenceDelegate(KEY_PAGES_NUMBERS, false) val isPagesNumbersEnabled: Boolean
get() = prefs.getBoolean(KEY_PAGES_NUMBERS, false)
fun getFallbackStorageDir(): File? { fun getFallbackStorageDir(): File? {
return prefs.getString(KEY_LOCAL_STORAGE, null)?.let { return prefs.getString(KEY_LOCAL_STORAGE, null)?.let {
@ -131,7 +139,7 @@ class AppSettings private constructor(private val prefs: SharedPreferences) :
} }
} }
fun dateFormat(format: String? = prefs.getString(KEY_DATE_FORMAT, "")): DateFormat = fun getDateFormat(format: String = prefs.getString(KEY_DATE_FORMAT, "").orEmpty()): DateFormat =
when (format) { when (format) {
"" -> DateFormat.getDateInstance(DateFormat.SHORT) "" -> DateFormat.getDateInstance(DateFormat.SHORT)
else -> SimpleDateFormat(format, Locale.getDefault()) else -> SimpleDateFormat(format, Locale.getDefault())
@ -156,6 +164,10 @@ class AppSettings private constructor(private val prefs: SharedPreferences) :
} }
} }
private fun <E : Enum<E>> String.findEnumValue(values: Array<E>): E? {
return values.find { it.name == this }
}
companion object { companion object {
const val PAGE_SWITCH_TAPS = "taps" const val PAGE_SWITCH_TAPS = "taps"
@ -165,7 +177,7 @@ class AppSettings private constructor(private val prefs: SharedPreferences) :
const val TRACK_FAVOURITES = "favourites" const val TRACK_FAVOURITES = "favourites"
const val KEY_LIST_MODE = "list_mode_2" const val KEY_LIST_MODE = "list_mode_2"
const val KEY_APP_SECTION = "app_section" const val KEY_APP_SECTION = "app_section_2"
const val KEY_THEME = "theme" const val KEY_THEME = "theme"
const val KEY_DYNAMIC_THEME = "dynamic_theme" const val KEY_DYNAMIC_THEME = "dynamic_theme"
const val KEY_THEME_AMOLED = "amoled_theme" const val KEY_THEME_AMOLED = "amoled_theme"
@ -215,7 +227,7 @@ class AppSettings private constructor(private val prefs: SharedPreferences) :
val isDynamicColorAvailable: Boolean val isDynamicColorAvailable: Boolean
get() = DynamicColors.isDynamicColorAvailable() || get() = DynamicColors.isDynamicColorAvailable() ||
(isSamsung && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) (isSamsung && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
private val isSamsung private val isSamsung
get() = Build.MANUFACTURER.equals("samsung", ignoreCase = true) get() = Build.MANUFACTURER.equals("samsung", ignoreCase = true)

@ -1,26 +1,15 @@
package org.koitharu.kotatsu.core.prefs package org.koitharu.kotatsu.core.prefs
import android.content.Context import android.content.Context
import android.content.SharedPreferences import androidx.core.content.edit
import org.koitharu.kotatsu.utils.delegates.prefs.LongPreferenceDelegate
class AppWidgetConfig private constructor( private const val CATEGORY_ID = "cat_id"
private val prefs: SharedPreferences,
val widgetId: Int
) : SharedPreferences by prefs {
var categoryId by LongPreferenceDelegate(CATEGORY_ID, 0L) class AppWidgetConfig(context: Context, val widgetId: Int) {
companion object { private val prefs = context.getSharedPreferences("appwidget_$widgetId", Context.MODE_PRIVATE)
private const val CATEGORY_ID = "cat_id" var categoryId: Long
get() = prefs.getLong(CATEGORY_ID, 0L)
fun getInstance(context: Context, widgetId: Int) = AppWidgetConfig( set(value) = prefs.edit { putLong(CATEGORY_ID, value) }
context.getSharedPreferences( }
"appwidget_$widgetId",
Context.MODE_PRIVATE
), widgetId
)
}
}

@ -164,7 +164,7 @@ class DetailsViewModel(
branch: String?, branch: String?,
): List<ChapterListItem> { ): List<ChapterListItem> {
val result = ArrayList<ChapterListItem>(chapters.size) val result = ArrayList<ChapterListItem>(chapters.size)
val dateFormat = settings.dateFormat() val dateFormat = settings.getDateFormat()
val currentIndex = chapters.indexOfFirst { it.id == currentId } val currentIndex = chapters.indexOfFirst { it.id == currentId }
val firstNewIndex = chapters.size - newCount val firstNewIndex = chapters.size - newCount
val downloadedIds = downloadedChapters?.mapToSet { it.id } val downloadedIds = downloadedChapters?.mapToSet { it.id }
@ -196,7 +196,7 @@ class DetailsViewModel(
val result = ArrayList<ChapterListItem>(sourceChapters.size) val result = ArrayList<ChapterListItem>(sourceChapters.size)
val currentIndex = sourceChapters.indexOfFirst { it.id == currentId } val currentIndex = sourceChapters.indexOfFirst { it.id == currentId }
val firstNewIndex = sourceChapters.size - newCount val firstNewIndex = sourceChapters.size - newCount
val dateFormat = settings.dateFormat() val dateFormat = settings.getDateFormat()
for (i in sourceChapters.indices) { for (i in sourceChapters.indices) {
val chapter = sourceChapters[i] val chapter = sourceChapters[i]
if (chapter.branch != branch) { if (chapter.branch != branch) {
@ -253,4 +253,4 @@ class DetailsViewModel(
} }
return groups.maxByOrNull { it.value.size }?.key return groups.maxByOrNull { it.value.size }?.key
} }
} }

@ -46,7 +46,7 @@ class ChaptersDialog : AlertDialogFragment<DialogChaptersBinding>(),
} }
val currentId = arguments?.getLong(ARG_CURRENT_ID, 0L) ?: 0L val currentId = arguments?.getLong(ARG_CURRENT_ID, 0L) ?: 0L
val currentPosition = chapters.indexOfFirst { it.id == currentId } val currentPosition = chapters.indexOfFirst { it.id == currentId }
val dateFormat = get<AppSettings>().dateFormat() val dateFormat = get<AppSettings>().getDateFormat()
binding.recyclerViewChapters.adapter = ChaptersAdapter(this).apply { binding.recyclerViewChapters.adapter = ChaptersAdapter(this).apply {
setItems(chapters.mapIndexed { index, chapter -> setItems(chapters.mapIndexed { index, chapter ->
chapter.toListItem( chapter.toListItem(
@ -96,4 +96,4 @@ class ChaptersDialog : AlertDialogFragment<DialogChaptersBinding>(),
putLong(ARG_CURRENT_ID, currentId) putLong(ARG_CURRENT_ID, currentId)
}.show(fm, TAG) }.show(fm, TAG)
} }
} }

@ -62,7 +62,7 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
entryValues = arrayOf("", "MM/dd/yy", "dd/MM/yy", "yyyy-MM-dd", "dd MMM yyyy", "MMM dd, yyyy") entryValues = arrayOf("", "MM/dd/yy", "dd/MM/yy", "yyyy-MM-dd", "dd MMM yyyy", "MMM dd, yyyy")
val now = Date().time val now = Date().time
entries = entryValues.map { value -> entries = entryValues.map { value ->
val formattedDate = settings.dateFormat(value.toString()).format(now) val formattedDate = settings.getDateFormat(value.toString()).format(now)
if (value == "") { if (value == "") {
"${context.getString(R.string.system_default)} ($formattedDate)" "${context.getString(R.string.system_default)} ($formattedDate)"
} else { } else {
@ -171,4 +171,4 @@ class MainSettingsFragment : BasePreferenceFragment(R.string.settings),
summary = storage?.getStorageName(context) ?: getString(R.string.not_available) summary = storage?.getStorageName(context) ?: getString(R.string.not_available)
} }
} }
} }

@ -10,14 +10,13 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.settings.utils.RingtonePickContract import org.koitharu.kotatsu.settings.utils.RingtonePickContract
import org.koitharu.kotatsu.utils.ext.toUriOrNull
class NotificationSettingsLegacyFragment : BasePreferenceFragment(R.string.notifications) { class NotificationSettingsLegacyFragment : BasePreferenceFragment(R.string.notifications) {
private val ringtonePickContract = registerForActivityResult( private val ringtonePickContract = registerForActivityResult(
RingtonePickContract(get<Context>().getString(R.string.notification_sound)) RingtonePickContract(get<Context>().getString(R.string.notification_sound))
) { uri -> ) { uri ->
settings.notificationSound = uri?.toString() ?: return@registerForActivityResult settings.notificationSound = uri ?: return@registerForActivityResult
findPreference<Preference>(AppSettings.KEY_NOTIFICATIONS_SOUND)?.run { findPreference<Preference>(AppSettings.KEY_NOTIFICATIONS_SOUND)?.run {
summary = RingtoneManager.getRingtone(context, uri)?.getTitle(context) summary = RingtoneManager.getRingtone(context, uri)?.getTitle(context)
?: getString(R.string.silent) ?: getString(R.string.silent)
@ -31,7 +30,7 @@ class NotificationSettingsLegacyFragment : BasePreferenceFragment(R.string.notif
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
findPreference<Preference>(AppSettings.KEY_NOTIFICATIONS_SOUND)?.run { findPreference<Preference>(AppSettings.KEY_NOTIFICATIONS_SOUND)?.run {
val uri = settings.notificationSound.toUriOrNull() val uri = settings.notificationSound
summary = RingtoneManager.getRingtone(context, uri)?.getTitle(context) summary = RingtoneManager.getRingtone(context, uri)?.getTitle(context)
?: getString(R.string.silent) ?: getString(R.string.silent)
} }
@ -40,10 +39,10 @@ class NotificationSettingsLegacyFragment : BasePreferenceFragment(R.string.notif
override fun onPreferenceTreeClick(preference: Preference): Boolean { override fun onPreferenceTreeClick(preference: Preference): Boolean {
return when (preference.key) { return when (preference.key) {
AppSettings.KEY_NOTIFICATIONS_SOUND -> { AppSettings.KEY_NOTIFICATIONS_SOUND -> {
ringtonePickContract.launch(settings.notificationSound.toUriOrNull()) ringtonePickContract.launch(settings.notificationSound)
true true
} }
else -> super.onPreferenceTreeClick(preference) else -> super.onPreferenceTreeClick(preference)
} }
} }
} }

@ -183,7 +183,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
setShortcutId(manga.id.toString()) setShortcutId(manga.id.toString())
priority = NotificationCompat.PRIORITY_DEFAULT priority = NotificationCompat.PRIORITY_DEFAULT
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
builder.setSound(settings.notificationSound.toUriOrNull()) builder.setSound(settings.notificationSound)
var defaults = if (settings.notificationLight) { var defaults = if (settings.notificationLight) {
setLights(colorPrimary, 1000, 5000) setLights(colorPrimary, 1000, 5000)
NotificationCompat.DEFAULT_LIGHTS NotificationCompat.DEFAULT_LIGHTS
@ -298,4 +298,4 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
} }
} }
} }
} }

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class BoolPreferenceDelegate(private val key: String, private val defaultValue: Boolean) :
ReadWriteProperty<SharedPreferences, Boolean> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Boolean {
return thisRef.getBoolean(key, defaultValue)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Boolean) {
thisRef.edit {
putBoolean(key, value)
}
}
}

@ -1,27 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class EnumPreferenceDelegate<E : Enum<*>>(
private val cls: Class<E>,
private val key: String,
private val defValue: E
) : ReadWriteProperty<SharedPreferences, E> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): E {
val name = thisRef.getString(key, null)
if (name === null) {
return defValue
}
return cls.enumConstants?.find { it.name == name } ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: E) {
thisRef.edit {
putString(key, value.name)
}
}
}

@ -1,28 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@Deprecated("")
class IntEnumPreferenceDelegate<E : Enum<*>>(
private val cls: Class<E>,
private val key: String,
private val defValue: E
) : ReadWriteProperty<SharedPreferences, E> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): E {
val ord = thisRef.getInt(key, -1)
if (ord == -1) {
return defValue
}
return cls.enumConstants?.getOrNull(ord) ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: E) {
thisRef.edit {
putInt(key, value.ordinal)
}
}
}

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class IntPreferenceDelegate(private val key: String, private val defaultValue: Int) :
ReadWriteProperty<SharedPreferences, Int> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int {
return thisRef.getInt(key, defaultValue)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) {
thisRef.edit {
putInt(key, value)
}
}
}

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class LongPreferenceDelegate(private val key: String, private val defaultValue: Long) :
ReadWriteProperty<SharedPreferences, Long> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Long {
return thisRef.getLong(key, defaultValue)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Long) {
thisRef.edit {
putLong(key, value)
}
}
}

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class NullableStringPreferenceDelegate(private val key: String) :
ReadWriteProperty<SharedPreferences, String?> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): String? {
return thisRef.getString(key, null)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: String?) {
thisRef.edit {
putString(key, value)
}
}
}

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class StringIntPreferenceDelegate(private val key: String, private val defValue: Int) :
ReadWriteProperty<SharedPreferences, Int> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int {
return thisRef.getString(key, defValue.toString())?.toIntOrNull() ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) {
thisRef.edit {
putString(key, value.toString())
}
}
}

@ -1,20 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class StringPreferenceDelegate(private val key: String, private val defValue: String) :
ReadWriteProperty<SharedPreferences, String> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): String {
return thisRef.getString(key, defValue) ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: String) {
thisRef.edit {
putString(key, value)
}
}
}

@ -1,23 +0,0 @@
package org.koitharu.kotatsu.utils.delegates.prefs
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class StringSetPreferenceDelegate(
private val key: String,
private val defValue: Set<String> = emptySet()
) :
ReadWriteProperty<SharedPreferences, Set<String>> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Set<String> {
return thisRef.getStringSet(key, defValue) ?: defValue
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Set<String>) {
thisRef.edit {
putStringSet(key, value)
}
}
}

@ -50,7 +50,7 @@ class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
finishAfterTransition() finishAfterTransition()
return return
} }
config = AppWidgetConfig.getInstance(this, appWidgetId) config = AppWidgetConfig(this, appWidgetId)
viewModel.checkedId = config.categoryId viewModel.checkedId = config.categoryId
viewModel.content.observe(this, this::onContentChanged) viewModel.content.observe(this, this::onContentChanged)
@ -118,4 +118,4 @@ class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(intent) sendBroadcast(intent)
} }
} }

@ -24,7 +24,7 @@ class ShelfListFactory(
) : RemoteViewsService.RemoteViewsFactory { ) : RemoteViewsService.RemoteViewsFactory {
private val dataSet = ArrayList<Manga>() private val dataSet = ArrayList<Manga>()
private val config = AppWidgetConfig.getInstance(context, widgetId) private val config = AppWidgetConfig(context, widgetId)
override fun onCreate() { override fun onCreate() {
} }
@ -73,4 +73,4 @@ class ShelfListFactory(
override fun getViewTypeCount() = 1 override fun getViewTypeCount() = 1
override fun onDestroy() = Unit override fun onDestroy() = Unit
} }

Loading…
Cancel
Save