From 4477d2cd476185df205e0102dc21023337a5e1a0 Mon Sep 17 00:00:00 2001 From: mdrlzy Date: Tue, 9 Jul 2024 23:22:11 +0600 Subject: [PATCH] revamped quick screen --- .../rate/presentation/quick/QuickScreen.kt | 111 ++++++++++-------- .../rate/presentation/quick/QuickViewModel.kt | 9 +- .../search/SearchCurrencyScreen.kt | 22 +--- .../rate/presentation/ui/ListHeader.kt | 24 ++++ app/src/main/res/values/strings.xml | 4 +- 5 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 app/src/main/java/dev/arkbuilders/rate/presentation/ui/ListHeader.kt diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt index 096cc8f3b..89e57d8b8 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt @@ -13,7 +13,6 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.offset @@ -66,6 +65,7 @@ import dev.arkbuilders.rate.presentation.ui.AppSwipeToDismiss import dev.arkbuilders.rate.presentation.ui.CurrIcon import dev.arkbuilders.rate.presentation.ui.CurrencyInfoItem import dev.arkbuilders.rate.presentation.ui.GroupViewPager +import dev.arkbuilders.rate.presentation.ui.ListHeader import dev.arkbuilders.rate.presentation.ui.LoadingScreen import dev.arkbuilders.rate.presentation.ui.NoResult import dev.arkbuilders.rate.presentation.ui.NotifyRemovedSnackbarVisuals @@ -183,28 +183,36 @@ private fun Content( ) { onFilterChanged(it) } - if (state.pages.size == 1) { - GroupPage( + if (state.filter.isNotEmpty()) { + SearchPage( filter = state.filter, - currencies = state.currencies, - quickPairs = state.pages.first().pairs, - onDelete = onDelete, - onLongClick = onLongClick, - onNewCode = onNewCode + frequent = state.frequent, + currencies = state.currencies ) } else { - GroupViewPager( - modifier = Modifier.padding(top = 20.dp), - groups = groups - ) { index -> + if (state.pages.size == 1) { GroupPage( - filter = state.filter, + frequent = state.frequent, currencies = state.currencies, - quickPairs = state.pages[index].pairs, + quickPairs = state.pages.first().pairs, onDelete = onDelete, onLongClick = onLongClick, onNewCode = onNewCode ) + } else { + GroupViewPager( + modifier = Modifier.padding(top = 20.dp), + groups = groups + ) { index -> + GroupPage( + frequent = state.frequent, + currencies = state.currencies, + quickPairs = state.pages[index].pairs, + onDelete = onDelete, + onLongClick = onLongClick, + onNewCode = onNewCode + ) + } } } } @@ -212,58 +220,69 @@ private fun Content( @Composable private fun GroupPage( - filter: String, + frequent: List, currencies: List, quickPairs: List, onDelete: (QuickPair) -> Unit, onLongClick: (QuickPair) -> Unit = {}, onNewCode: (CurrencyCode) -> Unit = {} ) { - val filteredPairs = quickPairs.filter { quick -> - val containsFrom = - quick.from.contains(filter, ignoreCase = true) - val containsTo = quick.to.any { amount -> - amount.code.contains( - filter, - ignoreCase = true + LazyColumn(modifier = Modifier.fillMaxSize()) { + item { + ListHeader(text = stringResource(R.string.quick_calculations)) + } + items(quickPairs, key = { it.id }) { + AppSwipeToDismiss( + content = { QuickItem(it, onLongClick) }, + onDelete = { onDelete(it) } ) + AppHorDiv16() + } + if (frequent.isNotEmpty()) { + item { + ListHeader(text = stringResource(R.string.frequent_currencies)) + } + items(frequent) { name -> + CurrencyInfoItem(name) { onNewCode(it.code) } + } + } + item { + ListHeader(text = stringResource(R.string.all_currencies)) + } + items(currencies, key = { it.code }) { name -> + CurrencyInfoItem(name) { onNewCode(it.code) } } - - containsFrom || containsTo } +} + +@Composable +private fun SearchPage( + filter: String, + frequent: List, + currencies: List, + onNewCode: (CurrencyCode) -> Unit = {} +) { val filteredCurrencies = currencies.filter { it.name.contains(filter, ignoreCase = true) || it.code.contains(filter, ignoreCase = true) } - if (filteredPairs.isNotEmpty() || filteredCurrencies.isNotEmpty()) { + val filteredFrequent = frequent.filter { + it.name.contains(filter, ignoreCase = true) + || it.code.contains(filter, ignoreCase = true) + } + if (filteredCurrencies.isNotEmpty() || filteredFrequent.isNotEmpty()) { LazyColumn(modifier = Modifier.fillMaxSize()) { - if (filteredPairs.isNotEmpty()) { + if (filteredFrequent.isNotEmpty()) { item { - Text( - modifier = Modifier.padding(start = 16.dp, top = 24.dp), - text = stringResource(R.string.quick_calculations), - color = ArkColor.TextTertiary, - fontWeight = FontWeight.Medium - ) - AppHorDiv16(modifier = Modifier.padding(top = 12.dp)) + ListHeader(text = stringResource(R.string.frequent_currencies)) } - items(filteredPairs, key = { it.id }) { - AppSwipeToDismiss( - content = { QuickItem(it, onLongClick) }, - onDelete = { onDelete(it) } - ) - AppHorDiv16() + items(filteredFrequent) { name -> + CurrencyInfoItem(name) { onNewCode(it.code) } } } if (filteredCurrencies.isNotEmpty()) { item { - Text( - modifier = Modifier.padding(start = 16.dp, top = 24.dp), - text = stringResource(R.string.currencies), - color = ArkColor.TextTertiary, - fontWeight = FontWeight.Medium - ) - AppHorDiv16(modifier = Modifier.padding(top = 12.dp)) + ListHeader(text = stringResource(R.string.all_currencies)) } items(filteredCurrencies, key = { it.code }) { name -> CurrencyInfoItem(name) { onNewCode(it.code) } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickViewModel.kt index fec101280..b67dfbc66 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickViewModel.kt @@ -13,6 +13,7 @@ import dev.arkbuilders.rate.domain.repo.CurrencyRepo import dev.arkbuilders.rate.domain.repo.PortfolioRepo import dev.arkbuilders.rate.domain.repo.Prefs import dev.arkbuilders.rate.domain.repo.QuickRepo +import dev.arkbuilders.rate.domain.usecase.CalcFrequentCurrUseCase import dev.arkbuilders.rate.domain.usecase.ConvertWithRateUseCase import dev.arkbuilders.rate.presentation.shared.AppSharedFlow import dev.arkbuilders.rate.presentation.ui.NotifyAddedSnackbarVisuals @@ -34,6 +35,7 @@ data class QuickScreenPage( data class QuickScreenState( val filter: String = "", val currencies: List = emptyList(), + val frequent: List = emptyList(), val pages: List = emptyList(), val initialized: Boolean = false ) @@ -51,6 +53,7 @@ class QuickViewModel( private val quickRepo: QuickRepo, private val prefs: Prefs, private val convertUseCase: ConvertWithRateUseCase, + private val calcFrequentCurrUseCase: CalcFrequentCurrUseCase, private val analyticsManager: AnalyticsManager, ) : ViewModel(), ContainerHost { override val container: Container = @@ -81,8 +84,10 @@ class QuickViewModel( }.launchIn(viewModelScope) val names = currencyRepo.getCurrencyName().getOrNull()!! + val frequent = calcFrequentCurrUseCase.invoke() + .map { currencyRepo.nameByCodeUnsafe(it) } reduce { - state.copy(currencies = names) + state.copy(currencies = names, frequent = frequent) } } } @@ -111,6 +116,7 @@ class QuickViewModelFactory @AssistedInject constructor( private val currencyRepo: CurrencyRepo, private val prefs: Prefs, private val convertUseCase: ConvertWithRateUseCase, + private val calcFrequentCurrUseCase: CalcFrequentCurrUseCase, private val analyticsManager: AnalyticsManager, ) : ViewModelProvider.Factory { override fun create(modelClass: Class): T { @@ -120,6 +126,7 @@ class QuickViewModelFactory @AssistedInject constructor( quickRepo, prefs, convertUseCase, + calcFrequentCurrUseCase, analyticsManager ) as T } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/search/SearchCurrencyScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/search/SearchCurrencyScreen.kt index 9d1edd433..1a1f9b5d5 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/search/SearchCurrencyScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/search/SearchCurrencyScreen.kt @@ -32,6 +32,7 @@ import dev.arkbuilders.rate.presentation.ui.AppHorDiv import dev.arkbuilders.rate.presentation.ui.AppHorDiv16 import dev.arkbuilders.rate.presentation.ui.AppTopBarBack import dev.arkbuilders.rate.presentation.ui.CurrencyInfoItem +import dev.arkbuilders.rate.presentation.ui.ListHeader import dev.arkbuilders.rate.presentation.ui.LoadingScreen import dev.arkbuilders.rate.presentation.ui.NoResult import dev.arkbuilders.rate.presentation.ui.SearchTextField @@ -106,7 +107,7 @@ private fun Results( filter.isNotEmpty() -> { if (topResultsFiltered.isNotEmpty()) { LazyColumn { - item { Header(header = "Top results") } + item { ListHeader(stringResource(R.string.top_results)) } items(topResultsFiltered) { name -> CurrencyInfoItem(name) { onClick(it) } } @@ -119,12 +120,12 @@ private fun Results( else -> { LazyColumn { if (frequent.isNotEmpty()) { - item { Header(header = "Frequent currencies") } + item { ListHeader(stringResource(R.string.frequent_currencies)) } items(frequent) { name -> CurrencyInfoItem(name) { onClick(it) } } } - item { Header(header = "All currencies") } + item { ListHeader(stringResource(R.string.all_currencies)) } items(all) { name -> CurrencyInfoItem(name) { onClick(it) } } @@ -133,19 +134,4 @@ private fun Results( } } -@Composable -private fun Header(header: String) { - Text( - modifier = Modifier.padding( - start = 16.dp, - top = 24.dp, - end = 16.dp, - ), - text = header, - fontWeight = FontWeight.Medium, - color = ArkColor.TextTertiary - ) - AppHorDiv16(modifier = Modifier.padding(top = 12.dp)) -} - diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/ui/ListHeader.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/ui/ListHeader.kt new file mode 100644 index 000000000..265f16d87 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/ui/ListHeader.kt @@ -0,0 +1,24 @@ +package dev.arkbuilders.rate.presentation.ui + +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import dev.arkbuilders.rate.presentation.theme.ArkColor + +@Composable +fun ListHeader(text: String) { + Text( + modifier = Modifier.padding( + start = 16.dp, + top = 24.dp, + end = 16.dp, + ), + text = text, + fontWeight = FontWeight.Medium, + color = ArkColor.TextTertiary + ) + AppHorDiv16(modifier = Modifier.padding(top = 12.dp)) +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3fd3c367b..68329eaa9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -64,7 +64,7 @@ New group Crash reports - Currencies + All currencies OK Oops! Something went wrong @@ -133,5 +133,7 @@ Search a currency New Asset Undo + Frequent currencies + Top results