Skip to content

fix(balance): prevent unneccesary recomp/flicker of balance when open… #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions api/src/main/java/com/getcode/network/BalanceController.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.getcode.network

import android.content.Context
import android.icu.text.NumberFormat
import android.icu.text.NumberFormat.INTEGERSTYLE
import com.getcode.manager.SessionManager
import com.getcode.model.Currency
import com.getcode.model.CurrencyCode
import com.getcode.model.KinAmount
import com.getcode.model.PrefsString
import com.getcode.model.Rate
import com.getcode.network.client.TransactionReceiver
Expand All @@ -26,12 +23,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
Expand All @@ -42,17 +37,16 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import timber.log.Timber
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.math.roundToInt

data class BalanceDisplay(
val marketValue: Double = 0.0,
val formattedValue: String = "",
val currency: Currency? = null,

)

open class BalanceController @Inject constructor(
exchange: Exchange,
networkObserver: NetworkConnectivityListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.getcode.ed25519.Ed25519.KeyPair
import com.getcode.model.Chat
import com.getcode.model.ChatMessage
import com.getcode.model.Cursor
import com.getcode.model.ID
import com.getcode.network.client.Client
import com.getcode.network.client.fetchMessagesFor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ data object BalanceModal : ChatGraph, ModalRoot {

LifecycleEffect(
onDisposed = {
viewModel.dispatchEvent(
BalanceSheetViewModel.Event.OnDebugBucketsVisible(
false
val disposedScreen = navigator.lastItem
if (disposedScreen !is BalanceModal) {
viewModel.dispatchEvent(
BalanceSheetViewModel.Event.OnDebugBucketsVisible(false)
)
)
}
}
)
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/getcode/ui/components/chat/ChatInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.getcode.theme.CodeTheme
import com.getcode.theme.extraLarge
import com.getcode.theme.inputColors
import com.getcode.ui.components.TextInput
import com.getcode.ui.utils.rememberedClickable
import com.getcode.ui.utils.withTopBorder

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -64,7 +65,7 @@ fun ChatInput(
.align(Alignment.Bottom)
.border(width = 1.dp, color = Color.White, shape = CircleShape)
.clip(CircleShape)
.clickable { onSendCash() }
.rememberedClickable { onSendCash() }
.size(ChatInput_Size)
.padding(8.dp),
contentAlignment = Alignment.Center,
Expand Down Expand Up @@ -110,7 +111,7 @@ fun ChatInput(
.align(Alignment.Bottom)
.background(ChatOutgoing, shape = CircleShape)
.clip(CircleShape)
.clickable { onSendMessage() }
.rememberedClickable { onSendMessage() }
.size(ChatInput_Size)
.padding(8.dp),
contentAlignment = Alignment.Center,
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/getcode/ui/components/chat/ChatNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.getcode.theme.CodeTheme
import com.getcode.ui.components.Badge
import com.getcode.ui.components.chat.utils.localizedText
import com.getcode.ui.utils.debugBounds
import com.getcode.ui.utils.rememberedClickable
import com.getcode.util.DateUtils
import com.getcode.util.formatTimeRelatively
import com.getcode.util.toInstantFromMillis
Expand All @@ -44,7 +45,7 @@ fun ChatNode(
) {
Column(
modifier = modifier
.clickable { onClick() }
.rememberedClickable { onClick() }
.padding(
vertical = CodeTheme.dimens.grid.x3,
horizontal = CodeTheme.dimens.inset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.getcode.solana.organizer.AccountType
import com.getcode.theme.BrandLight
import com.getcode.theme.CodeTheme
import com.getcode.ui.components.MiddleEllipsisText
import com.getcode.ui.utils.rememberedClickable


@Composable
Expand All @@ -41,7 +42,7 @@ fun BucketDebugger() {
items(buckets) { info ->
Column(
modifier = Modifier
.clickable {
.rememberedClickable {
clipboard.setText(AnnotatedString(info.address.base58()))
}
.padding(horizontal = CodeTheme.dimens.grid.x3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -226,8 +227,9 @@ fun BalanceTop(
isClickable: Boolean,
onClick: () -> Unit = {}
) {
val text = rememberSaveable(state.amountText) { state.amountText }
AmountArea(
amountText = state.amountText,
amountText = text,
isAltCaption = false,
isAltCaptionKinIcon = false,
isLoading = state.chatsLoading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import com.getcode.view.main.getKin.GetKinSheetViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.cache
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import timber.log.Timber
import javax.inject.Inject

Expand Down Expand Up @@ -60,9 +62,7 @@ class BalanceSheetViewModel @Inject constructor(
data class OnDebugBucketsVisible(val show: Boolean) : Event
data class OnBuyModuleStateChanged(val module: Feature) : Event
data class OnCurrencySelectionStateChanged(val module: Feature): Event
data class OnLatestRateChanged(
val rate: Rate,
) : Event
data class OnLatestRateChanged(val rate: Rate) : Event

data class OnBalanceChanged(
val flagResId: Int?,
Expand Down Expand Up @@ -92,6 +92,7 @@ class BalanceSheetViewModel @Inject constructor(

balanceController.formattedBalance
.filterNotNull()
.distinctUntilChanged()
.onEach {
dispatchEvent(
Dispatchers.Main,
Expand All @@ -102,7 +103,8 @@ class BalanceSheetViewModel @Inject constructor(
isKin = it.currency == Currency.Kin
)
)
}.launchIn(viewModelScope)
}
.launchIn(viewModelScope)

historyController.chats
.onEach {
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/com/getcode/view/main/giveKin/AmountText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -50,8 +52,20 @@ fun AmountText(
textStyle: TextStyle = CodeTheme.typography.h1,
) {
val centeredText = textStyle.copy(textAlign = TextAlign.Center)
var scaledTextStyle by remember(amountText) { mutableStateOf(centeredText) }
var isReadyToDraw by remember(amountText) { mutableStateOf(false) }

val cachedSize: TextUnit? by remember(amountText) {
derivedStateOf { AmountSizeStore.lookup(amountText) }
}

var scaledTextStyle by remember(amountText, cachedSize) {
mutableStateOf(
cachedSize?.let { centeredText.copy(fontSize = it) } ?: centeredText
)
}

var isReadyToDraw by remember(amountText) {
mutableStateOf(AmountSizeStore.hasCachedSize(amountText))
}

Row(
modifier = Modifier
Expand Down
Loading