Skip to content

Migrate Compose Samples away from using value based BasicTextField - #1745

Merged
srikrishnasakunia merged 5 commits into
mainfrom
mm-update-btf
Dec 1, 2026
Merged

srikrishnasakunia merged 5 commits into
mainfrom
mm-update-btf

Conversation

@MagicalMeghan

@MagicalMeghan MagicalMeghan commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Migrates all remaining value-based text fields across compose-samples to state-based TextFieldState APIs (Compose 1.13 / Material 3 1.5):

-Jetcaster: : Migrated TV BasicTextField and Mobile SearchBar to TextFieldState / SearchBarState, hoisting keywordState onto SearchScreenViewModel.
-Jetsnack: Migrated SearchBar's BasicTextField from TextFieldValue to TextFieldState in Search.kt.
-Reply: Migrated ReplyDockedSearchBar from deprecated DockedSearchBar to state-based SearchBar + ExpandedDockedSearchBar with TextFieldState.
-JetNews: Migrated HomeSearch's OutlinedTextField to TextFieldState, hoisting searchInputState onto HomeViewModel and updating HomeScreenTests.

@MagicalMeghan
MagicalMeghan requested a review from a team as a code owner November 30, 2026 20:07
@MagicalMeghan MagicalMeghan changed the title Migrate Compose Samples away from using value based textfield Migrate Compose Samples away from using value based BasicTextField Nov 30, 2026
@compose-devrel-github-bot

Copy link
Copy Markdown
Contributor

⚡ Jetpack Compose Performance Review

A review of the newly introduced and modified Jetpack Compose code in .gemini/pr_diff.patch was conducted across all 7 performance categories. The findings and recommended optimizations are detailed below.


Finding 1: Missing contentType in LazyColumn.items

  • File: Reply/app/src/main/java/com/example/reply/ui/components/ReplyAppBars.kt
  • Line: 144
  • Rule Category: 3. Lazy layouts optimization (references/lazy-layouts.md)

Why it impacts Compose performance

In ReplyDockedSearchBar, the items in the search results LazyColumn define a key (key = { it.id }) but omit contentType:

items(items = searchResults, key = { it.id }) { email ->

Omitting contentType prevents Compose from reusing composition slots and layout nodes when recycling items of the same visual type during scrolling. Providing an explicit contentType allows Compose to reuse slot structures, avoiding composition disposal and inflation overhead during fast scrolling.

Suggestion

Specify a contentType for all items in the list:

items(
    items = searchResults,
    key = { it.id },
    contentType = { "email" },
) { email ->

Finding 2: Asynchronous snapshotFlow + LaunchedEffect for synchronous derivation and SnapshotStateList double-mutation

  • File: Reply/app/src/main/java/com/example/reply/ui/components/ReplyAppBars.kt
  • Lines: 71–92 (and line 69)
  • Rule Category: 1. State, snapshots, and stability (references/state-and-stability.md)

Why it impacts Compose performance

  1. Unnecessary Coroutine & Flow Dispatch: LaunchedEffect(textFieldState, emails) creates a coroutine and sets up a snapshotFlow observer solely to synchronously filter the in-memory emails collection whenever textFieldState.text changes. Using coroutine dispatch and flow observation for synchronous state derivation adds unnecessary allocation and dispatch overhead.
  2. Double Invalidation from mutableStateListOf: In the collection loop, searchResults.clear() is called immediately followed by searchResults.addAll(...). Because searchResults is an observable SnapshotStateList, each operation triggers a snapshot mutation. Readers of searchResults (such as LazyColumn and the if (searchResults.isNotEmpty()) check) invalidate twice per keystroke: once with an empty list, and then immediately again with the filtered results, causing layout jitter and redundant composition passes.

Suggestion

Replace the SnapshotStateList and LaunchedEffect with a synchronous derivation using remember(emails) { derivedStateOf { ... } }:

val searchResults by remember(emails) {
    derivedStateOf {
        val query = textFieldState.text.toString()
        if (query.isEmpty()) {
            emptyList()
        } else {
            emails.filter {
                it.subject.startsWith(prefix = query, ignoreCase = true) ||
                    it.sender.fullName.startsWith(prefix = query, ignoreCase = true)
            }
        }
    }
}

This derivation evaluates synchronously, updates readers atomically in a single pass without intermediate empty-state popping, and removes the need for LaunchedEffect, snapshotFlow, and mutableStateListOf allocations.

focusManager.clearFocus(force = true)
},
singleLine = true,
lineLimits = TextFieldLineLimits.SingleLine,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

singleline should handle the enter key press, so the above modifier might be unnecessary. please check

@halilozercan

Copy link
Copy Markdown

LGTM now. I have no idea where the approve button is on github or if I have one.

@srikrishnasakunia
srikrishnasakunia merged commit 2ae1a32 into main Dec 1, 2026
16 checks passed
@srikrishnasakunia
srikrishnasakunia deleted the mm-update-btf branch December 1, 2026 19:03
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.

4 participants