From 375b75dd5dbc11be45d7f34587f4bc38e72bb884 Mon Sep 17 00:00:00 2001 From: Dev-Pasaka <=> Date: Wed, 8 Jan 2025 00:09:17 +0300 Subject: [PATCH] App: Successfuly intergrate transactions --- .../kotlin/org/example/di/useCaseModule.kt | 2 +- .../usecase/wallet/TransferCryptoUseCase.kt | 9 ++---- .../presentation/navigation/NavGraph.kt | 28 +++++++++++++------ .../presentation/navigation/Screens.kt | 17 +++++++---- .../screens/QrCodeScannerScreen.kt | 5 ++-- .../dashboardScreen/DashBoardScreen.kt | 13 +++++++-- .../DashboardScreenMiddleSection.kt | 4 ++- .../TransferCryptoBottomSheetContent.kt | 26 +++++++++++------ .../dashboardScreen/components/WalletItem.kt | 6 ++-- .../screens/transferScreen/TransferScreen.kt | 20 +++++-------- .../transferScreen/components/DialogBox.kt | 3 ++ .../{ => components}/ErrorDialog.kt | 7 ++--- .../components/SuccessDialog.kt | 4 +-- .../kotlin/org/example/project/App.kt | 2 +- 14 files changed, 88 insertions(+), 58 deletions(-) rename composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/{ => components}/ErrorDialog.kt (59%) diff --git a/composeApp/src/commonMain/kotlin/org/example/di/useCaseModule.kt b/composeApp/src/commonMain/kotlin/org/example/di/useCaseModule.kt index 958b2a9..5741d3c 100644 --- a/composeApp/src/commonMain/kotlin/org/example/di/useCaseModule.kt +++ b/composeApp/src/commonMain/kotlin/org/example/di/useCaseModule.kt @@ -17,6 +17,6 @@ val useCaseModule = module{ single{GetSessionStatusUseCase(get())} single{ CreateRequestLinkUseCase(get())} single{ GetRequestLinkDataUseCase(get())} - single{ TransferCryptoUseCase(get(), get())} + single{ TransferCryptoUseCase(get())} single{ GetTransactionUseCase(get(), get())} } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/org/example/domain/usecase/wallet/TransferCryptoUseCase.kt b/composeApp/src/commonMain/kotlin/org/example/domain/usecase/wallet/TransferCryptoUseCase.kt index 76bb40c..eeb3d39 100644 --- a/composeApp/src/commonMain/kotlin/org/example/domain/usecase/wallet/TransferCryptoUseCase.kt +++ b/composeApp/src/commonMain/kotlin/org/example/domain/usecase/wallet/TransferCryptoUseCase.kt @@ -12,28 +12,25 @@ import org.example.domain.repository.WalletRepository class TransferCryptoUseCase( private val repository: WalletRepository, - private val keyValueStorage: KeyValueStorage ) { operator fun invoke( body: TransferCryptoReq ): Flow> = flow { emit(Resource.Loading()) try { - val walletId = keyValueStorage.getString(Constants.WALLET_ID) ?: "" val data = body.copy( - walletId =walletId, idempotencyKey = generateUUID() ) val response = repository.transferCrypto(data) println("Transfer response: $response") - if (response.status){ + if (response.status) { emit(Resource.Success(data = response, message = "Success")) - }else{ + } else { emit(Resource.Error(response.message)) } - }catch (e: Exception){ + } catch (e: Exception) { e.printStackTrace() emit(Resource.Error(e.message.toString())) } diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/NavGraph.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/NavGraph.kt index d81cf8d..c348eec 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/NavGraph.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/NavGraph.kt @@ -38,22 +38,29 @@ fun NavGraph( onNavigateToExplorer = { url -> navController.navigate(Explorer(url)) }, - onNavigateToTransfer = { amount, address, blockchain, selectedTokenId -> + onNavigateToTransfer = { amount, address, blockchain, selectedTokenId, individualWalletId -> navController.navigate( TransactScreen( + individualWalletId = individualWalletId, amount = amount.toString(), address = address, blockchain = blockchain, tokenId = selectedTokenId ) - ){ - popUpTo(Screen.DashBoard){ + ) { + popUpTo(Screen.DashBoard) { inclusive = true } } }, - onOpenQRCodeScreen = {blockchain, tokenId -> - navController.navigate(QRCodeScanner(blockchain = blockchain, tokenId = tokenId)) + onOpenQRCodeScreen = { blockchain, tokenId, individualWalletId -> + navController.navigate( + QRCodeScanner( + individualWalletId = individualWalletId, + blockchain = blockchain, + tokenId = tokenId + ) + ) } ) } @@ -77,18 +84,20 @@ fun NavGraph( } ) } - composable {backStackEntry -> + composable { backStackEntry -> val data: QRCodeScanner = backStackEntry.toRoute() QrCodeScannerScreen( + individualWalletId = data.individualWalletId, tokenId = data.tokenId, blockchain = data.blockchain, onNavigateBack = { navController.navigateUp() }, - onNavigateToTransfer = { address, amount, blockchain, tokenId -> + onNavigateToTransfer = { address, amount, blockchain, tokenId, individualWalletId -> navController.navigate( TransactScreen( + individualWalletId = individualWalletId, address = address, amount = amount.toString(), blockchain = blockchain, @@ -102,12 +111,13 @@ fun NavGraph( val data: TransactScreen = backStackEntry.toRoute() TransferScreen( tokenId = data.tokenId, + individualWalletId = data.individualWalletId, blockchain = data.blockchain, address = data.address, amount = data.amount, onCancel = { - navController.navigate(Screen.DashBoard){ - popUpTo{ + navController.navigate(Screen.DashBoard) { + popUpTo { inclusive = true } } diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/Screens.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/Screens.kt index 85ab78f..ab4c636 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/Screens.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/navigation/Screens.kt @@ -6,30 +6,37 @@ import kotlinx.serialization.Serializable sealed class Screen() { @Serializable object Splash : Screen() + @Serializable object DashBoard : Screen() + @Serializable - object AuthScreen: Screen() + object AuthScreen : Screen() + @Serializable object OnBoarding : Screen() } - @Serializable -data class QRCodeScanner(val blockchain: String, val tokenId : String) : Screen() +data class QRCodeScanner( + val individualWalletId: String, + val blockchain: String, + val tokenId: String +) : Screen() @Serializable data class Explorer( val url: String -): Screen() +) : Screen() @Serializable data class TransactScreen( + val individualWalletId: String, val address: String, val blockchain: String, val tokenId: String, val amount: String -) :Screen() +) : Screen() diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/QrCodeScannerScreen.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/QrCodeScannerScreen.kt index b07a9b5..37fd874 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/QrCodeScannerScreen.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/QrCodeScannerScreen.kt @@ -24,10 +24,11 @@ import qrscanner.QrScanner @Composable fun QrCodeScannerScreen( + individualWalletId: String, tokenId: String, blockchain: String, onNavigateBack: () -> Unit, - onNavigateToTransfer: (String, Double, String, String) -> Unit + onNavigateToTransfer: (String, Double, String, String,String) -> Unit ) { var qrCodeData by remember { mutableStateOf("") } var flashlightOn by remember { mutableStateOf(false) } @@ -46,7 +47,7 @@ fun QrCodeScannerScreen( val address = obj?.get("address") val amount = obj?.get("amount")?.toDoubleOrNull() if (address != null && amount != null){ - onNavigateToTransfer(address, amount, blockchain, tokenId) + onNavigateToTransfer(address, amount, blockchain, tokenId, individualWalletId) } } } diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/DashBoardScreen.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/DashBoardScreen.kt index a8f25f4..5628217 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/DashBoardScreen.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/DashBoardScreen.kt @@ -51,8 +51,8 @@ import org.koin.compose.viewmodel.koinViewModel @Composable fun DashBoardScreen( onNavigateToExplorer: (String) -> Unit, - onOpenQRCodeScreen: (String, String) -> Unit, - onNavigateToTransfer: (Double, String, String, String) -> Unit + onOpenQRCodeScreen: (String, String, String) -> Unit, + onNavigateToTransfer: (Double, String, String, String, String) -> Unit ) { val viewModel: DashboardScreenViewModel = koinViewModel() @@ -69,6 +69,10 @@ fun DashBoardScreen( var walletId by remember { mutableStateOf("") } + var selectedIndividualWalletId by remember { + mutableStateOf("") + } + val scope = rememberCoroutineScope() var selectedBottomSheetContent by mutableStateOf("") @@ -103,12 +107,14 @@ fun DashBoardScreen( ) "Send" -> TransferCryptoBottomSheetContent( + selectedIndividualWalletId = selectedIndividualWalletId, selectedBlockchain = selectedBlockchain, selectedTokenId = selectedTokenId, onOpenQrScanner = { onOpenQRCodeScreen( selectedBlockchain, - selectedTokenId + selectedTokenId, + selectedIndividualWalletId ) }, onCancel = { @@ -150,6 +156,7 @@ fun DashBoardScreen( walletContent = content }, onSelectWalletId = { walletId = it }, + onSelectIndividualWalletId = { selectedIndividualWalletId = it }, onSend = { blockchain, tokenId -> scope.launch { bottomSheetScaffoldState.bottomSheetState.expand() diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/DashboardScreenMiddleSection.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/DashboardScreenMiddleSection.kt index c61ed03..c32e18b 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/DashboardScreenMiddleSection.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/DashboardScreenMiddleSection.kt @@ -35,6 +35,7 @@ fun DashboardScreenMiddleSection( onNavigateToExplorer: (String) -> Unit, onRequest: (Pair) -> Unit, onSelectWalletId: (String) -> Unit, + onSelectIndividualWalletId: (String) -> Unit, onSend: (String, String) -> Unit, isWalletLoading: Boolean, ) { @@ -82,7 +83,8 @@ fun DashboardScreenMiddleSection( onNavigateToExplorer = onNavigateToExplorer, onRequest = onRequest, onSend = onSend, - onSelectWalletId = onSelectWalletId + onSelectWalletId = onSelectWalletId, + onSelectIndividualWalletId = onSelectIndividualWalletId ) } diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/TransferCryptoBottomSheetContent.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/TransferCryptoBottomSheetContent.kt index 2838dc1..a9a8827 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/TransferCryptoBottomSheetContent.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/TransferCryptoBottomSheetContent.kt @@ -33,11 +33,12 @@ import cryptowallet.composeapp.generated.resources.qr_code @Composable fun TransferCryptoBottomSheetContent( + selectedIndividualWalletId: String, selectedBlockchain: String, selectedTokenId: String, onCancel: () -> Unit, - onOpenQrScanner : () -> Unit, - onNavigateToTransfer: (Double,String,String, String) -> Unit + onOpenQrScanner: () -> Unit, + onNavigateToTransfer: (Double, String, String, String, String) -> Unit ) { val clipboardManager = LocalClipboardManager.current var address by remember { @@ -94,12 +95,13 @@ fun TransferCryptoBottomSheetContent( ), trailingIcon = { - Row(horizontalArrangement = Arrangement.SpaceBetween, + Row( + horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically, - ){ - AnimatedVisibility(address.isEmpty()){ + ) { + AnimatedVisibility(address.isEmpty()) { IconButton( onClick = { val text = clipboardManager.getText() @@ -114,7 +116,7 @@ fun TransferCryptoBottomSheetContent( ) } } - AnimatedVisibility(address.isNotEmpty()){ + AnimatedVisibility(address.isNotEmpty()) { IconButton( onClick = { address = "" @@ -130,14 +132,14 @@ fun TransferCryptoBottomSheetContent( } Spacer(modifier = Modifier.width(8.dp)) - AnimatedVisibility(address.isEmpty()){ + AnimatedVisibility(address.isEmpty()) { IconButton( onClick = { onOpenQrScanner() } ) { Icon( - painter = painterResource(Res.drawable.qr_code), + painter = painterResource(Res.drawable.qr_code), contentDescription = "Clear", tint = MaterialTheme.colorScheme.onPrimaryContainer, modifier = Modifier.size(32.dp) @@ -153,7 +155,13 @@ fun TransferCryptoBottomSheetContent( CustomNumberKeyboard( isDoneEnable = address.isNotEmpty(), onDone = { - onNavigateToTransfer(it.toDoubleOrNull() ?: 0.0, address, selectedBlockchain, selectedTokenId) + onNavigateToTransfer( + it.toDoubleOrNull() ?: 0.0, + address, + selectedBlockchain, + selectedTokenId, + selectedIndividualWalletId + ) } ) } diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/WalletItem.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/WalletItem.kt index 5510758..9d56d81 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/WalletItem.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/dashboardScreen/components/WalletItem.kt @@ -54,6 +54,7 @@ fun WalletItem( onNavigateToExplorer: (String) -> Unit, onRequest: (Pair) -> Unit, onSelectWalletId: (String) -> Unit, + onSelectIndividualWalletId: (String) -> Unit, onSend: (String, String) -> Unit ) { val clipboardManager = LocalClipboardManager.current @@ -190,8 +191,9 @@ fun WalletItem( onSend( wallet.blockchain, wallet.walletBalance?.tokenId ?: "" - ) - }, + ) + onSelectIndividualWalletId(wallet.individualWalletId) + }, shape = CircleShape, color = Color.Transparent ) { diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/TransferScreen.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/TransferScreen.kt index 2353912..783d9b2 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/TransferScreen.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/TransferScreen.kt @@ -1,16 +1,11 @@ package org.example.presentation.screens.transferScreen -import androidx.compose.animation.core.animateFloatAsState -import androidx.compose.foundation.Canvas import androidx.compose.foundation.Image -import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.statusBarsPadding @@ -32,7 +27,6 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip -import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import cryptowallet.composeapp.generated.resources.Res @@ -43,17 +37,15 @@ import cryptowallet.composeapp.generated.resources.sol_icon import org.jetbrains.compose.resources.painterResource import androidx.compose.runtime.setValue import androidx.compose.runtime.getValue -import androidx.compose.ui.geometry.CornerRadius -import androidx.compose.ui.graphics.drawscope.clipRect import androidx.lifecycle.compose.collectAsStateWithLifecycle -import kotlinx.coroutines.delay import org.example.data.remote.dto.request.TransferCryptoReq -import org.example.presentation.screens.transferScreen.components.DialogBox +import org.example.presentation.screens.transferScreen.components.ErrorDialog import org.example.presentation.screens.transferScreen.components.SuccessDialog import org.koin.compose.viewmodel.koinViewModel @Composable fun TransferScreen( + individualWalletId: String, blockchain: String = "ETH-SEPOLIA", tokenId: String, address: String, @@ -76,6 +68,7 @@ fun TransferScreen( // Dialogs SuccessDialog( isVisible = showSuccessDialog, + message = dialogMessage, onClose = { showSuccessDialog = false onCancel() @@ -83,6 +76,7 @@ fun TransferScreen( ) ErrorDialog( isVisible = showErrorDialog, + message = dialogMessage, onClose = { showErrorDialog = false onCancel() @@ -96,12 +90,12 @@ fun TransferScreen( when (trasferDialogEvents.value.status) { "Success" -> { showSuccessDialog = true - dialogMessage = trasferDialogEvents.value.message + dialogMessage = viewModel.transferState.message } "Error" -> { showErrorDialog = true - dialogMessage = trasferDialogEvents.value.message + dialogMessage = viewModel.transferState.message } else -> {} @@ -218,7 +212,7 @@ fun TransferScreen( idempotencyKey = "", destinationAddress = address, amount = amount.toDouble(), - walletId = "", + walletId = individualWalletId, tokenId = tokenId ) ) diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/DialogBox.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/DialogBox.kt index 8da345a..96f8fda 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/DialogBox.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/DialogBox.kt @@ -4,6 +4,8 @@ import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape @@ -39,6 +41,7 @@ fun DialogBox( .padding(16.dp) ) { Column( + modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally ) { Text( diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/ErrorDialog.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/ErrorDialog.kt similarity index 59% rename from composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/ErrorDialog.kt rename to composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/ErrorDialog.kt index 58dfe3c..4d9bcae 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/ErrorDialog.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/ErrorDialog.kt @@ -1,15 +1,14 @@ -package org.example.presentation.screens.transferScreen +package org.example.presentation.screens.transferScreen.components import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color -import org.example.presentation.screens.transferScreen.components.DialogBox @Composable -fun ErrorDialog(isVisible: Boolean, onClose: () -> Unit) { +fun ErrorDialog(isVisible: Boolean, message: String, onClose: () -> Unit) { DialogBox( title = "Error", - message = "An error occurred. Please try again.", + message = message, isVisible = isVisible, onClose = onClose, backgroundColor = Color(0xFFF44336), // Red background diff --git a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/SuccessDialog.kt b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/SuccessDialog.kt index 6fb673a..219d61e 100644 --- a/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/SuccessDialog.kt +++ b/composeApp/src/commonMain/kotlin/org/example/presentation/screens/transferScreen/components/SuccessDialog.kt @@ -4,10 +4,10 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color @Composable -fun SuccessDialog(isVisible: Boolean, onClose: () -> Unit) { +fun SuccessDialog(isVisible: Boolean, message: String, onClose: () -> Unit) { DialogBox( title = "Success", - message = "Transaction completed successfully.", + message = message, isVisible = isVisible, onClose = onClose, backgroundColor = Color(0xFF4CAF50), // Green background diff --git a/composeApp/src/commonMain/kotlin/org/example/project/App.kt b/composeApp/src/commonMain/kotlin/org/example/project/App.kt index fc83f26..334b9aa 100644 --- a/composeApp/src/commonMain/kotlin/org/example/project/App.kt +++ b/composeApp/src/commonMain/kotlin/org/example/project/App.kt @@ -13,8 +13,8 @@ import org.example.presentation.theme.AppTheme @Composable @Preview fun App() { + val navController = rememberNavController() AppTheme { - val navController = rememberNavController() NavGraph( navController = navController )