-
Notifications
You must be signed in to change notification settings - Fork 7
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
Compose material 3 bindings #197
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cb773c1
Add some material3 bindings - wip
js-bonin 702504c
wip
js-bonin 332f86c
wip
js-bonin 67f4a7c
wip
js-bonin bb40be1
Update api
js-bonin 36a5d17
format
js-bonin 1f6005e
pr comments
js-bonin 3d1adfd
format
js-bonin 0cd3419
Update api
js-bonin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDBasicTextField.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.mirego.trikot.viewmodels.declarative.compose.viewmodel.material3 | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.text.BasicTextField | ||
import androidx.compose.foundation.text.KeyboardActions | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextFieldColors | ||
import androidx.compose.material3.TextFieldDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Brush | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.SolidColor | ||
import androidx.compose.ui.graphics.takeOrElse | ||
import androidx.compose.ui.text.TextLayoutResult | ||
import androidx.compose.ui.text.TextStyle | ||
import com.mirego.trikot.viewmodels.declarative.components.VMDTextFieldViewModel | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.composeValue | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.isOverridingAlpha | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.vmdModifier | ||
import com.mirego.trikot.viewmodels.declarative.compose.viewmodel.internal.FormattedVisualTransformation | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun VMDBasicTextField( | ||
viewModel: VMDTextFieldViewModel, | ||
modifier: Modifier = Modifier, | ||
readOnly: Boolean = false, | ||
textStyle: TextStyle = TextStyle.Default, | ||
placeHolderStyle: TextStyle = LocalTextStyle.current, | ||
keyboardActions: KeyboardActions = KeyboardActions(), | ||
singleLine: Boolean = false, | ||
maxLines: Int = Int.MAX_VALUE, | ||
minLines: Int = 1, | ||
isError: Boolean = false, | ||
onTextLayout: (TextLayoutResult) -> Unit = {}, | ||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, | ||
colors: TextFieldColors = TextFieldDefaults.colors(), | ||
cursorBrush: Brush = SolidColor(Color.Black), | ||
leadingIcon: @Composable (() -> Unit)? = null, | ||
trailingIcon: @Composable (() -> Unit)? = null, | ||
contentPadding: PaddingValues = TextFieldDefaults.contentPaddingWithoutLabel() | ||
) { | ||
val textFieldViewModel: VMDTextFieldViewModel by viewModel.observeAsState(excludedProperties = if (modifier.isOverridingAlpha()) listOf(viewModel::isHidden) else emptyList()) | ||
val keyboardActionsDelegate = buildKeyboardActions(viewModel, keyboardActions) | ||
|
||
val textColor = textStyle.color | ||
val mergedTextStyle = textStyle.merge(TextStyle(color = textColor)) | ||
val visualTransformation = FormattedVisualTransformation(viewModel.formatText) | ||
|
||
BasicTextField( | ||
value = viewModel.text, | ||
onValueChange = { value: String -> | ||
viewModel.onValueChange(value) | ||
}, | ||
modifier = modifier.vmdModifier(viewModel), | ||
enabled = textFieldViewModel.isEnabled, | ||
readOnly = readOnly, | ||
textStyle = mergedTextStyle, | ||
keyboardOptions = KeyboardOptions( | ||
keyboardType = textFieldViewModel.keyboardType.composeValue, | ||
capitalization = textFieldViewModel.autoCapitalization.composeValue, | ||
imeAction = textFieldViewModel.keyboardReturnKeyType.composeValue | ||
), | ||
keyboardActions = keyboardActionsDelegate, | ||
maxLines = maxLines, | ||
minLines = minLines, | ||
singleLine = singleLine, | ||
visualTransformation = visualTransformation, | ||
onTextLayout = onTextLayout, | ||
interactionSource = interactionSource, | ||
cursorBrush = cursorBrush, | ||
decorationBox = @Composable { innerTextField -> | ||
TextFieldDefaults.DecorationBox( | ||
value = viewModel.text, | ||
visualTransformation = visualTransformation, | ||
innerTextField = innerTextField, | ||
placeholder = { | ||
Text( | ||
text = textFieldViewModel.placeholder, | ||
style = placeHolderStyle | ||
) | ||
}, | ||
singleLine = singleLine, | ||
enabled = viewModel.isEnabled, | ||
isError = isError, | ||
interactionSource = interactionSource, | ||
colors = colors, | ||
contentPadding = contentPadding, | ||
leadingIcon = leadingIcon, | ||
trailingIcon = trailingIcon | ||
) | ||
} | ||
) | ||
} |
96 changes: 96 additions & 0 deletions
96
...otlin/com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDCheckbox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.mirego.trikot.viewmodels.declarative.compose.viewmodel.material3 | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.RowScope | ||
import androidx.compose.foundation.selection.toggleable | ||
import androidx.compose.material3.Checkbox | ||
import androidx.compose.material3.CheckboxColors | ||
import androidx.compose.material3.CheckboxDefaults | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.Role | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import com.mirego.trikot.viewmodels.declarative.components.VMDToggleViewModel | ||
import com.mirego.trikot.viewmodels.declarative.components.factory.VMDComponents | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.isOverridingAlpha | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.vmdModifier | ||
import com.mirego.trikot.viewmodels.declarative.compose.viewmodel.VMDLabeledComponent | ||
import com.mirego.trikot.viewmodels.declarative.content.VMDContent | ||
import com.mirego.trikot.viewmodels.declarative.content.VMDNoContent | ||
import kotlinx.coroutines.MainScope | ||
|
||
@Composable | ||
fun VMDCheckbox( | ||
modifier: Modifier = Modifier, | ||
componentModifier: Modifier = Modifier, | ||
viewModel: VMDToggleViewModel<VMDNoContent>, | ||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, | ||
colors: CheckboxColors = CheckboxDefaults.colors() | ||
) { | ||
VMDCheckbox( | ||
modifier = modifier, | ||
componentModifier = componentModifier, | ||
viewModel = viewModel, | ||
label = {}, | ||
interactionSource = interactionSource, | ||
colors = colors | ||
) | ||
} | ||
|
||
@Composable | ||
fun <C : VMDContent> VMDCheckbox( | ||
modifier: Modifier = Modifier, | ||
componentModifier: Modifier = Modifier, | ||
viewModel: VMDToggleViewModel<C>, | ||
label: @Composable (RowScope.(field: C) -> Unit), | ||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, | ||
colors: CheckboxColors = CheckboxDefaults.colors() | ||
) { | ||
val toggleViewModel: VMDToggleViewModel<C> by viewModel.observeAsState(excludedProperties = if (modifier.isOverridingAlpha()) listOf(viewModel::isHidden) else emptyList()) | ||
|
||
VMDLabeledComponent( | ||
modifier = modifier | ||
.toggleable( | ||
value = toggleViewModel.isOn, | ||
role = Role.Checkbox, | ||
onValueChange = { checked -> viewModel.onValueChange(checked) }, | ||
) | ||
.vmdModifier(toggleViewModel), | ||
label = { label(toggleViewModel.label) }, | ||
content = { | ||
Checkbox( | ||
onCheckedChange = null, | ||
modifier = componentModifier, | ||
enabled = toggleViewModel.isEnabled, | ||
checked = toggleViewModel.isOn, | ||
interactionSource = interactionSource, | ||
colors = colors, | ||
) | ||
} | ||
) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun EnabledToggleCheckboxPreview() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all Previews should be private, we don't want to expose that in the public API |
||
val toggleViewModel = VMDComponents.Toggle.withState(true, MainScope()) | ||
VMDCheckbox(viewModel = toggleViewModel) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun DisabledToggleCheckboxPreview() { | ||
val toggleViewModel = VMDComponents.Toggle.withState(false, MainScope()) | ||
VMDCheckbox(viewModel = toggleViewModel) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun SimpleTextToggleCheckboxPreview() { | ||
val toggleViewModel = VMDComponents.Toggle.withText("Label", true, MainScope()) | ||
VMDCheckbox(viewModel = toggleViewModel, label = { Text(it.text) }) | ||
} |
62 changes: 62 additions & 0 deletions
62
...trikot/viewmodels/declarative/compose/viewmodel/material3/VMDCircularProgressIndicator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.mirego.trikot.viewmodels.declarative.compose.viewmodel.material3 | ||
|
||
import androidx.compose.animation.core.animateFloatAsState | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.ProgressIndicatorDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.StrokeCap | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.Dp | ||
import com.mirego.trikot.viewmodels.declarative.components.VMDProgressViewModel | ||
import com.mirego.trikot.viewmodels.declarative.components.factory.VMDComponents | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.isOverridingAlpha | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState | ||
import com.mirego.trikot.viewmodels.declarative.compose.extensions.vmdModifier | ||
import kotlinx.coroutines.MainScope | ||
|
||
@Composable | ||
fun VMDCircularProgressIndicator( | ||
modifier: Modifier = Modifier, | ||
viewModel: VMDProgressViewModel, | ||
color: Color = MaterialTheme.colorScheme.primary, | ||
strokeWidth: Dp = ProgressIndicatorDefaults.CircularStrokeWidth, | ||
trackColor: Color = ProgressIndicatorDefaults.circularTrackColor, | ||
strokeCap: StrokeCap = StrokeCap.Square | ||
) { | ||
val progressViewModel: VMDProgressViewModel by viewModel.observeAsState(excludedProperties = if (modifier.isOverridingAlpha()) listOf(viewModel::isHidden) else emptyList()) | ||
val animatedProgress by animateFloatAsState(targetValue = progressViewModel.determination?.progressRatio ?: 0f, label = "animatedProgress") | ||
|
||
val newModifier = modifier.vmdModifier(progressViewModel) | ||
|
||
if (viewModel.determination == null) { | ||
CircularProgressIndicator( | ||
modifier = newModifier, | ||
color = color, | ||
strokeWidth = strokeWidth, | ||
trackColor = trackColor, | ||
strokeCap = strokeCap | ||
) | ||
} else { | ||
CircularProgressIndicator( | ||
modifier = newModifier, | ||
progress = animatedProgress, | ||
color = color, | ||
strokeWidth = strokeWidth, | ||
trackColor = trackColor, | ||
strokeCap = strokeCap | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun DeterminateCircularProgressIndicatorPreview() { | ||
val progressViewModel = VMDComponents.Progress.determinate(0.25f, MainScope()) | ||
VMDCircularProgressIndicator( | ||
viewModel = progressViewModel | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Opt-in, we should propagate ExperimentalMaterial3Api annotation
i.e.
@ExperimentalMaterial3Api
same for the others
after that, the sample can opt-in at the gradle level.