Skip to content

Commit

Permalink
revamped quick screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrlzy committed Jul 9, 2024
1 parent 7e0278c commit 4477d2c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -183,87 +183,106 @@ 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
)
}
}
}
}
}

@Composable
private fun GroupPage(
filter: String,
frequent: List<CurrencyName>,
currencies: List<CurrencyName>,
quickPairs: List<QuickPair>,
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<CurrencyName>,
currencies: List<CurrencyName>,
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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,7 @@ data class QuickScreenPage(
data class QuickScreenState(
val filter: String = "",
val currencies: List<CurrencyName> = emptyList(),
val frequent: List<CurrencyName> = emptyList(),
val pages: List<QuickScreenPage> = emptyList(),
val initialized: Boolean = false
)
Expand All @@ -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<QuickScreenState, QuickScreenEffect> {
override val container: Container<QuickScreenState, QuickScreenEffect> =
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
Expand All @@ -120,6 +126,7 @@ class QuickViewModelFactory @AssistedInject constructor(
quickRepo,
prefs,
convertUseCase,
calcFrequentCurrUseCase,
analyticsManager
) as T
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}
Expand All @@ -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) }
}
Expand All @@ -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))
}


Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<string name="new_group">New group</string>

<string name="crash_reports">Crash reports</string>
<string name="currencies">Currencies</string>
<string name="all_currencies">All currencies</string>

<string name="ok">OK</string>
<string name="oops_something_went_wrong">Oops! Something went wrong</string>
Expand Down Expand Up @@ -133,5 +133,7 @@
<string name="search_currency">Search a currency</string>
<string name="new_asset">New Asset</string>
<string name="undo">Undo</string>
<string name="frequent_currencies">Frequent currencies</string>
<string name="top_results">Top results</string>

</resources>

0 comments on commit 4477d2c

Please sign in to comment.