Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {
applicationId = "ltd.grunt.brainwallet"
minSdk = 29
targetSdk = 35
versionCode = 202506342
versionName = "v4.10.4"
versionCode = 202506343
versionName = "v4.10.5"
multiDexEnabled = true
base.archivesName.set("${defaultConfig.versionName}(${defaultConfig.versionCode})")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Learned something here.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.brainwallet.ui.BrainwalletViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.koin.android.annotation.KoinViewModel
Expand All @@ -26,41 +27,37 @@ class ShopBentoViewModel(

val currentCountryISO: String = Locale.getDefault().country.ifEmpty { "US" }
init {
viewModelScope.launch {
settingRepository.settings.collect { setting ->
_state.update {
it.copy(
darkMode = setting.isDarkMode,
countryIso = currentCountryISO
)
}
}
}
viewModelScope.launch {
shopProxyRepository.refresh()
shopProxyRepository.shopProxy.collect { shopList ->
val widget = shopList.firstOrNull()?.widget.orEmpty()
val cards = shopList.firstOrNull()?.shopCards.orEmpty()
.filter { it.countryCode == currentCountryISO }
var imageUrl1 = ""
var imageUrl2 = ""
var imageUrl3 = ""
combine(
settingRepository.settings,
shopProxyRepository.shopProxy
) { setting, shopList -> setting to shopList }
.collect { (setting, shopList) ->
val widget = shopList.firstOrNull()?.widget.orEmpty()
val cards = shopList.firstOrNull()?.shopCards.orEmpty()
.filter { it.countryCode == currentCountryISO }
var imageUrl1 = ""
var imageUrl2 = ""
var imageUrl3 = ""

if (cards.count() >= 3) {
imageUrl1 = cards[0].cardImageWebP
imageUrl2 = cards[1].cardImageWebP
imageUrl3 = cards[2].cardImageWebP
}
_state.update {
it.copy(
shopBaseUrl = widget,
shopCards = cards,
cardImageURL1 = imageUrl1,
cardImageURL2 = imageUrl2,
cardImageURL3 = imageUrl3
)
if (cards.count() >= 3) {
imageUrl1 = cards[0].cardImageWebP
imageUrl2 = cards[1].cardImageWebP
imageUrl3 = cards[2].cardImageWebP
}
_state.update {
it.copy(
darkMode = setting.isDarkMode,
countryIso = currentCountryISO,
shopBaseUrl = widget,
shopCards = cards,
cardImageURL1 = imageUrl1,
cardImageURL2 = imageUrl2,
cardImageURL3 = imageUrl3
)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import com.brainwallet.data.repository.SettingRepository
import com.brainwallet.data.repository.ShopProxy
import com.brainwallet.data.repository.ShopProxyRepository
import com.brainwallet.testing.FlakyTest
import com.brainwallet.util.MainDispatcherRule
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import io.mockk.coEvery

class ShopBentoViewModelTest {

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

private lateinit var app: Application
private lateinit var settingRepository: SettingRepository
private lateinit var shopProxyRepository: ShopProxyRepository
Expand Down Expand Up @@ -62,16 +68,16 @@ class ShopBentoViewModelTest {
@Test
fun `init - sets shopBaseUrl from widget`() = runTest {
turbineScope {
shopProxyFlow.emit(listOf(ShopProxy(widget = "https://shop.example.com", shopCards = emptyList())))
shopProxyFlow.emit(listOf(ShopProxy(widget = "https://embed.bitrefill.com", shopCards = emptyList())))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary to change but used a more relevant domain


val viewModel = buildViewModel()
val turbine = viewModel.state.testIn(backgroundScope)

settingsFlow.emit(AppSetting())
advanceTimeBy(100)
advanceUntilIdle()

val state = turbine.expectMostRecentItem()
assertEquals("https://shop.example.com", state.shopBaseUrl)
assertEquals("https://embed.bitrefill.com", state.shopBaseUrl)
turbine.cancelAndIgnoreRemainingEvents()
}
}
Expand Down
Loading