Repository navigation
Migrate Card and Time pickers Compose component snippets - #1165
pengzhi-cb wants to merge 9 commits into
Conversation
|
Here is the summary of changes. You are about to add 2 region tags.
This comment is generated by snippet-bot.
|
9bec927 to
941cd69
Compare
a788f90 to
f82996a
Compare
| val formatter = remember { SimpleDateFormat("hh:mm a", Locale.getDefault()) } | ||
|
|
||
| // [START android_compose_components_dial_usestate_caller] | ||
| var selectedTime: TimePickerState? by remember { mutableStateOf(null) } |
There was a problem hiding this comment.
Why does this need to be a nullable type?
There was a problem hiding this comment.
This PR migrates the existing hardcoded snippet on https://developer.android.com/develop/ui/compose/components/time-pickers#use verbatim (which was originally excerpted from TimePickerExamples() at lines 74 and 114–122 of this same file).
Inspecting the code, it's nullable here because the preceding DialUseStateExample(onConfirm: (TimePickerState) -> Unit, onDismiss: () -> Unit) only passes a TimePickerState back to the caller when onConfirm fires, so null represents the initial state that shows "No time selected.".
|
|
||
| if (selectedTime != null) { | ||
| val cal = Calendar.getInstance() | ||
| cal.set(Calendar.HOUR_OF_DAY, selectedTime!!.hour) |
There was a problem hiding this comment.
The !! feels like a code smell caused by the earlier use of a nullable type.
There was a problem hiding this comment.
I kept it verbatim by default to minimize diffs during migration, but the !! can be eliminated by smart-casting with a local val.
Let me know if you want to proceed with that change
There was a problem hiding this comment.
Went ahead and replaced selectedTime!! with a smart-cast local val currentSelectedTime = selectedTime in c4f55d3
cc: @kkuan2011
| // [START android_compose_components_card_minimal] | ||
| @Composable | ||
| fun CardMinimalExample() { | ||
| Card() { |
There was a problem hiding this comment.
Good catch, Card() came from copying the hardcoded snippet on https://developer.android.com/develop/ui/compose/components/card#basic verbatim.
Removed the redundant () in 68b4c11.
Code snippets are for:
Total: 2 code snippets (2 Kotlin and 0 XML) across 2 pages.
List of modifications
Card {insideCardMinimalExample(android_compose_components_card_minimalinCard.kt).DialUseStateExamplecaller snippet (android_compose_components_dial_usestate_callerinTimePickers.kt) fromonConfirm = {\n time ->toonConfirm = { time ->for standard 4-space Kotlin formatting, replacedselectedTime!!with a smart-cast localval currentSelectedTime = selectedTime, and preserved both standalone// ...comments verbatim.