-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jvsena42/feat/screen_receive
Feat/screen receive
- Loading branch information
Showing
9 changed files
with
154 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../main/java/com/github/jvsena42/floresta/presentation/ui/screens/receive/ReceiveUIState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
34 changes: 34 additions & 0 deletions
34
...ain/java/com/github/jvsena42/floresta/presentation/ui/screens/receive/ReceiveViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...c/main/java/com/github/jvsena42/floresta/presentation/ui/screens/receive/ScreenReceive.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |