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

Compose material 3 bindings #197

Merged
merged 9 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -169,6 +169,68 @@ public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/VM
public static final fun VMDText--4IGK_g (Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextViewModel;JJLandroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontFamily;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/text/style/TextAlign;JIZIILkotlin/jvm/functions/Function1;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/runtime/Composer;III)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/ComposableSingletons$VMDCheckboxKt {
public static final field INSTANCE Lcom/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/ComposableSingletons$VMDCheckboxKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public static field lambda-2 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$compose_flow_release ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-2$compose_flow_release ()Lkotlin/jvm/functions/Function4;
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/ComposableSingletons$VMDSwitchKt {
public static final field INSTANCE Lcom/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/ComposableSingletons$VMDSwitchKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public static field lambda-2 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$compose_flow_release ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-2$compose_flow_release ()Lkotlin/jvm/functions/Function4;
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDBasicTextFieldKt {
public static final fun VMDBasicTextField (Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextFieldViewModel;Landroidx/compose/ui/Modifier;ZLandroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/foundation/text/KeyboardActions;ZIIZLkotlin/jvm/functions/Function1;Landroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/TextFieldColors;Landroidx/compose/ui/graphics/Brush;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/foundation/layout/PaddingValues;Landroidx/compose/runtime/Composer;III)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDCheckboxKt {
public static final fun DisabledToggleCheckboxPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun EnabledToggleCheckboxPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun SimpleTextToggleCheckboxPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun VMDCheckbox (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDToggleViewModel;Landroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/CheckboxColors;Landroidx/compose/runtime/Composer;II)V
public static final fun VMDCheckbox (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDToggleViewModel;Lkotlin/jvm/functions/Function4;Landroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/CheckboxColors;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDCircularProgressIndicatorKt {
public static final fun DeterminateCircularProgressIndicatorPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun VMDCircularProgressIndicator-DUhRLBM (Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDProgressViewModel;JFJILandroidx/compose/runtime/Composer;II)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDDropDownMenuKt {
public static final fun VMDDropDownMenu-x0xb5LI (Lcom/mirego/trikot/viewmodels/declarative/components/VMDPickerViewModel;ZLkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;JLandroidx/compose/ui/window/PopupProperties;Lkotlin/jvm/functions/Function5;Landroidx/compose/runtime/Composer;II)V
public static final fun VMDDropDownMenuItem (Lcom/mirego/trikot/viewmodels/declarative/components/VMDPickerViewModel;Lcom/mirego/trikot/viewmodels/declarative/components/VMDPickerItemViewModel;ILandroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function0;Landroidx/compose/foundation/layout/PaddingValues;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lkotlin/jvm/functions/Function2;Landroidx/compose/material3/MenuItemColors;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDLinearProgressIndicatorKt {
public static final fun DeterminateLinearProgressIndicatorPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun VMDLinearProgressIndicator-eaDK9VM (Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDProgressViewModel;JJLandroidx/compose/runtime/Composer;II)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDSwitchKt {
public static final fun DisabledSwitchPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun EnabledSwitchPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun SimpleTextSwitchPreview (Landroidx/compose/runtime/Composer;I)V
public static final fun VMDSwitch (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDToggleViewModel;Landroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/SwitchColors;Landroidx/compose/runtime/Composer;II)V
public static final fun VMDSwitch (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDToggleViewModel;Lkotlin/jvm/functions/Function4;Landroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/SwitchColors;Landroidx/compose/runtime/Composer;II)V
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDTextFieldKt {
public static final fun VMDTextField (Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextFieldViewModel;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/text/TextStyle;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/foundation/text/KeyboardActions;ZZIILandroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/ui/graphics/Shape;Landroidx/compose/material3/TextFieldColors;Landroidx/compose/runtime/Composer;III)V
public static final fun buildKeyboardActions (Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextFieldViewModel;Landroidx/compose/foundation/text/KeyboardActions;)Landroidx/compose/foundation/text/KeyboardActions;
}

public final class com/mirego/trikot/viewmodels/declarative/compose/viewmodel/material3/VMDTextKt {
public static final fun VMDText--4IGK_g (Landroidx/compose/ui/Modifier;Lcom/mirego/trikot/viewmodels/declarative/components/VMDTextViewModel;JJLandroidx/compose/ui/text/font/FontStyle;Landroidx/compose/ui/text/font/FontWeight;Landroidx/compose/ui/text/font/FontFamily;JLandroidx/compose/ui/text/style/TextDecoration;Landroidx/compose/ui/text/style/TextAlign;JIZIILkotlin/jvm/functions/Function1;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/runtime/Composer;III)V
}

public final class com/mirego/trikot/viewmodels/declarative/configuration/DefaultImageProvider : com/mirego/trikot/viewmodels/declarative/configuration/VMDImageProvider {
public static final field $stable I
public fun <init> ()V
Expand Down
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)
Copy link
Member

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.

@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
)
}
)
}
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() {
Copy link
Member

Choose a reason for hiding this comment

The 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) })
}
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
)
}
Loading
Loading