Skip to content

Commit 9ef7a89

Browse files
committed
fix(balance): prevent unneccesary recomp/flicker of balance when opening sheet
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 4e3dab3 commit 9ef7a89

File tree

9 files changed

+38
-23
lines changed

9 files changed

+38
-23
lines changed

api/src/main/java/com/getcode/network/BalanceController.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.getcode.network
22

33
import android.content.Context
4-
import android.icu.text.NumberFormat
5-
import android.icu.text.NumberFormat.INTEGERSTYLE
64
import com.getcode.manager.SessionManager
75
import com.getcode.model.Currency
86
import com.getcode.model.CurrencyCode
9-
import com.getcode.model.KinAmount
107
import com.getcode.model.PrefsString
118
import com.getcode.model.Rate
129
import com.getcode.network.client.TransactionReceiver
@@ -26,12 +23,10 @@ import kotlinx.coroutines.CoroutineScope
2623
import kotlinx.coroutines.Dispatchers
2724
import kotlinx.coroutines.flow.Flow
2825
import kotlinx.coroutines.flow.MutableStateFlow
29-
import kotlinx.coroutines.flow.SharedFlow
3026
import kotlinx.coroutines.flow.SharingStarted
3127
import kotlinx.coroutines.flow.StateFlow
3228
import kotlinx.coroutines.flow.combine
3329
import kotlinx.coroutines.flow.distinctUntilChanged
34-
import kotlinx.coroutines.flow.filterNotNull
3530
import kotlinx.coroutines.flow.flatMapLatest
3631
import kotlinx.coroutines.flow.flowOf
3732
import kotlinx.coroutines.flow.flowOn
@@ -42,17 +37,16 @@ import kotlinx.coroutines.flow.onEach
4237
import kotlinx.coroutines.flow.stateIn
4338
import kotlinx.coroutines.launch
4439
import timber.log.Timber
45-
import java.util.Locale
4640
import java.util.concurrent.TimeUnit
4741
import javax.inject.Inject
48-
import kotlin.math.roundToInt
4942

5043
data class BalanceDisplay(
5144
val marketValue: Double = 0.0,
5245
val formattedValue: String = "",
5346
val currency: Currency? = null,
5447

5548
)
49+
5650
open class BalanceController @Inject constructor(
5751
exchange: Exchange,
5852
networkObserver: NetworkConnectivityListener,

api/src/main/java/com/getcode/network/source/ChatMessagePagingSource.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.getcode.ed25519.Ed25519.KeyPair
66
import com.getcode.model.Chat
77
import com.getcode.model.ChatMessage
88
import com.getcode.model.Cursor
9-
import com.getcode.model.ID
109
import com.getcode.network.client.Client
1110
import com.getcode.network.client.fetchMessagesFor
1211

app/src/main/java/com/getcode/navigation/screens/ChatScreens.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ data object BalanceModal : ChatGraph, ModalRoot {
133133

134134
LifecycleEffect(
135135
onDisposed = {
136-
viewModel.dispatchEvent(
137-
BalanceSheetViewModel.Event.OnDebugBucketsVisible(
138-
false
136+
val disposedScreen = navigator.lastItem
137+
if (disposedScreen !is BalanceModal) {
138+
viewModel.dispatchEvent(
139+
BalanceSheetViewModel.Event.OnDebugBucketsVisible(false)
139140
)
140-
)
141+
}
141142
}
142143
)
143144
}

app/src/main/java/com/getcode/ui/components/chat/ChatInput.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.getcode.theme.CodeTheme
3838
import com.getcode.theme.extraLarge
3939
import com.getcode.theme.inputColors
4040
import com.getcode.ui.components.TextInput
41+
import com.getcode.ui.utils.rememberedClickable
4142
import com.getcode.ui.utils.withTopBorder
4243

4344
@OptIn(ExperimentalFoundationApi::class)
@@ -64,7 +65,7 @@ fun ChatInput(
6465
.align(Alignment.Bottom)
6566
.border(width = 1.dp, color = Color.White, shape = CircleShape)
6667
.clip(CircleShape)
67-
.clickable { onSendCash() }
68+
.rememberedClickable { onSendCash() }
6869
.size(ChatInput_Size)
6970
.padding(8.dp),
7071
contentAlignment = Alignment.Center,
@@ -110,7 +111,7 @@ fun ChatInput(
110111
.align(Alignment.Bottom)
111112
.background(ChatOutgoing, shape = CircleShape)
112113
.clip(CircleShape)
113-
.clickable { onSendMessage() }
114+
.rememberedClickable { onSendMessage() }
114115
.size(ChatInput_Size)
115116
.padding(8.dp),
116117
contentAlignment = Alignment.Center,

app/src/main/java/com/getcode/ui/components/chat/ChatNode.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.getcode.theme.CodeTheme
2727
import com.getcode.ui.components.Badge
2828
import com.getcode.ui.components.chat.utils.localizedText
2929
import com.getcode.ui.utils.debugBounds
30+
import com.getcode.ui.utils.rememberedClickable
3031
import com.getcode.util.DateUtils
3132
import com.getcode.util.formatTimeRelatively
3233
import com.getcode.util.toInstantFromMillis
@@ -44,7 +45,7 @@ fun ChatNode(
4445
) {
4546
Column(
4647
modifier = modifier
47-
.clickable { onClick() }
48+
.rememberedClickable { onClick() }
4849
.padding(
4950
vertical = CodeTheme.dimens.grid.x3,
5051
horizontal = CodeTheme.dimens.inset

app/src/main/java/com/getcode/view/main/account/BucketDebugger.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.getcode.solana.organizer.AccountType
1919
import com.getcode.theme.BrandLight
2020
import com.getcode.theme.CodeTheme
2121
import com.getcode.ui.components.MiddleEllipsisText
22+
import com.getcode.ui.utils.rememberedClickable
2223

2324

2425
@Composable
@@ -41,7 +42,7 @@ fun BucketDebugger() {
4142
items(buckets) { info ->
4243
Column(
4344
modifier = Modifier
44-
.clickable {
45+
.rememberedClickable {
4546
clipboard.setText(AnnotatedString(info.address.base58()))
4647
}
4748
.padding(horizontal = CodeTheme.dimens.grid.x3)

app/src/main/java/com/getcode/view/main/balance/BalanceSheet.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.compose.runtime.Composable
2222
import androidx.compose.runtime.derivedStateOf
2323
import androidx.compose.runtime.getValue
2424
import androidx.compose.runtime.remember
25+
import androidx.compose.runtime.saveable.rememberSaveable
2526
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
2627
import androidx.compose.ui.Alignment.Companion.CenterVertically
2728
import androidx.compose.ui.Modifier
@@ -226,8 +227,9 @@ fun BalanceTop(
226227
isClickable: Boolean,
227228
onClick: () -> Unit = {}
228229
) {
230+
val text = rememberSaveable(state.amountText) { state.amountText }
229231
AmountArea(
230-
amountText = state.amountText,
232+
amountText = text,
231233
isAltCaption = false,
232234
isAltCaptionKinIcon = false,
233235
isLoading = state.chatsLoading,

app/src/main/java/com/getcode/view/main/balance/BalanceSheetViewModel.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import com.getcode.view.main.getKin.GetKinSheetViewModel
2020
import dagger.hilt.android.lifecycle.HiltViewModel
2121
import kotlinx.coroutines.Dispatchers
2222
import kotlinx.coroutines.flow.SharingStarted
23+
import kotlinx.coroutines.flow.cache
2324
import kotlinx.coroutines.flow.distinctUntilChanged
2425
import kotlinx.coroutines.flow.filterNotNull
2526
import kotlinx.coroutines.flow.launchIn
2627
import kotlinx.coroutines.flow.map
2728
import kotlinx.coroutines.flow.onEach
2829
import kotlinx.coroutines.flow.shareIn
30+
import kotlinx.coroutines.flow.stateIn
2931
import timber.log.Timber
3032
import javax.inject.Inject
3133

@@ -60,9 +62,7 @@ class BalanceSheetViewModel @Inject constructor(
6062
data class OnDebugBucketsVisible(val show: Boolean) : Event
6163
data class OnBuyModuleStateChanged(val module: Feature) : Event
6264
data class OnCurrencySelectionStateChanged(val module: Feature): Event
63-
data class OnLatestRateChanged(
64-
val rate: Rate,
65-
) : Event
65+
data class OnLatestRateChanged(val rate: Rate) : Event
6666

6767
data class OnBalanceChanged(
6868
val flagResId: Int?,
@@ -92,6 +92,7 @@ class BalanceSheetViewModel @Inject constructor(
9292

9393
balanceController.formattedBalance
9494
.filterNotNull()
95+
.distinctUntilChanged()
9596
.onEach {
9697
dispatchEvent(
9798
Dispatchers.Main,
@@ -102,7 +103,8 @@ class BalanceSheetViewModel @Inject constructor(
102103
isKin = it.currency == Currency.Kin
103104
)
104105
)
105-
}.launchIn(viewModelScope)
106+
}
107+
.launchIn(viewModelScope)
106108

107109
historyController.chats
108110
.onEach {

app/src/main/java/com/getcode/view/main/giveKin/AmountText.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import androidx.compose.foundation.layout.wrapContentWidth
1313
import androidx.compose.foundation.shape.CircleShape
1414
import androidx.compose.material.Text
1515
import androidx.compose.runtime.Composable
16+
import androidx.compose.runtime.derivedStateOf
1617
import androidx.compose.runtime.getValue
1718
import androidx.compose.runtime.mutableStateOf
1819
import androidx.compose.runtime.remember
20+
import androidx.compose.runtime.saveable.rememberSaveable
1921
import androidx.compose.runtime.setValue
2022
import androidx.compose.ui.Alignment.Companion.CenterVertically
2123
import androidx.compose.ui.Modifier
@@ -50,8 +52,20 @@ fun AmountText(
5052
textStyle: TextStyle = CodeTheme.typography.h1,
5153
) {
5254
val centeredText = textStyle.copy(textAlign = TextAlign.Center)
53-
var scaledTextStyle by remember(amountText) { mutableStateOf(centeredText) }
54-
var isReadyToDraw by remember(amountText) { mutableStateOf(false) }
55+
56+
val cachedSize: TextUnit? by remember(amountText) {
57+
derivedStateOf { AmountSizeStore.lookup(amountText) }
58+
}
59+
60+
var scaledTextStyle by remember(amountText, cachedSize) {
61+
mutableStateOf(
62+
cachedSize?.let { centeredText.copy(fontSize = it) } ?: centeredText
63+
)
64+
}
65+
66+
var isReadyToDraw by remember(amountText) {
67+
mutableStateOf(AmountSizeStore.hasCachedSize(amountText))
68+
}
5569

5670
Row(
5771
modifier = Modifier

0 commit comments

Comments
 (0)