Skip to content

Commit

Permalink
Merge pull request #7 from jvsena42/feat/screen_receive
Browse files Browse the repository at this point in the history
Feat/screen receive
  • Loading branch information
jvsena42 authored Oct 25, 2024
2 parents 887e867 + 88d7414 commit 41d19ae
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation("net.java.dev.jna:jna:5.14.0@aar")
implementation("com.google.zxing:core:3.4.1")
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.github.jvsena42.floresta.domain.floresta.FlorestaDaemonImpl
import com.github.jvsena42.floresta.domain.floresta.FlorestaService
import com.github.jvsena42.floresta.presentation.ui.screens.main.MainViewmodel
import com.github.jvsena42.floresta.presentation.ui.screens.home.HomeViewModel
import com.github.jvsena42.floresta.presentation.ui.screens.receive.ReceiveViewModel
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
Expand Down Expand Up @@ -46,6 +47,7 @@ val presentationModule = module {
florestaDaemon = get()
)
}
viewModel { ReceiveViewModel(walletManager = get()) }
}

val domainModule = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WalletManager(
private lateinit var florestaDbPath: String
private lateinit var wallet: Wallet
private val blockchainClient: ElectrumClient by lazy { ElectrumClient(ELECTRUM_ADDRESS) }
private var fullScanRequired: Boolean = false
private var fullScanRequired: Boolean = true

init {
setPathAndConnectDb(dbPath)
Expand Down Expand Up @@ -244,7 +244,7 @@ class WalletManager(
}

companion object {
private const val TAG = "WalletObject"
private const val TAG = "WalletManager"
const val ELECTRUM_ADDRESS = "127.0.0.1:50001"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class HomeViewModel(

init {
syncInLoop()
if (walletRepository.doesWalletExist()) {
Log.d(TAG, "mnemonic: ${walletRepository.getMnemonic().getOrNull()}")
}
}

private suspend fun updateUI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ enum class Destinations(
val label: String,
@DrawableRes val icon: Int
) {
HOME(route = "Home", label = "Home", R.drawable.ic_home)
HOME(route = "Home", label = "Home", R.drawable.ic_home),
RECEIVE(route = "Receive", label = "Receive", R.drawable.ic_arrow_down)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package com.github.jvsena42.floresta.presentation.ui.screens.main
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
Expand All @@ -24,6 +21,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.github.jvsena42.floresta.presentation.ui.screens.home.ScreenHome
import com.github.jvsena42.floresta.presentation.ui.screens.receive.ScreenReceive
import com.github.jvsena42.floresta.presentation.ui.theme.FlorestaTheme
import com.github.jvsena42.floresta.presentation.ui.theme.Primary
import org.koin.androidx.compose.KoinAndroidContext
Expand All @@ -40,7 +38,7 @@ class MainActivity : ComponentActivity() {
Scaffold(modifier = Modifier.fillMaxSize(),
bottomBar = {
NavigationBar(
containerColor = Primary
// containerColor = Primary
) {
Destinations.entries.forEach { destination ->
NavigationBarItem(
Expand Down Expand Up @@ -77,6 +75,9 @@ class MainActivity : ComponentActivity() {
composable(Destinations.HOME.route) {
ScreenHome()
}
composable(Destinations.RECEIVE.route) {
ScreenReceive()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.jvsena42.floresta.presentation.ui.screens.receive

import androidx.compose.runtime.Stable

@Stable
data class ReceiveUIState(
val address: String = "",
val bip21Uri: String? = null,
val isLoading: Boolean = true
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.jvsena42.floresta.presentation.ui.screens.receive

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jvsena42.floresta.domain.bitcoin.WalletManager
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class ReceiveViewModel(
private val walletManager: WalletManager
): ViewModel() {

private val _uiState = MutableStateFlow(ReceiveUIState())
val uiState = _uiState.asStateFlow()

init {
updateAddress()
}

private fun updateAddress() = viewModelScope.launch {
val address = walletManager.getLastUnusedAddress()
delay(500)
Log.d(TAG, "updateAddress: Address: ${address.address} uri: ${address.address.toQrUri()}")
_uiState.update { it.copy(address = address.address.toString(), bip21Uri = address.address.toQrUri(), isLoading = false) }
}

private companion object {
private const val TAG = "ReceiveViewModel"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.jvsena42.floresta.presentation.ui.screens.receive

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import org.koin.androidx.compose.koinViewModel
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.github.jvsena42.floresta.presentation.ui.theme.FlorestaTheme
import kotlinx.coroutines.launch

@Composable
fun ScreenReceive(
viewModel: ReceiveViewModel = koinViewModel()
) {
val uiState: ReceiveUIState by viewModel.uiState.collectAsState()
ScreenReceive(uiState)
}

@Composable
private fun ScreenReceive(uiState: ReceiveUIState) {
val clipboardManager: ClipboardManager = LocalClipboardManager.current
val scope = rememberCoroutineScope()
val snackBarHostState = remember { SnackbarHostState() }

Scaffold(
snackbarHost = {
SnackbarHost(hostState = snackBarHostState)
},
) { padding ->
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
.padding(padding),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
AnimatedVisibility(visible = uiState.isLoading) {
CircularProgressIndicator(
modifier = Modifier
.size(200.dp)
.padding(32.dp)
)
}

Text(
uiState.address,
style = MaterialTheme.typography.titleMedium,
modifier = Modifier
.padding(horizontal = 32.dp)
.clickable {
clipboardManager.setText(AnnotatedString(uiState.address))
scope.launch {
snackBarHostState
.showSnackbar(
message = "Text copied to clipboard",
duration = SnackbarDuration.Short
)
}
}
)
}
}
}

@Preview
@Composable
private fun Preview() {
FlorestaTheme {
ScreenReceive(uiState = ReceiveUIState(address = "bc1qfdsafkpowfenfsdlknv"))
}
}

0 comments on commit 41d19ae

Please sign in to comment.