From 4f9369237795ca303e216910cd8c072466c680c6 Mon Sep 17 00:00:00 2001 From: sajal Date: Sun, 22 Dec 2024 00:16:47 +0530 Subject: [PATCH] Save Account Info --- wallet_app/app/build.gradle.kts | 4 ++ .../walletapp/datastore/DataStoreModule.kt | 48 +++++++++++++++++++ .../com/example/walletapp/ui/WalletApp.kt | 31 +++++++----- .../FinalizeAccountCreationScreen.kt | 12 ++++- .../ui/onboarding/ImportAccountScreen.kt | 16 ++++++- 5 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 wallet_app/app/src/main/java/com/example/walletapp/datastore/DataStoreModule.kt diff --git a/wallet_app/app/build.gradle.kts b/wallet_app/app/build.gradle.kts index 28eef37..b1c09b6 100644 --- a/wallet_app/app/build.gradle.kts +++ b/wallet_app/app/build.gradle.kts @@ -74,6 +74,10 @@ dependencies { implementation("com.swmansion.starknet:starknet:0.13.0@aar"){ isTransitive = true } + + // DataStore + implementation("androidx.datastore:datastore-preferences:1.1.1") + // Navigation stuff implementation(libs.androidx.navigation.compose.v283) implementation(libs.androidx.navigation.compose.v283) diff --git a/wallet_app/app/src/main/java/com/example/walletapp/datastore/DataStoreModule.kt b/wallet_app/app/src/main/java/com/example/walletapp/datastore/DataStoreModule.kt new file mode 100644 index 0000000..8bc3b34 --- /dev/null +++ b/wallet_app/app/src/main/java/com/example/walletapp/datastore/DataStoreModule.kt @@ -0,0 +1,48 @@ +package com.example.walletapp.datastore + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +class DataStoreModule(private val context: Context) { + companion object { + private val Context.dataStore: DataStore by preferencesDataStore("wallet_prefs") + private val HAS_ACCOUNT = booleanPreferencesKey("has_account") + private val ACCOUNT_NAME = stringPreferencesKey("acc_name") + private val PRIVATE_KEY = stringPreferencesKey("private_key") + } + + val hasAccount: Flow = context.dataStore.data.map { preferences -> + preferences[HAS_ACCOUNT] ?: false + } + + suspend fun setHasAccount(hasAccount: Boolean) { + context.dataStore.edit { preferences -> + preferences[HAS_ACCOUNT] = hasAccount + } + } + val accName: Flow = context.dataStore.data.map { preferences -> + preferences[ACCOUNT_NAME] ?: "" + } + + suspend fun setAccountName(accName: String) { + context.dataStore.edit { preferences -> + preferences[ACCOUNT_NAME] = accName + } + } + val privateKey: Flow = context.dataStore.data.map { preferences -> + preferences[PRIVATE_KEY] ?: "" + } + + suspend fun setPrivateKey(privateKey: String) { + context.dataStore.edit { preferences -> + preferences[PRIVATE_KEY] = privateKey + } + } +} \ No newline at end of file diff --git a/wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt b/wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt index 51b4ed2..3350ed2 100644 --- a/wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt +++ b/wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt @@ -1,12 +1,15 @@ package com.example.walletapp.ui import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import com.example.walletapp.BuildConfig +import com.example.walletapp.datastore.DataStoreModule import com.example.walletapp.ui.account.AddTokenScreen import com.example.walletapp.ui.account.TokenViewModel import com.example.walletapp.ui.account.WalletScreen @@ -19,6 +22,9 @@ import com.example.walletapp.ui.onboarding.OnboardingScreen import com.example.walletapp.ui.theme.WalletappTheme import com.example.walletapp.ui.transfer.ReceiveScreen import com.example.walletapp.ui.transfer.SendScreen +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.serialization.Serializable // main user account view @@ -48,21 +54,17 @@ object Receive @Composable fun WalletApp(tokenViewModel: TokenViewModel) { val walletViewModel: WalletViewModel = viewModel() + val context = LocalContext.current + val dataStore = DataStoreModule(context) + val hasAccountState = dataStore.hasAccount.collectAsState(initial = null) + hasAccountState.value?.let { hasAccount -> WalletappTheme { - // TODO(#109): get this information from a data store - val hasAccount = false - fun getStart(): Any { - return if (hasAccount) { - Wallet - } else { - Onboarding - } - } + val startDestination = if (hasAccount) Wallet else Onboarding val navController = rememberNavController() - NavHost(navController, startDestination = getStart()) { + NavHost(navController, startDestination = startDestination) { composable { OnboardingScreen( @@ -78,7 +80,7 @@ fun WalletApp(tokenViewModel: TokenViewModel) { } composable { FinalizeAccountCreationScreen( - onContinue = { navController.navigate( route = CreatePin )}, + onContinue = { navController.navigate( route = CreatePin ) }, onBackButtonPressed = { navController.navigateUp() } ) } @@ -90,7 +92,11 @@ fun WalletApp(tokenViewModel: TokenViewModel) { } composable { CreatePinScreen( - onContinue = { navController.navigate( route = Wallet )} + onContinue = { navController.navigate( route = Wallet ) + CoroutineScope(Dispatchers.IO).launch { + dataStore.setHasAccount(true) + } + } ) } @@ -119,4 +125,5 @@ fun WalletApp(tokenViewModel: TokenViewModel) { } } } + } } \ No newline at end of file diff --git a/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/FinalizeAccountCreationScreen.kt b/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/FinalizeAccountCreationScreen.kt index 91d43ba..f2caf3f 100644 --- a/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/FinalizeAccountCreationScreen.kt +++ b/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/FinalizeAccountCreationScreen.kt @@ -32,6 +32,10 @@ import android.widget.Toast import androidx.compose.ui.graphics.painter.Painter import androidx.core.graphics.toColorInt import com.example.walletapp.R +import com.example.walletapp.datastore.DataStoreModule +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -90,7 +94,7 @@ fun FinalizeAccountCreationScreen( fun AccountInfoView(onContinue: () -> Unit) { val context = LocalContext.current as Activity val clipboardManager = LocalContext.current.getSystemService(ClipboardManager::class.java) - + val dataStore = DataStoreModule(context) var checked by remember { mutableStateOf(true) } val accountName = "Starknet" val privateKey = "q78ggh277ibckewjtnM" @@ -276,7 +280,11 @@ fun AccountInfoView(onContinue: () -> Unit) { Spacer(modifier = Modifier.weight(1f)) Button( - onClick = onContinue, + onClick = {onContinue() + CoroutineScope(Dispatchers.IO).launch { + dataStore.setAccountName(accountName) + dataStore.setPrivateKey(privateKey) + }}, contentPadding = ButtonDefaults.ContentPadding, shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.buttonColors( diff --git a/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/ImportAccountScreen.kt b/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/ImportAccountScreen.kt index a996ad7..ea9b69e 100644 --- a/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/ImportAccountScreen.kt +++ b/wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/ImportAccountScreen.kt @@ -52,7 +52,10 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.graphics.toColorInt import com.example.walletapp.R +import com.example.walletapp.datastore.DataStoreModule import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -145,6 +148,8 @@ fun PrivateKeyView(modifier: Modifier = Modifier, onNext: () -> Unit) { val scope = rememberCoroutineScope() var openBottomSheet by remember { mutableStateOf(false) } val sheetState = rememberModalBottomSheetState() + val context = (LocalContext.current as Activity) + val dataStore = DataStoreModule(context) Column( modifier = modifier @@ -197,7 +202,10 @@ fun PrivateKeyView(modifier: Modifier = Modifier, onNext: () -> Unit) { Spacer(modifier = Modifier.weight(1f)) Button( - onClick = onNext, + onClick = {onNext() + CoroutineScope(Dispatchers.IO).launch { + dataStore.setPrivateKey(accountName) + }}, contentPadding = ButtonDefaults.ContentPadding, shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.buttonColors( @@ -231,6 +239,7 @@ fun CreateNameView(modifier: Modifier = Modifier, onFinishAccountImport: () -> U val borderColor = Color("#1B1B76".toColorInt()) var accountName by remember { mutableStateOf("") } val context = (LocalContext.current as Activity) + val dataStore = DataStoreModule(context) Column( modifier = modifier @@ -283,7 +292,10 @@ fun CreateNameView(modifier: Modifier = Modifier, onFinishAccountImport: () -> U Spacer(modifier = Modifier.weight(1f)) Button( - onClick = onFinishAccountImport, + onClick = {onFinishAccountImport() + CoroutineScope(Dispatchers.IO).launch { + dataStore.setAccountName(accountName) + }}, contentPadding = ButtonDefaults.ContentPadding, shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.buttonColors(