Skip to content

Commit

Permalink
Save Account Info
Browse files Browse the repository at this point in the history
  • Loading branch information
sajalbnl committed Dec 21, 2024
1 parent d7484aa commit 4f93692
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
4 changes: 4 additions & 0 deletions wallet_app/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Preferences> 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<Boolean> = 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<String> = context.dataStore.data.map { preferences ->
preferences[ACCOUNT_NAME] ?: ""
}

suspend fun setAccountName(accName: String) {
context.dataStore.edit { preferences ->
preferences[ACCOUNT_NAME] = accName
}
}
val privateKey: Flow<String> = context.dataStore.data.map { preferences ->
preferences[PRIVATE_KEY] ?: ""
}

suspend fun setPrivateKey(privateKey: String) {
context.dataStore.edit { preferences ->
preferences[PRIVATE_KEY] = privateKey
}
}
}
31 changes: 19 additions & 12 deletions wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Onboarding> {
OnboardingScreen(
Expand All @@ -78,7 +80,7 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
}
composable<FinalizeAccountCreation> {
FinalizeAccountCreationScreen(
onContinue = { navController.navigate( route = CreatePin )},
onContinue = { navController.navigate( route = CreatePin ) },
onBackButtonPressed = { navController.navigateUp() }
)
}
Expand All @@ -90,7 +92,11 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
}
composable<CreatePin> {
CreatePinScreen(
onContinue = { navController.navigate( route = Wallet )}
onContinue = { navController.navigate( route = Wallet )
CoroutineScope(Dispatchers.IO).launch {
dataStore.setHasAccount(true)
}
}
)
}

Expand Down Expand Up @@ -119,4 +125,5 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4f93692

Please sign in to comment.