Skip to content

Optimize ElapsedTimeText recompositions and enable tabular figures#515

Open
temcguir wants to merge 14 commits into
temcguir/state_conflationfrom
temcguir/elapsed_time_opt
Open

Optimize ElapsedTimeText recompositions and enable tabular figures#515
temcguir wants to merge 14 commits into
temcguir/state_conflationfrom
temcguir/elapsed_time_opt

Conversation

@temcguir
Copy link
Copy Markdown
Collaborator

@temcguir temcguir commented May 15, 2026

Problem

Reading the elapsed time state at the root of PreviewScreen caused the entire screen to recompose on every timer tick. This added unnecessary load to the Compose runtime.

Solution

  • Refactored ContentScreen and LayoutWrapper to pass a lambda provider () -> CaptureUiState.Ready instead of the raw state.
  • Used derivedStateOf in PreviewScreen to lazily evaluate the state.
  • Passed the formatted time provider lambda directly to ElapsedTimeText within its slot, deferring the state read to the component level.

Impact

Isolated recompositions to just the components that need the state, improving performance during recording.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the elapsed time display logic to use a lambda provider and introduces tabular figures for the timer text to prevent layout jitter. While the changes aim to improve performance, the reviewer identified that reading the state value at the top level of the PreviewScreen composable still triggers unnecessary recompositions, defeating the intended optimization. Feedback also highlights a hardcoded format string that should be moved to resources, outdated KDoc documentation, and a suggestion to use LocalTextStyle to avoid overriding theme defaults. Finally, a minor cleanup is requested to replace a fully qualified name with an import.

@temcguir temcguir force-pushed the temcguir/elapsed_time_opt branch from 4b07289 to 9a8a270 Compare May 15, 2026 03:11
@temcguir
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the PreviewScreen to use a provider pattern for CaptureUiState.Ready, which optimizes how state is passed to child components. It also updates the ElapsedTimeText component to use a provider for formatted time and applies tabular numbers for consistent character width. Review feedback suggests using a safe cast within the captureUiStateProvider to prevent potential crashes during state transitions and extracting the hardcoded time format string to strings.xml to support localization and adhere to the project's style guide.

@temcguir temcguir changed the title feat: Optimize ElapsedTimeText recompositions and enable tabular figures Optimize ElapsedTimeText recompositions and enable tabular figures May 15, 2026
@temcguir temcguir force-pushed the temcguir/elapsed_time_opt branch from d2c801f to 999349e Compare May 15, 2026 18:21
@temcguir temcguir force-pushed the temcguir/elapsed_time_opt branch from 999349e to 1894f13 Compare May 15, 2026 18:26
@temcguir temcguir force-pushed the temcguir/elapsed_time_opt branch from 1894f13 to 43c5368 Compare May 15, 2026 18:30
@temcguir temcguir marked this pull request as ready for review May 15, 2026 18:30
Copy link
Copy Markdown
Collaborator

@Kimblebee Kimblebee left a comment

Choose a reason for hiding this comment

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

I think we could consider the tradeoffs of passing everything into a lambda vs decoupling high-frequency values (like elapsedTimeNanos) from the UI state and exposing it as a separate flow?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As something adjacent to this PR's changes, could this be optimized as well? Wouldnt the updates to elapsed time cause this LaunchedEffect to repeatedly trigger while recording?

textAlign = TextAlign.Center
text = formattedTime,
textAlign = TextAlign.Center,
style = androidx.compose.material3.LocalTextStyle.current.copy(fontFeatureSettings = "tnum")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: can we clean this up and import LocalTextStyle at the top of the class like the others?

Comment on lines 131 to 134
* @param formattedTimeProvider a provider for the formatted time string.
*/
@Composable
fun ElapsedTimeText(modifier: Modifier = Modifier, elapsedTimeUiState: ElapsedTimeUiState) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The updated param Kdoc is incongruent with the funciton signature

@temcguir temcguir changed the base branch from main to temcguir/state_conflation May 22, 2026 15:53
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.

2 participants