mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Add: Base- & SwitchPref composables
This commit is contained in:
parent
3156315741
commit
7ea7761877
@ -0,0 +1,149 @@
|
||||
package com.machiav3lli.fdroid.ui.compose.components.prefs
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.requiredWidth
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.surfaceColorAtElevation
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.machiav3lli.fdroid.content.BooleanPrefsMeta
|
||||
import com.machiav3lli.fdroid.content.Preferences
|
||||
import com.machiav3lli.fdroid.ui.compose.utils.addIf
|
||||
|
||||
@Composable
|
||||
fun BasePreference(
|
||||
modifier: Modifier = Modifier,
|
||||
@StringRes titleId: Int,
|
||||
@StringRes summaryId: Int = -1,
|
||||
summary: String? = null,
|
||||
isEnabled: Boolean = true,
|
||||
index: Int = 0,
|
||||
groupSize: Int = 1,
|
||||
startWidget: (@Composable () -> Unit)? = null,
|
||||
endWidget: (@Composable () -> Unit)? = null,
|
||||
bottomWidget: (@Composable () -> Unit)? = null,
|
||||
onClick: (() -> Unit)? = null,
|
||||
) {
|
||||
val base = index.toFloat() / groupSize
|
||||
val rank = (index + 1f) / groupSize
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.clip(
|
||||
RoundedCornerShape(
|
||||
topStart = if (base == 0f) 16.dp else 6.dp,
|
||||
topEnd = if (base == 0f) 16.dp else 6.dp,
|
||||
bottomStart = if (rank == 1f) 16.dp else 6.dp,
|
||||
bottomEnd = if (rank == 1f) 16.dp else 6.dp
|
||||
)
|
||||
)
|
||||
.background(MaterialTheme.colorScheme.surfaceColorAtElevation((rank * 24).dp))
|
||||
.heightIn(min = 64.dp)
|
||||
.addIf(onClick != null) {
|
||||
clickable(enabled = isEnabled, onClick = onClick!!)
|
||||
}, verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Row(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 8.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
startWidget?.let {
|
||||
startWidget()
|
||||
Spacer(modifier = Modifier.requiredWidth(8.dp))
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.addIf(!isEnabled) {
|
||||
alpha(0.3f)
|
||||
}
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(id = titleId),
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontSize = 16.sp
|
||||
)
|
||||
if (summaryId != -1 || summary != null) {
|
||||
Text(
|
||||
text = summary ?: stringResource(id = summaryId),
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
)
|
||||
}
|
||||
bottomWidget?.let {
|
||||
Spacer(modifier = Modifier.requiredWidth(8.dp))
|
||||
bottomWidget()
|
||||
}
|
||||
}
|
||||
endWidget?.let {
|
||||
Spacer(modifier = Modifier.requiredWidth(8.dp))
|
||||
endWidget()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SwitchPreference(
|
||||
modifier: Modifier = Modifier,
|
||||
prefKey: Preferences.Key<Boolean>,
|
||||
index: Int = 0,
|
||||
groupSize: Int = 1,
|
||||
enabled: Boolean = true,
|
||||
onCheckedChange: ((Boolean) -> Unit) = {},
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val (checked, check) = remember(Preferences[prefKey]) { mutableStateOf(Preferences[prefKey]) }
|
||||
|
||||
BasePreference(
|
||||
modifier = modifier,
|
||||
titleId = BooleanPrefsMeta[prefKey]?.first ?: -1,
|
||||
summaryId = BooleanPrefsMeta[prefKey]?.second ?: -1,
|
||||
index = index,
|
||||
groupSize = groupSize,
|
||||
isEnabled = enabled,
|
||||
onClick = {
|
||||
onCheckedChange(!checked)
|
||||
Preferences[prefKey] = !checked
|
||||
check(!checked)
|
||||
},
|
||||
endWidget = {
|
||||
Switch(
|
||||
modifier = Modifier
|
||||
.height(24.dp),
|
||||
checked = checked,
|
||||
onCheckedChange = {
|
||||
onCheckedChange(it)
|
||||
Preferences[prefKey] = it
|
||||
check(it)
|
||||
},
|
||||
enabled = enabled,
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user