Skip to content

Commit

Permalink
fix country
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 28, 2024
1 parent d48e40b commit 2e47427
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tonapps.tonkeeper.extensions

import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.settings.SettingsRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import java.util.Locale

fun SettingsRepository.getNormalizeCountryFlow(api: API) = countryFlow.map { country ->
if (country.equals("AUTO", true) || country.isEmpty()) {
api.resolveCountry() ?: getLocale().country
} else {
country
}
}.flowOn(Dispatchers.IO)

fun SettingsRepository.getLocaleCountryFlow(api: API) = getNormalizeCountryFlow(api).map {
Locale("", it)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.tonapps.tonkeeper.ui.screen.browser.main

import android.app.Application
import com.tonapps.extensions.MutableEffectFlow
import com.tonapps.tonkeeper.extensions.getLocaleCountryFlow
import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.settings.SettingsRepository
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.map
import java.util.Locale

class BrowserMainViewModel(
app: Application,
private val settings: SettingsRepository
private val settings: SettingsRepository,
private val api: API,
): BaseWalletVM(app) {

private val _childBottomScrolled = MutableEffectFlow<Boolean>()
Expand All @@ -19,9 +20,7 @@ class BrowserMainViewModel(
private val _childTopScrolled = MutableEffectFlow<Boolean>()
val childTopScrolled = _childTopScrolled.asSharedFlow()

val countryFlow = settings.countryFlow.map {
Locale("", it)
}
val countryFlow = settings.getLocaleCountryFlow(api)

fun setBottomScrolled(value: Boolean) {
_childBottomScrolled.tryEmit(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CountryPickerViewModel(
}
}

private fun searchCountries(countries: List<Country>, selectedCountry: String, query: String): List<Item> {
private suspend fun searchCountries(countries: List<Country>, selectedCountry: String, query: String): List<Item> {
val filteredList = countries.filter { it.contains(query) }
val uiItems = mutableListOf<Item>()
for ((index, data) in filteredList.withIndex()) {
Expand All @@ -82,7 +82,7 @@ class CountryPickerViewModel(
return uiItems
}

private fun defaultCountries(countries: List<Country>, suggest: List<Country>, selectedCountry: String): List<Item> {
private suspend fun defaultCountries(countries: List<Country>, suggest: List<Country>, selectedCountry: String): List<Item> {
val uiItems = mutableListOf<Item>()
for ((index, data) in suggest.withIndex()) {
val position = ListCell.getPosition(suggest.size, index)
Expand All @@ -105,18 +105,32 @@ class CountryPickerViewModel(
settingsRepository.country = country
}

private fun createItem(
private suspend fun createItem(
position: ListCell.Position,
data: Country,
selectedCountry: String
): Item.Country {
val selected = data.code.equals(selectedCountry, ignoreCase = true)
val selected: Boolean
val emoji: String
val code: String

if (data.code.equals("auto", true)) {
val apiCountry = api.resolveCountry() ?: settingsRepository.getLocale().country
selected = selectedCountry.equals("auto", true)
code = apiCountry
emoji = apiCountry.countryEmoji
} else {
selected = data.code.equals(selectedCountry, ignoreCase = true)
code = data.code
emoji = data.emoji
}

return Item.Country(
position,
data.code,
data.name,
data.emoji,
selected
position = position,
code = code,
name = data.name,
emoji = emoji,
selected = selected
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.tonapps.tonkeeper.ui.screen.purchase

import android.app.Application
import com.tonapps.tonkeeper.extensions.getNormalizeCountryFlow
import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.tonkeeper.ui.screen.purchase.list.Item
import com.tonapps.uikit.list.ListCell
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.purchase.PurchaseRepository
import com.tonapps.wallet.data.purchase.entity.PurchaseMethodEntity
Expand All @@ -21,13 +23,14 @@ class PurchaseViewModel(
private val wallet: WalletEntity,
private val settingsRepository: SettingsRepository,
private val purchaseRepository: PurchaseRepository,
private val api: API,
): BaseWalletVM(app) {

enum class Tab {
BUY, SELL
}

val countryFlow = settingsRepository.countryFlow
val countryFlow = settingsRepository.getNormalizeCountryFlow(api)

private val _tabFlow = MutableStateFlow(Tab.BUY)
val tabFlow = _tabFlow.asStateFlow()
Expand Down

0 comments on commit 2e47427

Please sign in to comment.