Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ImageVector support to the preference datastore ui #2

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dev.patrickgold.jetpref.datastore.ui
import androidx.annotation.DrawableRes
import androidx.compose.material.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource

@Composable
Expand All @@ -38,3 +39,21 @@ internal fun maybeJetIcon(
else -> null
}
}

@Composable
internal fun maybeJetIcon(
imageVector: ImageVector?,
iconSpaceReserved: Boolean,
contentDescription: String? = null,
): @Composable (() -> Unit)? {
return when {
imageVector != null -> ({
Icon(
imageVector = imageVector,
contentDescription = contentDescription,
)
})
iconSpaceReserved -> ({ })
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dev.patrickgold.jetpref.datastore.ui

import androidx.annotation.DrawableRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -50,7 +49,7 @@ import kotlin.math.roundToLong
internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreference(
pref: PreferenceData<V>,
modifier: Modifier,
@DrawableRes iconId: Int?,
icon: JetIcon,
iconSpaceReserved: Boolean,
title: String,
valueLabel: @Composable (V) -> String,
Expand Down Expand Up @@ -84,7 +83,7 @@ internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreferenc
isDialogOpen.value = true
}
),
icon = maybeJetIcon(iconId, iconSpaceReserved),
icon = icon.getIcon(iconSpaceReserved),
text = title,
secondaryText = summary(prefValue),
enabled = isEnabled,
Expand Down Expand Up @@ -139,7 +138,7 @@ internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreferenc
primaryPref: PreferenceData<V>,
secondaryPref: PreferenceData<V>,
modifier: Modifier,
@DrawableRes iconId: Int?,
icon: JetIcon,
iconSpaceReserved: Boolean,
title: String,
primaryLabel: String,
Expand Down Expand Up @@ -179,7 +178,7 @@ internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreferenc
isDialogOpen.value = true
}
),
icon = maybeJetIcon(iconId, iconSpaceReserved),
icon = icon.getIcon(iconSpaceReserved = iconSpaceReserved),
text = title,
secondaryText = summary(primaryPrefValue, secondaryPrefValue),
enabled = isEnabled,
Expand Down Expand Up @@ -262,7 +261,7 @@ internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreferenc
*
* @param pref The integer preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand All @@ -289,7 +288,7 @@ internal fun <T : PreferenceModel, V> PreferenceUiScope<T>.DialogSliderPreferenc
fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
pref: PreferenceData<Int>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
valueLabel: @Composable (Int) -> String = { it.toString() },
Expand All @@ -303,7 +302,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
pref, modifier, iconId, iconSpaceReserved, title, valueLabel, summary, min, max,
pref, modifier, icon, iconSpaceReserved, title, valueLabel, summary, min, max,
stepIncrement, onPreviewSelectedValue, dialogStrings, enabledIf, visibleIf,
) {
try {
Expand All @@ -320,7 +319,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
* @param primaryPref The primary integer preference data entry from the datastore.
* @param secondaryPref The secondary integer preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand Down Expand Up @@ -353,7 +352,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
primaryPref: PreferenceData<Int>,
secondaryPref: PreferenceData<Int>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
primaryLabel: String,
Expand All @@ -370,9 +369,9 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
primaryPref, secondaryPref, modifier, iconId, iconSpaceReserved, title,
primaryLabel, secondaryLabel, valueLabel, summary, min, max, stepIncrement,
onPreviewSelectedPrimaryValue, onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
primaryPref, secondaryPref, modifier, icon, iconSpaceReserved, title, primaryLabel,
secondaryLabel, valueLabel, summary, min, max, stepIncrement, onPreviewSelectedPrimaryValue,
onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
) {
try {
it.roundToInt()
Expand All @@ -387,7 +386,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
*
* @param pref The long preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand All @@ -414,7 +413,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
pref: PreferenceData<Long>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
valueLabel: @Composable (Long) -> String = { it.toString() },
Expand All @@ -428,7 +427,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
pref, modifier, iconId, iconSpaceReserved, title, valueLabel, summary, min, max,
pref, modifier, icon, iconSpaceReserved, title, valueLabel, summary, min, max,
stepIncrement, onPreviewSelectedValue, dialogStrings, enabledIf, visibleIf,
) {
try {
Expand All @@ -445,7 +444,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
* @param primaryPref The primary long preference data entry from the datastore.
* @param secondaryPref The secondary long preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand Down Expand Up @@ -478,7 +477,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
primaryPref: PreferenceData<Long>,
secondaryPref: PreferenceData<Long>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
primaryLabel: String,
Expand All @@ -495,9 +494,9 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
primaryPref, secondaryPref, modifier, iconId, iconSpaceReserved, title,
primaryLabel, secondaryLabel, valueLabel, summary, min, max, stepIncrement,
onPreviewSelectedPrimaryValue, onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
primaryPref, secondaryPref, modifier, icon, iconSpaceReserved, title, primaryLabel,
secondaryLabel, valueLabel, summary, min, max, stepIncrement, onPreviewSelectedPrimaryValue,
onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
) {
try {
it.roundToLong()
Expand All @@ -512,7 +511,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
*
* @param pref The double preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand All @@ -539,7 +538,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
pref: PreferenceData<Double>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
valueLabel: @Composable (Double) -> String = { it.toString() },
Expand All @@ -553,7 +552,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
pref, modifier, iconId, iconSpaceReserved, title, valueLabel, summary, min, max,
pref, modifier, icon, iconSpaceReserved, title, valueLabel, summary, min, max,
stepIncrement, onPreviewSelectedValue, dialogStrings, enabledIf, visibleIf,
) { it.toDouble() }
}
Expand All @@ -564,7 +563,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
* @param primaryPref The primary double preference data entry from the datastore.
* @param secondaryPref The secondary double preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand Down Expand Up @@ -597,7 +596,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
primaryPref: PreferenceData<Double>,
secondaryPref: PreferenceData<Double>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
primaryLabel: String,
Expand All @@ -614,9 +613,9 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
primaryPref, secondaryPref, modifier, iconId, iconSpaceReserved, title,
primaryLabel, secondaryLabel, valueLabel, summary, min, max, stepIncrement,
onPreviewSelectedPrimaryValue, onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
primaryPref, secondaryPref, modifier, icon, iconSpaceReserved, title, primaryLabel,
secondaryLabel, valueLabel, summary, min, max, stepIncrement, onPreviewSelectedPrimaryValue,
onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
) { it.toDouble() }
}

Expand All @@ -625,7 +624,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
*
* @param pref The float preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand All @@ -652,7 +651,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
pref: PreferenceData<Float>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
valueLabel: @Composable (Float) -> String = { it.toString() },
Expand All @@ -666,7 +665,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
pref, modifier, iconId, iconSpaceReserved, title, valueLabel, summary, min, max,
pref, modifier, icon, iconSpaceReserved, title, valueLabel, summary, min, max,
stepIncrement, onPreviewSelectedValue, dialogStrings, enabledIf, visibleIf,
) { it }
}
Expand All @@ -677,7 +676,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
* @param primaryPref The primary float preference data entry from the datastore.
* @param secondaryPref The secondary float preference data entry from the datastore.
* @param modifier Modifier to be applied to the underlying list item.
* @param iconId The icon ID of the list entry icon.
* @param icon The [JetIcon] of the list entry.
* @param iconSpaceReserved If the space at the start of the list item should be reserved (blank
* space) if no icon ID is provided.
* @param title The title of this preference, shown as the list item primary text (max 1 line).
Expand Down Expand Up @@ -710,7 +709,7 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
primaryPref: PreferenceData<Float>,
secondaryPref: PreferenceData<Float>,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
primaryLabel: String,
Expand All @@ -727,8 +726,8 @@ fun <T : PreferenceModel> PreferenceUiScope<T>.DialogSliderPreference(
visibleIf: PreferenceDataEvaluator = { true },
) {
DialogSliderPreference(
primaryPref, secondaryPref, modifier, iconId, iconSpaceReserved, title,
primaryLabel, secondaryLabel, valueLabel, summary, min, max, stepIncrement,
onPreviewSelectedPrimaryValue, onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
primaryPref, secondaryPref, modifier, icon, iconSpaceReserved, title, primaryLabel,
secondaryLabel, valueLabel, summary, min, max, stepIncrement, onPreviewSelectedPrimaryValue,
onPreviewSelectedSecondaryValue, dialogStrings, enabledIf, visibleIf,
) { it }
}
lm41 marked this conversation as resolved.
Show resolved Hide resolved
lm41 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.patrickgold.jetpref.datastore.ui

import androidx.annotation.DrawableRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector

/**
* Interface which provides the Icon for the JetPref library
*
* @since 0.1.0
*/
interface JetIcon {
/**
* Provie the Icon.
*
* @param iconSpaceReserved If the space at the start of the preference should be reserved,
* if no icon is provided.
*
* @since 0.1.0
*/
@Composable
fun getIcon(iconSpaceReserved: Boolean): @Composable (() -> Unit)?
}

class ImageVectorIcon(private val imageVector: ImageVector?) : JetIcon {
@Composable
override fun getIcon(iconSpaceReserved: Boolean): @Composable (() -> Unit)? {
return maybeJetIcon(imageVector = imageVector, iconSpaceReserved = iconSpaceReserved)
}
}

object EmptyIcon : JetIcon {
@Composable
override fun getIcon(iconSpaceReserved: Boolean): @Composable (() -> Unit)? {
return if (iconSpaceReserved) {
({ })
} else {
null
}
}
}

class DrawableResIcon(@DrawableRes private val iconId: Int?) : JetIcon {
@Composable
override fun getIcon(iconSpaceReserved: Boolean): @Composable (() -> Unit)? {
return maybeJetIcon(id = iconId, iconSpaceReserved = iconSpaceReserved)
}
}


/**
* Get the JetIcon from the [ImageVector] or the [EmptyIcon] if null
*
* @since 0.1.0
*/
val ImageVector?.jetIcon: JetIcon
get() = if (this == null) { EmptyIcon } else { ImageVectorIcon(this) }

/**
* Get the JetIcon from the [DrawableRes] ID or the [EmptyIcon] if null
*
* @since 0.1.0
*/
val @receiver:DrawableRes Int?.jetIcon: JetIcon
get() = if (this == null) { EmptyIcon } else { DrawableResIcon(this) }
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package dev.patrickgold.jetpref.datastore.ui

import android.annotation.SuppressLint
import androidx.annotation.DrawableRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -198,7 +197,7 @@ fun <T : PreferenceModel, V : Any> PreferenceUiScope<T>.ListPreference(
listPref: PreferenceData<V>,
switchPref: PreferenceData<Boolean>? = null,
modifier: Modifier = Modifier,
@DrawableRes iconId: Int? = null,
icon: JetIcon = EmptyIcon,
iconSpaceReserved: Boolean = this.iconSpaceReserved,
title: String,
summarySwitchDisabled: String? = null,
Expand Down Expand Up @@ -229,7 +228,7 @@ fun <T : PreferenceModel, V : Any> PreferenceUiScope<T>.ListPreference(
isDialogOpen.value = true
}
),
icon = maybeJetIcon(iconId, iconSpaceReserved),
icon = icon.getIcon(iconSpaceReserved),
text = title,
secondaryText = if (switchPrefValue?.value == true || switchPrefValue == null) {
entries.find {
Expand Down
Loading