Skip to content

Commit

Permalink
sca-133 soracard
Browse files Browse the repository at this point in the history
  • Loading branch information
arvifox committed Oct 8, 2023
1 parent 07642fd commit 643fa3e
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 159 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ buildscript {
composeCompiler : '1.5.3',
composeConstraintLayout: '1.1.0-alpha05',
uiCore : '0.2.7',
soraCard : '0.1.53',
soraCard : '0.1.54',
lazySodium : '5.0.2',
jna : '5.8.0',
accompanist : '0.30.1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ data class CardsState(
val cards: List<CardState> = emptyList(),
)

sealed interface CardState
sealed class CardState(
open val loading: Boolean,
)

sealed interface AssetCardState

Expand All @@ -62,7 +64,8 @@ data class TitledAmountCardState(
val collapsedState: Boolean = false,
val onCollapseClick: () -> Unit,
val onExpandClick: (() -> Unit)? = null,
) : CardState
override val loading: Boolean,
) : CardState(loading)

class FavoriteAssetsCardState(
val assets: List<AssetItemCardState>
Expand Down Expand Up @@ -118,18 +121,22 @@ fun mapAssetsToCardState(
}

data class SoraCardState(
val balance: String?,
val success: Boolean,
val ibanBalance: String?,
val kycStatus: String?,
val visible: Boolean = false,
) : CardState
override val loading: Boolean,
) : CardState(loading)

data class BuyXorState(
val visible: Boolean = false,
) : CardState
override val loading: Boolean,
) : CardState(loading)

data class ReferralState(
val visible: Boolean = false,
) : CardState
override val loading: Boolean,
) : CardState(loading)

class FavoritePoolsCardState(
val state: PoolsListState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import android.view.View
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ScrollState
import androidx.fragment.app.viewModels
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
Expand Down Expand Up @@ -84,9 +85,10 @@ class GetSoraCardFragment : SoraBaseFragment<GetSoraCardViewModel>() {
composable(
route = theOnlyRoute,
) {
val state = viewModel.state.collectAsStateWithLifecycle().value
GetSoraCardScreen(
scrollState = scrollState,
state = viewModel.state.value,
state = state,
viewModel::onSeeBlacklist,
viewModel::onEnableCard,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ fun GetSoraCardScreen(
),
color = MaterialTheme.customColors.statusError,
)

FilledButton(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package jp.co.soramitsu.feature_sora_card_impl.presentation

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import dagger.assisted.Assisted
Expand All @@ -55,10 +54,12 @@ import jp.co.soramitsu.oauth.base.sdk.contract.SoraCardContractData
import jp.co.soramitsu.oauth.base.sdk.contract.SoraCardResult
import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider
import jp.co.soramitsu.sora.substrate.substrate.ConnectionManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class GetSoraCardViewModel @AssistedInject constructor(
private val assetsRouter: AssetsRouter,
Expand All @@ -85,41 +86,40 @@ class GetSoraCardViewModel @AssistedInject constructor(
private val _launchSoraCardRegistration = SingleLiveEvent<SoraCardContractData>()
val launchSoraCardRegistration: LiveData<SoraCardContractData> = _launchSoraCardRegistration

var state = mutableStateOf(GetSoraCardState(applicationFee = ""))
private set
private val _state = MutableStateFlow(GetSoraCardState(applicationFee = "."))
val state = _state.asStateFlow()

private var applicationFeeCache: String? = null

init {
_toolbarState.value = initSmallTitle2(
title = R.string.get_sora_card_title,
)

viewModelScope.launch {
tryCatch {
state.value = state.value.copy(
applicationFee = soraCardInteractor.fetchApplicationFee()
)
}
}

soraCardInteractor.subscribeToSoraCardAvailabilityFlow()
.onEach {
.combine(connectionManager.connectionState) { f1, f2 ->
f1 to f2
}
.catch { onError(it) }
.onEach { (info, connection) ->
currentSoraCardContractData = createSoraCardContract(
userAvailableXorAmount = it.xorBalance.toDouble(),
isEnoughXorAvailable = it.enoughXor,
userAvailableXorAmount = info.xorBalance.toDouble(),
isEnoughXorAvailable = info.enoughXor,
)
state.value = state.value.copy(
xorRatioAvailable = it.xorRatioAvailable,
_state.value = _state.value.copy(
connection = connection,
applicationFee = fetchApplicationFee(),
xorRatioAvailable = info.xorRatioAvailable,
)
}.launchIn(viewModelScope)

connectionManager.connectionState
.catch { onError(it) }
.onEach {
state.value = state.value.copy(connection = it)
}
.launchIn(viewModelScope)
}

private suspend fun fetchApplicationFee(): String =
applicationFeeCache ?: soraCardInteractor.fetchApplicationFee().also {
applicationFeeCache = it
}

fun handleSoraCardResult(soraCardResult: SoraCardResult) {
when (soraCardResult) {
is SoraCardResult.NavigateTo -> {
Expand Down
Loading

0 comments on commit 643fa3e

Please sign in to comment.