Skip to content

Commit 815e676

Browse files
sajalbnltrbutler4
andauthored
Save Account Info (#138)
* copy address and back button fixes * Some minor changes * Remove local.properties from repository * Save Account Info * rename datastore * remove unused vars --------- Co-authored-by: Thomas <[email protected]>
1 parent 2bdc2f2 commit 815e676

File tree

6 files changed

+95
-31
lines changed

6 files changed

+95
-31
lines changed

wallet_app/app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ dependencies {
7474
implementation("com.swmansion.starknet:starknet:0.13.0@aar"){
7575
isTransitive = true
7676
}
77+
78+
// DataStore
79+
implementation("androidx.datastore:datastore-preferences:1.1.1")
80+
7781
// Navigation stuff
7882
implementation(libs.androidx.navigation.compose.v283)
7983
implementation(libs.androidx.navigation.compose.v283)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example.walletapp.datastore
2+
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.preferences.core.Preferences
6+
import androidx.datastore.preferences.core.booleanPreferencesKey
7+
import androidx.datastore.preferences.core.edit
8+
import androidx.datastore.preferences.core.stringPreferencesKey
9+
import androidx.datastore.preferences.preferencesDataStore
10+
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.map
12+
13+
class WalletStoreModule(private val context: Context) {
14+
companion object {
15+
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore("wallet_prefs")
16+
private val HAS_ACCOUNT = booleanPreferencesKey("has_account")
17+
private val ACCOUNT_NAME = stringPreferencesKey("acc_name")
18+
private val PRIVATE_KEY = stringPreferencesKey("private_key")
19+
}
20+
21+
val hasAccount: Flow<Boolean> = context.dataStore.data.map { preferences ->
22+
preferences[HAS_ACCOUNT] ?: false
23+
}
24+
25+
suspend fun setHasAccount(hasAccount: Boolean) {
26+
context.dataStore.edit { preferences ->
27+
preferences[HAS_ACCOUNT] = hasAccount
28+
}
29+
}
30+
val accName: Flow<String> = context.dataStore.data.map { preferences ->
31+
preferences[ACCOUNT_NAME] ?: ""
32+
}
33+
34+
suspend fun setAccountName(accName: String) {
35+
context.dataStore.edit { preferences ->
36+
preferences[ACCOUNT_NAME] = accName
37+
}
38+
}
39+
val privateKey: Flow<String> = context.dataStore.data.map { preferences ->
40+
preferences[PRIVATE_KEY] ?: ""
41+
}
42+
43+
suspend fun setPrivateKey(privateKey: String) {
44+
context.dataStore.edit { preferences ->
45+
preferences[PRIVATE_KEY] = privateKey
46+
}
47+
}
48+
}

wallet_app/app/src/main/java/com/example/walletapp/ui/WalletApp.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.example.walletapp.ui
22

33
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.collectAsState
45
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.platform.LocalContext
57
import androidx.lifecycle.viewmodel.compose.viewModel
68
import androidx.navigation.compose.NavHost
79
import androidx.navigation.compose.composable
810
import androidx.navigation.compose.rememberNavController
9-
import com.example.walletapp.BuildConfig
11+
import com.example.walletapp.datastore.WalletStoreModule
1012
import com.example.walletapp.ui.account.AddTokenScreen
1113
import com.example.walletapp.ui.account.TokenViewModel
1214
import com.example.walletapp.ui.account.WalletScreen
@@ -19,6 +21,9 @@ import com.example.walletapp.ui.onboarding.OnboardingScreen
1921
import com.example.walletapp.ui.theme.WalletappTheme
2022
import com.example.walletapp.ui.transfer.ReceiveScreen
2123
import com.example.walletapp.ui.transfer.SendScreen
24+
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.Dispatchers
26+
import kotlinx.coroutines.launch
2227
import kotlinx.serialization.Serializable
2328

2429
// main user account view
@@ -48,21 +53,17 @@ object Receive
4853
@Composable
4954
fun WalletApp(tokenViewModel: TokenViewModel) {
5055
val walletViewModel: WalletViewModel = viewModel()
56+
val context = LocalContext.current
57+
val dataStore = WalletStoreModule(context)
58+
val hasAccountState = dataStore.hasAccount.collectAsState(initial = null)
59+
hasAccountState.value?.let { hasAccount ->
5160
WalletappTheme {
5261

53-
// TODO(#109): get this information from a data store
54-
val hasAccount = false
5562

56-
fun getStart(): Any {
57-
return if (hasAccount) {
58-
Wallet
59-
} else {
60-
Onboarding
61-
}
62-
}
63+
val startDestination = if (hasAccount) Wallet else Onboarding
6364

6465
val navController = rememberNavController()
65-
NavHost(navController, startDestination = getStart()) {
66+
NavHost(navController, startDestination = startDestination) {
6667

6768
composable<Onboarding> {
6869
OnboardingScreen(
@@ -78,7 +79,7 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
7879
}
7980
composable<FinalizeAccountCreation> {
8081
FinalizeAccountCreationScreen(
81-
onContinue = { navController.navigate( route = CreatePin )},
82+
onContinue = { navController.navigate( route = CreatePin ) },
8283
onBackButtonPressed = { navController.navigateUp() }
8384
)
8485
}
@@ -90,7 +91,11 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
9091
}
9192
composable<CreatePin> {
9293
CreatePinScreen(
93-
onContinue = { navController.navigate( route = Wallet )}
94+
onContinue = { navController.navigate( route = Wallet )
95+
CoroutineScope(Dispatchers.IO).launch {
96+
dataStore.setHasAccount(true)
97+
}
98+
}
9499
)
95100
}
96101

@@ -119,4 +124,5 @@ fun WalletApp(tokenViewModel: TokenViewModel) {
119124
}
120125
}
121126
}
127+
}
122128
}

wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/FinalizeAccountCreationScreen.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ import androidx.compose.ui.unit.sp
2929
import android.content.ClipboardManager
3030
import android.content.ClipData
3131
import android.widget.Toast
32-
import androidx.compose.ui.graphics.painter.Painter
3332
import androidx.core.graphics.toColorInt
3433
import com.example.walletapp.R
34+
import com.example.walletapp.datastore.WalletStoreModule
35+
import kotlinx.coroutines.CoroutineScope
36+
import kotlinx.coroutines.Dispatchers
37+
import kotlinx.coroutines.launch
3538

3639
@OptIn(ExperimentalMaterial3Api::class)
3740
@Composable
@@ -90,7 +93,7 @@ fun FinalizeAccountCreationScreen(
9093
fun AccountInfoView(onContinue: () -> Unit) {
9194
val context = LocalContext.current as Activity
9295
val clipboardManager = LocalContext.current.getSystemService(ClipboardManager::class.java)
93-
96+
val dataStore = WalletStoreModule(context)
9497
var checked by remember { mutableStateOf(true) }
9598
val accountName = "Starknet"
9699
val privateKey = "q78ggh277ibckewjtnM"
@@ -276,7 +279,11 @@ fun AccountInfoView(onContinue: () -> Unit) {
276279
Spacer(modifier = Modifier.weight(1f))
277280

278281
Button(
279-
onClick = onContinue,
282+
onClick = {onContinue()
283+
CoroutineScope(Dispatchers.IO).launch {
284+
dataStore.setAccountName(accountName)
285+
dataStore.setPrivateKey(privateKey)
286+
}},
280287
contentPadding = ButtonDefaults.ContentPadding,
281288
shape = RoundedCornerShape(8.dp),
282289
colors = ButtonDefaults.buttonColors(

wallet_app/app/src/main/java/com/example/walletapp/ui/onboarding/ImportAccountScreen.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.example.walletapp.ui.onboarding
22

33
import android.app.Activity
4-
import android.content.Intent
5-
import androidx.compose.foundation.Image
64
import androidx.compose.foundation.background
7-
import androidx.compose.foundation.layout.Arrangement
85
import androidx.compose.foundation.layout.Box
96
import androidx.compose.foundation.layout.Column
107
import androidx.compose.foundation.layout.Row
@@ -13,7 +10,6 @@ import androidx.compose.foundation.layout.fillMaxSize
1310
import androidx.compose.foundation.layout.fillMaxWidth
1411
import androidx.compose.foundation.layout.height
1512
import androidx.compose.foundation.layout.padding
16-
import androidx.compose.foundation.layout.size
1713
import androidx.compose.foundation.layout.width
1814
import androidx.compose.foundation.shape.RoundedCornerShape
1915
import androidx.compose.material3.Button
@@ -24,16 +20,12 @@ import androidx.compose.material3.OutlinedTextField
2420
import androidx.compose.material3.Scaffold
2521
import androidx.compose.material3.Text
2622
import androidx.compose.material3.TextFieldDefaults
27-
import androidx.compose.material3.TopAppBar
2823
import androidx.compose.material3.ExperimentalMaterial3Api
2924
import androidx.compose.material3.IconButton
30-
import androidx.compose.material3.ModalBottomSheet
31-
import androidx.compose.material3.SheetState
3225
import androidx.compose.material3.rememberModalBottomSheetState
3326
import androidx.compose.material.icons.Icons
3427
import androidx.compose.material.icons.filled.ArrowBack
3528
import androidx.compose.material.icons.filled.ArrowForward
36-
import androidx.compose.material3.TopAppBarDefaults
3729
import androidx.compose.runtime.Composable
3830
import androidx.compose.runtime.getValue
3931
import androidx.compose.runtime.mutableStateOf
@@ -44,15 +36,16 @@ import androidx.compose.ui.Alignment
4436
import androidx.compose.ui.Modifier
4537
import androidx.compose.ui.graphics.Color
4638
import androidx.compose.ui.platform.LocalContext
47-
import androidx.compose.ui.res.painterResource
4839
import androidx.compose.ui.text.TextStyle
4940
import androidx.compose.ui.text.font.FontWeight
5041
import androidx.compose.ui.text.style.TextAlign
5142
import androidx.compose.ui.unit.dp
5243
import androidx.compose.ui.unit.sp
5344
import androidx.core.graphics.toColorInt
54-
import com.example.walletapp.R
45+
import com.example.walletapp.datastore.WalletStoreModule
5546
import kotlinx.coroutines.CoroutineScope
47+
import kotlinx.coroutines.Dispatchers
48+
import kotlinx.coroutines.launch
5649

5750
@OptIn(ExperimentalMaterial3Api::class)
5851
@Composable
@@ -145,6 +138,8 @@ fun PrivateKeyView(modifier: Modifier = Modifier, onNext: () -> Unit) {
145138
val scope = rememberCoroutineScope()
146139
var openBottomSheet by remember { mutableStateOf(false) }
147140
val sheetState = rememberModalBottomSheetState()
141+
val context = (LocalContext.current as Activity)
142+
val dataStore = WalletStoreModule(context)
148143

149144
Column(
150145
modifier = modifier
@@ -197,7 +192,10 @@ fun PrivateKeyView(modifier: Modifier = Modifier, onNext: () -> Unit) {
197192
Spacer(modifier = Modifier.weight(1f))
198193

199194
Button(
200-
onClick = onNext,
195+
onClick = {onNext()
196+
CoroutineScope(Dispatchers.IO).launch {
197+
dataStore.setPrivateKey(accountName)
198+
}},
201199
contentPadding = ButtonDefaults.ContentPadding,
202200
shape = RoundedCornerShape(8.dp),
203201
colors = ButtonDefaults.buttonColors(
@@ -231,6 +229,7 @@ fun CreateNameView(modifier: Modifier = Modifier, onFinishAccountImport: () -> U
231229
val borderColor = Color("#1B1B76".toColorInt())
232230
var accountName by remember { mutableStateOf("") }
233231
val context = (LocalContext.current as Activity)
232+
val dataStore = WalletStoreModule(context)
234233

235234
Column(
236235
modifier = modifier
@@ -283,7 +282,10 @@ fun CreateNameView(modifier: Modifier = Modifier, onFinishAccountImport: () -> U
283282
Spacer(modifier = Modifier.weight(1f))
284283

285284
Button(
286-
onClick = onFinishAccountImport,
285+
onClick = {onFinishAccountImport()
286+
CoroutineScope(Dispatchers.IO).launch {
287+
dataStore.setAccountName(accountName)
288+
}},
287289
contentPadding = ButtonDefaults.ContentPadding,
288290
shape = RoundedCornerShape(8.dp),
289291
colors = ButtonDefaults.buttonColors(

wallet_app/app/src/main/java/com/example/walletapp/utils/StarknetClient.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ const val ACCOUNT_CLASS_HASH = "0x04c6d6cf894f8bc96bb9c525e6853e5483177841f7388f
3232
class StarknetClient(private val rpcUrl: String) {
3333

3434
private val provider = JsonRpcProvider(rpcUrl)
35-
private val privateKey = BuildConfig.PRIVATE_KEY
36-
private val accountAddress = BuildConfig.ACCOUNT_ADDRESS
37-
private val tag = "StarknetClient"
3835
private val keystore = Keystore()
3936

4037
suspend fun deployAccount() {

0 commit comments

Comments
 (0)