Skip to content

Commit

Permalink
Flexa hidden behind feature flag (#1676)
Browse files Browse the repository at this point in the history
* Flexa hidden behind feature flag

* Remove unused flag IS_FIAT_CONVERSION_ENABLED

* Code cleanup

* Kotlin flow handling

---------

Co-authored-by: Honza <[email protected]>
  • Loading branch information
Milan-Cerovsky and HonzaR authored Nov 13, 2024
1 parent 882605d commit d4be4a5
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,6 @@ fun Reference(
}
}

@Composable
fun BodyWithFiatCurrencySymbol(
amount: String,
modifier: Modifier = Modifier
) {
Text(
text = amount,
style = MaterialTheme.typography.bodyLarge,
color = ZcashTheme.colors.textPrimary,
modifier = modifier
)
}

@Preview
@Composable
private fun NavigationTabTextPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.util.concurrent.atomic.AtomicInteger
class BalancesTestSetup(
private val composeTestRule: ComposeContentTestRule,
private val walletSnapshot: WalletSnapshot,
private val isShowFiatConversion: Boolean
) {
private val onSettingsCount = AtomicInteger(0)

Expand Down Expand Up @@ -43,7 +42,6 @@ class BalancesTestSetup(
showStatusDialog = null,
onStatusClick = {},
snackbarHostState = SnackbarHostState(),
isFiatConversionEnabled = isShowFiatConversion,
isUpdateAvailable = false,
isShowingErrorDialog = false,
setShowErrorDialog = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class BalancesViewIntegrationTest : UiTestPrerequisites() {
BalancesTestSetup(
composeTestRule,
walletSnapshot,
isShowFiatConversion = true
)

// This is just basic sanity check that we still have UI set up as expected after the state restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import co.electriccoin.zcash.test.UiTestPrerequisites
import co.electriccoin.zcash.ui.common.model.WalletSnapshot
import co.electriccoin.zcash.ui.design.component.CommonTag
import co.electriccoin.zcash.ui.fixture.WalletSnapshotFixture
import co.electriccoin.zcash.ui.screen.balances.BalancesTag
import co.electriccoin.zcash.ui.screen.balances.BalancesTestSetup
import co.electriccoin.zcash.ui.screen.send.clickSettingsTopAppBarMenu
import org.junit.Assert
Expand All @@ -23,6 +22,14 @@ class BalancesViewTest : UiTestPrerequisites() {
@get:Rule
val composeTestRule = createComposeRule()

private fun newTestSetup(walletSnapshot: WalletSnapshot = WalletSnapshotFixture.new()) =
BalancesTestSetup(
composeTestRule,
walletSnapshot = walletSnapshot,
).apply {
setDefaultContent()
}

@Test
@MediumTest
fun check_all_elementary_ui_elements_displayed() {
Expand All @@ -34,16 +41,6 @@ class BalancesViewTest : UiTestPrerequisites() {
}
}

@Test
@MediumTest
fun hide_fiat_conversion() {
newTestSetup(isShowFiatConversion = false)

composeTestRule.onNodeWithTag(BalancesTag.FIAT_CONVERSION).also {
it.assertDoesNotExist()
}
}

@Test
@MediumTest
fun hamburger_settings_test() {
Expand All @@ -55,15 +52,4 @@ class BalancesViewTest : UiTestPrerequisites() {

Assert.assertEquals(1, testSetup.getOnSettingsCount())
}

private fun newTestSetup(
isShowFiatConversion: Boolean = true,
walletSnapshot: WalletSnapshot = WalletSnapshotFixture.new()
) = BalancesTestSetup(
composeTestRule,
walletSnapshot = walletSnapshot,
isShowFiatConversion = isShowFiatConversion
).apply {
setDefaultContent()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import co.electriccoin.zcash.ui.common.usecase.ObserveConfigurationUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveContactByAddressUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveContactPickedUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveFastestServersUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveIsFlexaAvailableUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveSelectedEndpointUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveSynchronizerUseCase
import co.electriccoin.zcash.ui.common.usecase.ObserveWalletStateUseCase
Expand Down Expand Up @@ -63,6 +64,7 @@ val useCaseModule =
singleOf(::GetAddressesUseCase)
singleOf(::CopyToClipboardUseCase)
singleOf(::IsFlexaAvailableUseCase)
singleOf(::ObserveIsFlexaAvailableUseCase)
singleOf(::ShareImageUseCase)
singleOf(::Zip321BuildUriUseCase)
singleOf(::Zip321ProposalFromUriUseCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,59 @@ package co.electriccoin.zcash.ui.common.repository
import cash.z.ecc.sdk.ANDROID_STATE_FLOW_TIMEOUT
import co.electriccoin.zcash.configuration.api.ConfigurationProvider
import co.electriccoin.zcash.configuration.model.map.Configuration
import co.electriccoin.zcash.ui.BuildConfig
import co.electriccoin.zcash.ui.common.provider.GetVersionInfoProvider
import co.electriccoin.zcash.ui.configuration.ConfigurationEntries
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.WhileSubscribed
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

interface ConfigurationRepository {
val configurationFlow: StateFlow<Configuration?>

/**
* Returns true if Flexa is available, false otherwise & null if loading.
*/
val isFlexaAvailable: StateFlow<Boolean?>

suspend fun isFlexaAvailable(): Boolean
}

class ConfigurationRepositoryImpl(androidConfigurationProvider: ConfigurationProvider) : ConfigurationRepository {
class ConfigurationRepositoryImpl(
androidConfigurationProvider: ConfigurationProvider,
private val getVersionInfo: GetVersionInfoProvider,
) : ConfigurationRepository {
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

override val configurationFlow: StateFlow<Configuration?> =
androidConfigurationProvider.getConfigurationFlow()
.stateIn(
scope,
SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT.inWholeMilliseconds),
null
scope = scope,
started = SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT),
initialValue = null
)

override val isFlexaAvailable: StateFlow<Boolean?> =
configurationFlow
.filterNotNull()
.map {
val versionInfo = getVersionInfo()
!versionInfo.isTestnet &&
ConfigurationEntries.IS_FLEXA_AVAILABLE.getValue(it) &&
BuildConfig.ZCASH_FLEXA_KEY.isNotEmpty()
}
.stateIn(
scope = scope,
started = SharingStarted.WhileSubscribed(ANDROID_STATE_FLOW_TIMEOUT),
initialValue = null
)

override suspend fun isFlexaAvailable(): Boolean = isFlexaAvailable.filterNotNull().first()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ interface FlexaRepository {
class FlexaRepositoryImpl(
private val balanceRepository: BalanceRepository,
private val application: Application,
private val configurationRepository: ConfigurationRepository,
) : FlexaRepository {
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

private val publishableKey: String?
get() = BuildConfig.ZCASH_FLEXA_KEY.takeIf { it.isNotEmpty() }

override fun init() {
if (publishableKey == null) return

scope.launch {
if (!configurationRepository.isFlexaAvailable()) return@launch
val configuration = getFlexaClientConfiguration()
if (configuration != null) {
Flexa.init(configuration)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package co.electriccoin.zcash.ui.common.usecase

import co.electriccoin.zcash.ui.BuildConfig
import co.electriccoin.zcash.ui.common.provider.GetVersionInfoProvider
import co.electriccoin.zcash.ui.common.repository.ConfigurationRepository

class IsFlexaAvailableUseCase(
private val getVersionInfo: GetVersionInfoProvider,
private val configurationRepository: ConfigurationRepository
) {
operator fun invoke(): Boolean {
val versionInfo = getVersionInfo()
val isDebug = versionInfo.let { it.isDebuggable && !it.isRunningUnderTestService }
return !versionInfo.isTestnet && (BuildConfig.ZCASH_FLEXA_KEY.isNotEmpty() || isDebug)
}
suspend operator fun invoke() = configurationRepository.isFlexaAvailable()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.electriccoin.zcash.ui.common.usecase

import co.electriccoin.zcash.ui.common.repository.ConfigurationRepository

class ObserveIsFlexaAvailableUseCase(
private val configurationRepository: ConfigurationRepository
) {
operator fun invoke() = configurationRepository.isFlexaAvailable
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import cash.z.ecc.sdk.type.fromResources
import co.electriccoin.zcash.preference.EncryptedPreferenceProvider
import co.electriccoin.zcash.preference.StandardPreferenceProvider
import co.electriccoin.zcash.spackle.Twig
import co.electriccoin.zcash.ui.BuildConfig
import co.electriccoin.zcash.ui.common.model.OnboardingState
import co.electriccoin.zcash.ui.common.model.WalletRestoringState
import co.electriccoin.zcash.ui.common.model.WalletSnapshot
Expand Down Expand Up @@ -50,8 +49,6 @@ import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

// To make this more multiplatform compatible, we need to remove the dependency on Context
// for loading the preferences.
Expand Down Expand Up @@ -271,15 +268,11 @@ class WalletViewModel(
}
}

private suspend fun disconnectFlexa() =
suspendCoroutine { cont ->
if (isFlexaAvailable() && BuildConfig.ZCASH_FLEXA_KEY.isNotEmpty()) {
Flexa.buildIdentity().build().disconnect()
cont.resume(Unit)
} else {
cont.resume(Unit)
}
private suspend inline fun disconnectFlexa() {
if (isFlexaAvailable()) {
Flexa.buildIdentity().build().disconnect()
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import co.electriccoin.zcash.configuration.model.entry.ConfigKey
object ConfigurationEntries {
val IS_APP_UPDATE_CHECK_ENABLED = BooleanConfigurationEntry(ConfigKey("is_update_check_enabled"), true)

/*
* This isn't fully implemented yet, so it is disabled from being shown.
*/
val IS_FIAT_CONVERSION_ENABLED = BooleanConfigurationEntry(ConfigKey("is_fiat_conversion_enabled"), false)

/*
* A troubleshooting step. If we fix our bugs, this should be unnecessary.
*/
val IS_RESCAN_ENABLED = BooleanConfigurationEntry(ConfigKey("is_rescan_enabled"), true)

val IS_FLEXA_AVAILABLE = BooleanConfigurationEntry(ConfigKey("is_flexa_available"), false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import co.electriccoin.zcash.ui.common.model.WalletSnapshot
import co.electriccoin.zcash.ui.common.viewmodel.CheckUpdateViewModel
import co.electriccoin.zcash.ui.common.viewmodel.HomeViewModel
import co.electriccoin.zcash.ui.common.viewmodel.WalletViewModel
import co.electriccoin.zcash.ui.configuration.ConfigurationEntries
import co.electriccoin.zcash.ui.configuration.RemoteConfig
import co.electriccoin.zcash.ui.design.component.CircularScreenProgressIndicator
import co.electriccoin.zcash.ui.screen.balances.model.ShieldState
import co.electriccoin.zcash.ui.screen.balances.model.StatusAction
Expand Down Expand Up @@ -133,8 +131,6 @@ internal fun WrapBalances(
it?.appUpdateInfo != null && it.state == UpdateState.Prepared
}

val isFiatConversionEnabled = ConfigurationEntries.IS_FIAT_CONVERSION_ENABLED.getValue(RemoteConfig.current)

val (shieldState, setShieldState) =
rememberSaveable(stateSaver = ShieldState.Saver) { mutableStateOf(ShieldState.None) }

Expand Down Expand Up @@ -171,7 +167,6 @@ internal fun WrapBalances(
} else {
Balances(
balanceState = balanceState,
isFiatConversionEnabled = isFiatConversionEnabled,
isHideBalances = isHideBalances,
isUpdateAvailable = isUpdateAvailable,
onHideBalances = onHideBalances,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ package co.electriccoin.zcash.ui.screen.balances
*/
object BalancesTag {
const val STATUS = "status"
const val FIAT_CONVERSION = "fiat_conversion"
}
Loading

0 comments on commit d4be4a5

Please sign in to comment.