Skip to content

Commit

Permalink
Include sargon kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
micbakos-rdx committed Feb 20, 2024
1 parent a93963f commit 2dd7d18
Show file tree
Hide file tree
Showing 55 changed files with 407 additions and 138 deletions.
115 changes: 0 additions & 115 deletions android/app/src/main/java/com/radixdlt/sargon/android/MainActivity.kt

This file was deleted.

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion android/app/build.gradle.kts → jvm/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ android {
}

dependencies {
implementation(project(":sargon"))
implementation(project(":sargon-android"))

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
Expand Down
File renamed without changes.
File renamed without changes.
222 changes: 222 additions & 0 deletions jvm/app/src/main/java/com/radixdlt/sargon/android/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package com.radixdlt.sargon.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.radixdlt.sargon.AppearanceId
import com.radixdlt.sargon.DisplayName
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.Profile
import com.radixdlt.sargon.ProfileNetwork
import com.radixdlt.sargon.SecureStorage
import com.radixdlt.sargon.Wallet
import com.radixdlt.sargon.WalletClientModel
import com.radixdlt.sargon.android.ui.theme.SargonAndroidTheme
import com.radixdlt.sargon.newAppearanceIdPlaceholder
import com.radixdlt.sargon.newAppearanceIdPlaceholderOther
import com.radixdlt.sargon.newDisplayName
import com.radixdlt.sargon.newProfilePlaceholder
import kotlin.random.Random

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val storage = EphemeralKeystore()

setContent {
SargonAndroidTheme {
WalletContent(storage = storage)
}
}
}
}

@Composable
fun WalletContent(
modifier: Modifier = Modifier,
storage: SecureStorage
) {
var walletState: Wallet? by remember { mutableStateOf(null) }
var profile: Profile? by remember { mutableStateOf(null) }

Scaffold(
modifier = modifier,
topBar = {
TopAppBar(title = { Text(text = "Wallet Test") })
},
bottomBar = {
if (walletState == null) {
Button(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
onClick = {
walletState = Wallet.with(
entropy = ByteArray(32) { 0xFF.toByte() },
secureStorage = storage
)
profile = walletState?.profile()
}
) {
Text(text = "Generate new Wallet")
}
} else if (profile?.networks?.isEmpty() == true) {
Column(modifier = Modifier.padding(16.dp)) {
var accountName by remember {
mutableStateOf("")
}
TextField(
modifier = Modifier.fillMaxWidth(),
value = accountName,
onValueChange = { accountName = it },
label = {
Text(text = "New Account Name")
},
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
walletState?.createAndSaveNewAccount(
networkId = NetworkId.MAINNET,
name = DisplayName(accountName)
)

profile = walletState?.profile()
})
)
}
}
}
) { padding ->
LazyColumn(
modifier = Modifier.padding(padding),
contentPadding = PaddingValues(16.dp)
) {
items(profile?.networks.orEmpty()) {
Network(
network = it,
onAccountAdd = { newName ->
walletState?.createNewAccount(NetworkId.MAINNET, DisplayName(newName))?.let {
walletState?.addAccount(it)

profile = walletState?.profile()
}
}
)
}
}
}
}

@Composable
fun Network(
modifier: Modifier = Modifier,
network: ProfileNetwork,
onAccountAdd: (String) -> Unit
) {
ElevatedCard(
modifier = modifier.fillMaxWidth()
) {
Spacer(modifier = Modifier.height(16.dp))
Text(
modifier = Modifier.padding(horizontal = 16.dp),
text = "${network.id}"
)

network.accounts.forEach { account ->
Text(
modifier = Modifier.padding(horizontal = 32.dp),
text = account.displayName.value,
style = MaterialTheme.typography.labelLarge
)
Text(
modifier = Modifier.padding(horizontal = 32.dp),
text = account.address.address,
style = MaterialTheme.typography.labelSmall
)
HorizontalDivider(modifier = Modifier.padding(horizontal = 32.dp))
}

Column(modifier = Modifier.padding(16.dp)) {
var newAccountName by remember { mutableStateOf("") }
TextField(
modifier = Modifier.fillMaxWidth(),
value = newAccountName,
onValueChange = { newAccountName = it },
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
onAccountAdd(newAccountName)
newAccountName = ""
})
)
}

}
}

val Profile.Companion.placeholder: Profile
get() = newProfilePlaceholder()

fun DisplayName.Companion.from(value: String): DisplayName {
return newDisplayName(name = value)
}

val AppearanceId.Companion.placeholder: AppearanceId
get() = newAppearanceIdPlaceholder()

val AppearanceId.Companion.placeholderOther: AppearanceId
get() = newAppearanceIdPlaceholderOther()

val Wallet.Companion.defaultPhoneName: String
get() = "Android Phone"

fun Wallet.Companion.with(
entropy: ByteArray = ByteArray(32).apply { Random.nextBytes(this) },
phoneName: String = Wallet.Companion.defaultPhoneName,
secureStorage: SecureStorage
): Wallet {
return Wallet.byCreatingNewProfileAndSecretsWithEntropy(
entropy = entropy,
walletClientModel = WalletClientModel.ANDROID,
walletClientName = phoneName,
secureStorage = secureStorage
)
}

@Preview(showBackground = true)
@Composable
fun NetworkPreview() {
val profile = Profile.placeholder
Network(network = profile.networks.first(), onAccountAdd = {})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2dd7d18

Please sign in to comment.