Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(slideconfirm): make loading state visibility tied directly to l… #504

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions api/src/main/java/com/getcode/network/HistoryController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.getcode.network.repository.encodeBase64
import com.getcode.network.source.ChatMessagePagingSource
import com.getcode.util.resources.ResourceHelper
import com.getcode.util.resources.ResourceType
import com.getcode.utils.ErrorUtils
import com.getcode.utils.TraceType
import com.getcode.utils.trace
import kotlinx.coroutines.CoroutineScope
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.getcode.network.repository

import com.getcode.model.BalanceCurrencyFeature
import com.getcode.model.BetaFlag
import com.getcode.model.BuyModuleFeature
import com.getcode.model.Feature
import com.getcode.model.PrefsBool
import com.getcode.model.RequestKinFeature
import com.getcode.model.TipCardFeature
Expand All @@ -15,7 +17,7 @@ import javax.inject.Inject
* Collates [BetaOptions] with server availability (stored in [PrefRepository]).
*/
class FeatureRepository @Inject constructor(
betaFlags: BetaFlagsRepository,
private val betaFlags: BetaFlagsRepository,
prefRepository: PrefRepository,
) {
val buyModule = combine(
Expand All @@ -31,4 +33,6 @@ class FeatureRepository @Inject constructor(
val requestKin = betaFlags.observe().map { RequestKinFeature(it.giveRequestsEnabled) }

val balanceCurrencySelection = betaFlags.observe().map { BalanceCurrencyFeature(it.balanceCurrencySelectionEnabled) }

suspend fun isEnabled(feature: PrefsBool): Boolean = betaFlags.isEnabled(feature)
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class MessagingRepository @Inject constructor(
.build()

return networkOracle.managedRequest(messagingApi.pollMessages(request))
.observeOn(Schedulers.io())
.map { response ->
Timber.d("response=${response.messagesList}")
response.messagesList.mapNotNull { m -> StreamMessage.getInstance(m) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import com.getcode.network.exchange.Exchange
import com.getcode.solana.keys.PublicKey
import com.getcode.utils.ErrorUtils
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
import kotlin.coroutines.resume
Expand Down Expand Up @@ -90,7 +92,9 @@ class PaymentRepository @Inject constructor(
@SuppressLint("CheckResult")
suspend fun completePayment(amount: KinAmount, rendezvousKey: KeyPair) {
// 1. ensure we have exchange rates and compute the fees for this transaction
exchange.fetchRatesIfNeeded()
withContext(Dispatchers.IO) {
exchange.fetchRatesIfNeeded()
}

var paymentAmount = amount
return suspendCancellableCoroutine { cont ->
Expand Down Expand Up @@ -156,7 +160,7 @@ class PaymentRepository @Inject constructor(
Completable.concatArray(
balanceController.fetchBalance(),
client.fetchLimits(isForce = true)
).doOnComplete {
).observeOn(Schedulers.io()).doOnComplete {
analytics.transfer(
amount = paymentAmount,
successful = true,
Expand Down Expand Up @@ -209,7 +213,7 @@ class PaymentRepository @Inject constructor(
Completable.concatArray(
balanceController.fetchBalance(),
client.fetchLimits(isForce = true)
).doOnComplete {
).observeOn(Schedulers.io()).doOnComplete {
analytics.transferForTip(amount = amount, successful = true)
cont.resume(Unit)
}.subscribe()
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/getcode/ui/components/SlideToConfirm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.getcode.ui.components

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
Expand Down Expand Up @@ -178,7 +179,7 @@ fun SlideToConfirm(

val hapticFeedback = LocalHapticFeedback.current
val swipeState = rememberSwipeableState(
initialValue = if (loading) Anchor.End else Anchor.Start,
initialValue = Anchor.Start,
)

val composeScope = rememberCoroutineScope()
Expand Down Expand Up @@ -217,7 +218,6 @@ fun SlideToConfirm(
hint(swipeFraction, PaddingValues(horizontal = Thumb.Size + CodeTheme.dimens.grid.x2), label)
}


if (isSuccess) {
Image(
painter = painterResource(id = R.drawable.ic_check),
Expand All @@ -227,9 +227,13 @@ fun SlideToConfirm(
.align(Alignment.Center),
)
} else {
val loadingColor by animateColorAsState(
targetValue = if (loading) Color.White else Color.Transparent
)

CodeCircularProgressIndicator(
strokeWidth = CodeTheme.dimens.thickBorder,
color = calculateLoadingColor(swipeFraction),
color = loadingColor,
modifier = Modifier
.size(CodeTheme.dimens.grid.x4)
.align(Alignment.Center),
Expand Down Expand Up @@ -362,10 +366,6 @@ private fun calculateHintTextColor(swipeFraction: Float): Color {
return lerp(Color.White, Color.White.copy(alpha = 0f), fraction)
}

private fun calculateLoadingColor(swipeFraction: Float): Color {
if (swipeFraction < 0.1f) return Color.White.copy(0f)
return Color.White
}

@Preview
@Composable
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/getcode/util/DeeplinkHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ import android.content.Intent
import android.net.Uri
import androidx.core.net.toUri
import cafe.adriel.voyager.core.screen.Screen
import com.getcode.model.BetaFlag
import com.getcode.model.PrefsBool
import com.getcode.models.DeepLinkRequest
import com.getcode.models.encode
import com.getcode.navigation.screens.HomeScreen
import com.getcode.navigation.screens.LoginScreen
import com.getcode.network.repository.BetaFlagsRepository
import com.getcode.network.repository.encodeBase64
import com.getcode.network.repository.urlDecode
import com.getcode.ui.utils.getActivity
import com.getcode.utils.TraceType
import com.getcode.utils.base64EncodedData
import com.getcode.utils.trace
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.getcode.model.PrefsBool
import com.getcode.model.Rate
import com.getcode.network.BalanceController
import com.getcode.network.HistoryController
import com.getcode.network.repository.BetaFlagsRepository
import com.getcode.network.repository.FeatureRepository
import com.getcode.network.repository.PrefRepository
import com.getcode.util.Kin
Expand All @@ -18,6 +19,7 @@ import com.getcode.view.BaseViewModel2
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -124,6 +126,7 @@ class BalanceSheetViewModel @Inject constructor(

eventFlow
.filterIsInstance<Event.OnOpened>()
.filter { features.isEnabled(PrefsBool.TIPS_CHAT_ENABLED) }
.onEach { historyController.fetchChats(true) }
.launchIn(viewModelScope)
}
Expand Down
57 changes: 26 additions & 31 deletions app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -883,30 +883,28 @@ class HomeViewModel @Inject constructor(

val isReceived = payload != null
val presentationStyle = if (isReceived) PresentationStyle.Pop else PresentationStyle.Slide
withContext(Dispatchers.Main) {
uiFlow.update {
var billState = it.billState.copy(
bill = Bill.Payment(amount, code, request),
valuation = PaymentValuation(amount),
primaryAction = null,
)

if (isReceived) {
billState = billState.copy(
paymentConfirmation = PaymentConfirmation(
state = ConfirmationState.AwaitingConfirmation,
payload = code,
requestedAmount = amount,
localAmount = amount.replacing(exchange.localRate)
),
)
}
uiFlow.update {
var billState = it.billState.copy(
bill = Bill.Payment(amount, code, request),
valuation = PaymentValuation(amount),
primaryAction = null,
)

it.copy(
presentationStyle = presentationStyle,
billState = billState,
if (isReceived) {
billState = billState.copy(
paymentConfirmation = PaymentConfirmation(
state = ConfirmationState.AwaitingConfirmation,
payload = code,
requestedAmount = amount,
localAmount = amount.replacing(exchange.localRate)
),
)
}

it.copy(
presentationStyle = presentationStyle,
billState = billState,
)
}

analytics.requestShown(amount = amount)
Expand All @@ -929,17 +927,14 @@ class HomeViewModel @Inject constructor(
cashLinkManager.cancelBillTimeout()

val paymentConfirmation = uiFlow.value.billState.paymentConfirmation ?: return@launch
withContext(Dispatchers.Main) {
uiFlow.update {
val billState = it.billState
it.copy(
billState = billState.copy(
paymentConfirmation = paymentConfirmation.copy(state = ConfirmationState.Sending)
),
)
}
uiFlow.update {
val billState = it.billState
it.copy(
billState = billState.copy(
paymentConfirmation = paymentConfirmation.copy(state = ConfirmationState.Sending)
),
)
}

runCatching {
paymentRepository.completePayment(
paymentConfirmation.requestedAmount,
Expand Down
Loading