Skip to content

Migrate Card and Time pickers Compose component snippets - #1165

Open
pengzhi-cb wants to merge 9 commits into
android:mainfrom
pengzhi-cb:pengzhi-cb/compose-components-card-timepickers
Open

pengzhi-cb wants to merge 9 commits into
android:mainfrom
pengzhi-cb:pengzhi-cb/compose-components-card-timepickers

Conversation

@pengzhi-cb

@pengzhi-cb pengzhi-cb commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Code snippets are for:

  1. (1 snippet) https://developer.android.com/develop/ui/compose/components/card
  2. (1 snippet) https://developer.android.com/develop/ui/compose/components/time-pickers

Total: 2 code snippets (2 Kotlin and 0 XML) across 2 pages.

List of modifications
  1. Card page: Removed redundant empty parentheses in Card { inside CardMinimalExample (android_compose_components_card_minimal in Card.kt).
  2. Time-pickers page: Normalized the lambda header in the DialUseStateExample caller snippet (android_compose_components_dial_usestate_caller in TimePickers.kt) from onConfirm = {\n time -> to onConfirm = { time -> for standard 4-space Kotlin formatting, replaced selectedTime!! with a smart-cast local val currentSelectedTime = selectedTime, and preserved both standalone // ... comments verbatim.

@pengzhi-cb pengzhi-cb changed the title Add code snippets for Card and Time pickers guides Migrate Card and Time pickers Compose component snippets Oct 8, 2026
@pengzhi-cb
pengzhi-cb marked this pull request as ready for review October 8, 2026 20:43
@pengzhi-cb
pengzhi-cb requested a review from a team as a code owner October 8, 2026 20:43
@pengzhi-cb
pengzhi-cb requested a review from dturner October 8, 2026 20:43
@snippet-bot

snippet-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 2 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@pengzhi-cb
pengzhi-cb force-pushed the pengzhi-cb/compose-components-card-timepickers branch from 9bec927 to 941cd69 Compare October 15, 2026 13:10
val formatter = remember { SimpleDateFormat("hh:mm a", Locale.getDefault()) }

// [START android_compose_components_dial_usestate_caller]
var selectedTime: TimePickerState? by remember { mutableStateOf(null) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a nullable type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !! feels like a code smell caused by the earlier use of a nullable type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: redundant ()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants