Skip to content

Commit

Permalink
♻️ Use constant for missing resource id everywhere
Browse files Browse the repository at this point in the history
And deprecate the previously public constant from the API.
  • Loading branch information
Maxr1998 committed Sep 5, 2023
1 parent d0164fe commit 90f85c2
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 45 deletions.
24 changes: 12 additions & 12 deletions library/src/main/java/de/Maxr1998/modernpreferences/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID
import de.Maxr1998.modernpreferences.helpers.DependencyManager
import de.Maxr1998.modernpreferences.helpers.KEY_ROOT_SCREEN
import de.Maxr1998.modernpreferences.helpers.PreferenceMarker
Expand All @@ -45,27 +45,27 @@ import java.util.concurrent.atomic.AtomicBoolean
abstract class AbstractPreference internal constructor(val key: String) {
// UI
@StringRes
var titleRes: Int = DEFAULT_RES_ID
var titleRes: Int = DISABLED_RESOURCE_ID
var title: CharSequence = ""

@StringRes
var summaryRes: Int = DEFAULT_RES_ID
var summaryRes: Int = DISABLED_RESOURCE_ID
var summary: CharSequence? = null

@StringRes
var summaryDisabledRes: Int = DEFAULT_RES_ID
var summaryDisabledRes: Int = DISABLED_RESOURCE_ID
var summaryDisabled: CharSequence? = null

@DrawableRes
var iconRes: Int = DEFAULT_RES_ID
var iconRes: Int = DISABLED_RESOURCE_ID
var icon: Drawable? = null

@Deprecated(
message = "Replace with badgeInfo, which was introduced to allow for further badge customization",
level = DeprecationLevel.WARNING,
)
var badgeRes: Int
get() = badgeInfo?.textRes ?: DEFAULT_RES_ID
get() = badgeInfo?.textRes ?: DISABLED_RESOURCE_ID
set(value) {
badgeInfo = Badge(value)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ open class Preference(key: String) : AbstractPreference(key) {
var includeInCollapseSummary = true

@LayoutRes
open fun getWidgetLayoutResource(): Int = DEFAULT_RES_ID
open fun getWidgetLayoutResource(): Int = DISABLED_RESOURCE_ID

internal fun attachToScreen(screen: PreferenceScreen, position: Int) {
check(parent == null) { "Preference was already attached to a screen!" }
Expand All @@ -179,9 +179,9 @@ open class Preference(key: String) : AbstractPreference(key) {
}

protected open fun resolveSummary(context: Context): CharSequence? = when {
!enabled && summaryDisabledRes != DEFAULT_RES_ID -> context.resources.getText(summaryDisabledRes)
!enabled && summaryDisabledRes != DISABLED_RESOURCE_ID -> context.resources.getText(summaryDisabledRes)
!enabled && summaryDisabled != null -> summaryDisabled
summaryRes != -1 -> context.resources.getText(summaryRes)
summaryRes != DISABLED_RESOURCE_ID -> context.resources.getText(summaryRes)
summary != null -> summary
else -> null
}
Expand Down Expand Up @@ -215,7 +215,7 @@ open class Preference(key: String) : AbstractPreference(key) {
holder.icon?.apply {
itemVisible = true
when {
iconRes != DEFAULT_RES_ID -> setImageResource(iconRes)
iconRes != DISABLED_RESOURCE_ID -> setImageResource(iconRes)
icon != null -> setImageDrawable(icon)
else -> {
setImageDrawable(null)
Expand All @@ -233,7 +233,7 @@ open class Preference(key: String) : AbstractPreference(key) {
}
}
holder.title.apply {
if (titleRes != DEFAULT_RES_ID) setText(titleRes) else text = title
if (titleRes != DISABLED_RESOURCE_ID) setText(titleRes) else text = title
maxLines = Config.titleMaxLines
}
holder.summary?.apply {
Expand All @@ -246,7 +246,7 @@ open class Preference(key: String) : AbstractPreference(key) {
if (badgeInfo != null) {
holder.badge?.apply {
when {
badgeInfo.textRes != DEFAULT_RES_ID -> setText(badgeInfo.textRes)
badgeInfo.textRes != DISABLED_RESOURCE_ID -> setText(badgeInfo.textRes)
else -> text = badgeInfo.text
}
isVisible = badgeInfo.isVisible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import android.widget.SeekBar

const val KEY_ROOT_SCREEN = "root"

const val DEFAULT_RES_ID = -1
/**
* A resource ID as a default value for optional attributes.
*/
internal const val DISABLED_RESOURCE_ID = -1

@Deprecated("This constant was accidentally exposed and should not be used.")
const val DEFAULT_RES_ID = DISABLED_RESOURCE_ID

internal fun SeekBar.onSeek(callback: (Int, Boolean) -> Unit) {
setOnSeekBarChangeListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package de.Maxr1998.modernpreferences.preferences

import android.content.res.ColorStateList
import androidx.annotation.StringRes
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

data class Badge internal constructor(
@StringRes
val textRes: Int = DEFAULT_RES_ID,
val textRes: Int = DISABLED_RESOURCE_ID,
val text: CharSequence? = null,
val badgeColor: ColorStateList? = null,
) {
constructor(text: CharSequence?, badgeColor: ColorStateList? = null) : this(
textRes = DEFAULT_RES_ID,
textRes = DISABLED_RESOURCE_ID,
text = text,
badgeColor = badgeColor,
)
Expand All @@ -23,5 +23,5 @@ data class Badge internal constructor(
)

val isVisible: Boolean
get() = textRes != DEFAULT_RES_ID || text != null
get() = textRes != DISABLED_RESOURCE_ID || text != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferenceScreen
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.R
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

/**
* IMPORTANT: If you're using this independently from the helper DSLs,
Expand Down Expand Up @@ -69,14 +69,14 @@ class CollapsePreference(screen: PreferenceScreen.Builder, key: String) : Prefer
}

private fun buildSummary(context: Context) {
if (summaryRes != DEFAULT_RES_ID || summary != null) return
if (summaryRes != DISABLED_RESOURCE_ID || summary != null) return

summary = preferences.asSequence()
.filter(Preference::includeInCollapseSummary)
.take(MAX_PREFS_IN_SUMMARY)
.joinToString { p ->
when {
p.titleRes != DEFAULT_RES_ID -> context.getString(p.titleRes)
p.titleRes != DISABLED_RESOURCE_ID -> context.getString(p.titleRes)
else -> p.title
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.FrameLayout
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatEditText
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

class EditTextPreference(key: String) : DialogPreference(key) {

Expand All @@ -26,7 +27,7 @@ class EditTextPreference(key: String) : DialogPreference(key) {
var textInputType: Int = InputType.TYPE_NULL

@StringRes
var textInputHintRes: Int = -1
var textInputHintRes: Int = DISABLED_RESOURCE_ID
var textInputHint: CharSequence? = null

var textChangeListener: OnTextChangeListener? = null
Expand All @@ -47,15 +48,15 @@ class EditTextPreference(key: String) : DialogPreference(key) {

override fun createDialog(context: Context): Dialog = Config.dialogBuilderFactory(context).apply {
when {
titleRes != -1 -> setTitle(titleRes)
titleRes != DISABLED_RESOURCE_ID -> setTitle(titleRes)
else -> setTitle(title)
}
val editText = AppCompatEditText(context).apply {
if (textInputType != InputType.TYPE_NULL) {
inputType = textInputType
}
when {
textInputHintRes != -1 -> setHint(textInputHintRes)
textInputHintRes != DISABLED_RESOURCE_ID -> setHint(textInputHintRes)
textInputHint != null -> hint = textInputHint
}
setText(currentInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import androidx.core.view.isVisible
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.R
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

class ExpandableTextPreference(key: String) : Preference(key) {
private var expanded = false

@StringRes
var textRes: Int = -1
var textRes: Int = DISABLED_RESOURCE_ID
var text: CharSequence? = null

var monospace = true
Expand All @@ -51,7 +52,7 @@ class ExpandableTextPreference(key: String) : Preference(key) {
widget.tag = tv
tv.apply {
when {
textRes != -1 -> setText(textRes)
textRes != DISABLED_RESOURCE_ID -> setText(textRes)
else -> text = this@ExpandableTextPreference.text
}
typeface = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.core.view.isVisible
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.R
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

/**
* Shows a drawable inside an ImageView
Expand All @@ -45,7 +46,7 @@ class ImagePreference(key: String) : Preference(key) {
override fun getWidgetLayoutResource() = RESOURCE_CONST

@DrawableRes
var imageRes: Int = -1
var imageRes: Int = DISABLED_RESOURCE_ID
var imageDrawable: Drawable? = null
var lazyImage: (() -> Drawable)? = null

Expand All @@ -62,7 +63,7 @@ class ImagePreference(key: String) : Preference(key) {
super.bindViews(holder)
val image = holder.root.findViewById<ImageView>(R.id.map_image)
when {
imageRes != -1 -> image.setImageResource(imageRes)
imageRes != DISABLED_RESOURCE_ID -> image.setImageResource(imageRes)
imageDrawable != null -> image.setImageDrawable(imageDrawable)
lazyImage != null -> image.setImageDrawable(lazyImage?.invoke())
else -> image.setImageDrawable(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import android.graphics.drawable.StateListDrawable
import android.widget.CompoundButton
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

@Suppress("MemberVisibilityCanBePrivate")
abstract class TwoStatePreference(key: String) : StatefulPreference(key) {
Expand All @@ -43,7 +44,7 @@ abstract class TwoStatePreference(key: String) : StatefulPreference(key) {
var checkedChangeListener: OnCheckedChangeListener? = null

var summaryOn: CharSequence? = null
var summaryOnRes: Int = -1
var summaryOnRes: Int = DISABLED_RESOURCE_ID

/**
* When set to true, dependents are disabled when this preference is checked,
Expand All @@ -59,7 +60,7 @@ abstract class TwoStatePreference(key: String) : StatefulPreference(key) {
}

override fun resolveSummary(context: Context) = when {
checkedInternal && summaryOnRes != -1 -> context.resources.getText(summaryOnRes)
checkedInternal && summaryOnRes != DISABLED_RESOURCE_ID -> context.resources.getText(summaryOnRes)
checkedInternal && summaryOn != null -> summaryOn
else -> super.resolveSummary(context)
}
Expand All @@ -74,7 +75,7 @@ abstract class TwoStatePreference(key: String) : StatefulPreference(key) {
commitBoolean(new)
checkedInternal = new // Update internal state
if (holder != null) {
if (summaryOnRes != -1 || summaryOn != null) {
if (summaryOnRes != DISABLED_RESOURCE_ID || summaryOn != null) {
bindViews(holder)
} else {
updateButton(holder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID
import de.Maxr1998.modernpreferences.preferences.DialogPreference

abstract class AbstractChoiceDialogPreference(
Expand All @@ -32,7 +32,7 @@ abstract class AbstractChoiceDialogPreference(
}

override fun createDialog(context: Context): Dialog = Config.dialogBuilderFactory(context).apply {
if (titleRes != DEFAULT_RES_ID) setTitle(titleRes) else setTitle(title)
if (titleRes != DISABLED_RESOURCE_ID) setTitle(titleRes) else setTitle(title)
val dialogContent = RecyclerView(context).apply {
selectionAdapter = SelectionAdapter(
this@AbstractChoiceDialogPreference,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.Maxr1998.modernpreferences.preferences.choice

import android.content.Context
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

class MultiChoiceDialogPreference(
key: String,
Expand Down Expand Up @@ -58,7 +58,7 @@ class MultiChoiceDialogPreference(
autoGeneratedSummary && selections.isNotEmpty() -> {
selections.joinToString(limit = 3, truncated = "") { selection ->
when {
selection.titleRes != DEFAULT_RES_ID -> context.resources.getText(selection.titleRes)
selection.titleRes != DISABLED_RESOURCE_ID -> context.resources.getText(selection.titleRes)
else -> selection.title
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.core.content.res.use
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import de.Maxr1998.modernpreferences.R
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

internal class SelectionAdapter(
private val preference: AbstractChoiceDialogPreference,
Expand All @@ -36,21 +36,21 @@ internal class SelectionAdapter(
selector.isChecked = preference.isSelected(item)
title.apply {
when {
item.titleRes != -1 -> setText(item.titleRes)
item.titleRes != DISABLED_RESOURCE_ID -> setText(item.titleRes)
else -> text = item.title
}
}
summary.apply {
when {
item.summaryRes != -1 -> setText(item.summaryRes)
item.summaryRes != DISABLED_RESOURCE_ID -> setText(item.summaryRes)
else -> text = item.summary
}
isVisible = item.summaryRes != -1 || item.summary != null
isVisible = item.summaryRes != DISABLED_RESOURCE_ID || item.summary != null
}
if (item.badgeInfo != null) {
badge.apply {
when {
item.badgeInfo.textRes != DEFAULT_RES_ID -> setText(item.badgeInfo.textRes)
item.badgeInfo.textRes != DISABLED_RESOURCE_ID -> setText(item.badgeInfo.textRes)
else -> text = item.badgeInfo.text
}
isVisible = item.badgeInfo.isVisible
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.Maxr1998.modernpreferences.preferences.choice

import androidx.annotation.StringRes
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID
import de.Maxr1998.modernpreferences.preferences.Badge

/**
Expand Down Expand Up @@ -29,7 +29,7 @@ data class SelectionItem private constructor(
@StringRes
titleRes: Int,
@StringRes
summaryRes: Int = DEFAULT_RES_ID,
summaryRes: Int = DISABLED_RESOURCE_ID,
badgeInfo: Badge? = null,
) : this(
key = key,
Expand All @@ -50,9 +50,9 @@ data class SelectionItem private constructor(
badgeInfo: Badge? = null,
) : this(
key = key,
titleRes = DEFAULT_RES_ID,
titleRes = DISABLED_RESOURCE_ID,
title = title,
summaryRes = DEFAULT_RES_ID,
summaryRes = DISABLED_RESOURCE_ID,
summary = summary,
badgeInfo = badgeInfo,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.Maxr1998.modernpreferences.preferences.choice

import android.content.Context
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID
import de.Maxr1998.modernpreferences.helpers.DISABLED_RESOURCE_ID

class SingleChoiceDialogPreference(
key: String,
Expand Down Expand Up @@ -53,7 +53,7 @@ class SingleChoiceDialogPreference(
val selection = currentSelection
return when {
autoGeneratedSummary && selection != null -> when {
selection.titleRes != DEFAULT_RES_ID -> context.resources.getText(selection.titleRes)
selection.titleRes != DISABLED_RESOURCE_ID -> context.resources.getText(selection.titleRes)
else -> selection.title
}
else -> super.resolveSummary(context)
Expand Down

0 comments on commit 90f85c2

Please sign in to comment.