Skip to content

Commit e490125

Browse files
committed
chore(token/info): streamline and tie trend determination better to data changes
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent a161a56 commit e490125

5 files changed

Lines changed: 17 additions & 13 deletions

File tree

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/TokenInfoScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TokenInfoScreen(private val mint: Mint) : AppScreen(), ModalScreen, Parcel
8686

8787
TokenInfoScreen(viewModel)
8888

89-
LaunchedEffect(viewModel, mint) {
89+
LaunchedEffect(Unit) {
9090
viewModel.dispatchEvent(TokenInfoViewModel.Event.OnMintProvided(mint))
9191
}
9292

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/TokenInfoScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private fun TokenInfoScreen(
147147
// market cap
148148
state.marketCap?.let { mcap ->
149149
val loadable = state.historicalMarketCapData[state.selectedPeriod] ?: Loadable.Loaded(emptyList())
150-
item {
150+
item(key = "mcap") {
151151
MarketCapSection(
152152
modifier = Modifier
153153
.fillParentMaxWidth(),

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/components/marketcap/MarketCapChart.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ internal fun MarketCapChart(
9696
}
9797
}
9898

99+
var trend by remember { mutableStateOf<LineTrend>(LineTrend.Up) }
100+
99101
// Update the model when the window changes
100102
LaunchedEffect(windowedData) {
101103
if (windowedData.isNotEmpty()) {
104+
// Update trend BEFORE the model transaction
105+
trend = trendType.determineTrend(windowedData.yValues)
106+
102107
modelProducer.runTransaction {
103108
lineSeries {
104109
series(
@@ -110,12 +115,6 @@ internal fun MarketCapChart(
110115
}
111116
}
112117

113-
val trend by remember(windowedData) {
114-
derivedStateOf {
115-
trendType.determineTrend(windowedData.yValues)
116-
}
117-
}
118-
119118
MarketCapChart(
120119
modelProducer = modelProducer,
121120
trend = trend,

apps/flipcash/shared/tokens/src/main/kotlin/TokenInfoViewModel.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ import com.getcode.util.resources.ResourceHelper
4545
import com.getcode.view.BaseViewModel2
4646
import dagger.hilt.android.lifecycle.HiltViewModel
4747
import kotlinx.coroutines.flow.combine
48+
import kotlinx.coroutines.flow.debounce
4849
import kotlinx.coroutines.flow.distinctUntilChanged
50+
import kotlinx.coroutines.flow.drop
4951
import kotlinx.coroutines.flow.filterIsInstance
5052
import kotlinx.coroutines.flow.flatMapLatest
5153
import kotlinx.coroutines.flow.flowOf
@@ -134,6 +136,7 @@ class TokenInfoViewModel @Inject constructor(
134136
eventFlow
135137
.filterIsInstance<Event.OnMintProvided>()
136138
.map { it.mint }
139+
.distinctUntilChanged()
137140
.map { tokenController.getTokenMetadata(it) }
138141
.onResult(
139142
onSuccess = {
@@ -192,13 +195,14 @@ class TokenInfoViewModel @Inject constructor(
192195
usdf = balance,
193196
nativeAmount = balance.convertingTo(rate),
194197
)
195-
}.onEach {
198+
}.distinctUntilChanged().onEach {
196199
dispatchEvent(Event.OnReservesUpdated(it))
197200
}.launchIn(viewModelScope)
198201

199202

200-
stateFlow
201-
.map { it.selectedPeriod }
203+
eventFlow
204+
.filterIsInstance<Event.OnMarketCapPeriodSelected>()
205+
.map { it.period }
202206
.distinctUntilChanged()
203207
.onEach { dispatchEvent(Event.LoadHistoricalDataForPeriod(it)) }
204208
.launchIn(viewModelScope)
@@ -263,7 +267,7 @@ class TokenInfoViewModel @Inject constructor(
263267
) { usdMcap, rate ->
264268
usdMcap?.convertingTo(rate)
265269
}
266-
}.onEach {
270+
}.distinctUntilChanged().onEach {
267271
dispatchEvent(Event.OnMarketCapChanged(it))
268272
}
269273
.launchIn(viewModelScope)
@@ -374,6 +378,7 @@ class TokenInfoViewModel @Inject constructor(
374378

375379
companion object {
376380
val updateStateForEvent: (Event) -> ((State) -> State) = { event ->
381+
println("TOKEN INFO EVENT: ${event.javaClass.simpleName}")
377382
when (event) {
378383
is Event.CashReservesEnabled -> { state -> state.copy(cashReservesEnabled = event.enabled) }
379384
is Event.MarketCapChartEnabled -> { state -> state.copy(marketCapChartEnabled = event.enabled) }

services/opencode/src/main/kotlin/com/getcode/opencode/controllers/TokenController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class TokenController @Inject constructor(
311311
_state.value.tokens[mint]?.let { cached ->
312312
trace(
313313
tag = TAG,
314-
message = "Token metadata cache hit for ${cached.symbol} (${mint.base58()}...)",
314+
message = "Token metadata cache hit for ${cached.symbol}",
315315
type = TraceType.Process
316316
)
317317
return Result.success(TokenResult(cached, DataSource.Cache))

0 commit comments

Comments
 (0)