Skip to content

Commit

Permalink
Avoid unneeded recompositions & remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Feb 23, 2024
1 parent ccae039 commit fc7516e
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions shared/src/main/kotlin/watchfaces/hansie/HansieClock.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(InternalComposeOClockApi::class)

package org.splitties.compose.oclock.sample.watchfaces.hansie

import android.icu.text.MessageFormat
Expand Down Expand Up @@ -51,17 +49,13 @@ private fun HansieBackground() {

@Composable
private fun HansieHoursHand(locale: Locale) {
val time = LocalTime.current

val style = remember {
TextStyle.Default.copy(
fontSize = 26.sp,
fontWeight = FontWeight.W600,
color = Color.White
)
}
val handValue = time.hours

HansieHand(locale, style, HansieHandType.Hour)
}

Expand All @@ -84,8 +78,6 @@ private fun HansieMinutesHand(locale: Locale) {

@Composable
private fun HansieSecondsHand(locale: Locale) {
val time = LocalTime.current

val style = remember {
TextStyle.Default.copy(
fontSize = 14.sp,
Expand All @@ -102,26 +94,26 @@ private fun HansieSecondsHand(locale: Locale) {
}

@Composable
private fun HansieHand(locale: Locale, style: TextStyle, type: HansieHandType) {
val handValue = type.handValue(LocalTime.current)
val handValueInt = handValue.toInt()

val degrees = (handValue * type.factor) - 90f
private fun HansieHand(
locale: Locale,
style: TextStyle,
type: HansieHandType
) {
val time = LocalTime.current

val timeFormatter = remember {
val timeFormatter = remember(locale) {
MessageFormat(
"{0,spellout,full}",
locale
)
}
@OptIn(InternalComposeOClockApi::class)
val measurer = LocalTextMeasurerWithoutCache.current

val timeText = remember(handValueInt) {
timeFormatter.format(arrayOf(handValueInt))
}

val textLayoutResult by remember(timeText) {
val textLayoutResult by remember(timeFormatter) {
derivedStateOf {
val handValueInt = type.handValue(time).toInt()
val timeText = timeFormatter.format(arrayOf(handValueInt))
measurer.measure(
timeText,
style
Expand All @@ -130,6 +122,8 @@ private fun HansieHand(locale: Locale, style: TextStyle, type: HansieHandType) {
}

OClockCanvas {
val handValue = type.handValue(time)
val degrees = (handValue * type.factor) - 90f
rotate(degrees = degrees) {
drawText(
brush = style.brush ?: SolidColor(style.color),
Expand Down

0 comments on commit fc7516e

Please sign in to comment.