Update CreateMaskedEmailScreen.kt
This commit is contained in:
parent
4a081300cb
commit
5b6b7438df
1 changed files with 50 additions and 12 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package com.fastmask.ui.create
|
||||
|
||||
import android.widget.Toast
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
|
|
@ -22,6 +24,10 @@ import androidx.compose.material3.MaterialTheme
|
|||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.RadioButton
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarDuration
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.SnackbarResult
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
|
|
@ -29,13 +35,20 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.runtime.LaunchedEffect
|
||||
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.Modifier
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.fastmask.domain.model.EmailState
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
|
|
@ -45,16 +58,24 @@ fun CreateMaskedEmailScreen(
|
|||
) {
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
val context = LocalContext.current
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
val scope = rememberCoroutineScope()
|
||||
val haptic = LocalHapticFeedback.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.events.collectLatest { event ->
|
||||
when (event) {
|
||||
is CreateMaskedEmailEvent.Created -> {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"Created: ${event.email}",
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
val result = snackbarHostState.showSnackbar(
|
||||
message = "Created: ${event.email}",
|
||||
actionLabel = "Copy",
|
||||
duration = SnackbarDuration.Long
|
||||
)
|
||||
if (result == SnackbarResult.ActionPerformed) {
|
||||
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clip = ClipData.newPlainText("Email", event.email)
|
||||
clipboard.setPrimaryClip(clip)
|
||||
}
|
||||
onNavigateBack()
|
||||
}
|
||||
}
|
||||
|
|
@ -66,18 +87,29 @@ fun CreateMaskedEmailScreen(
|
|||
TopAppBar(
|
||||
title = { Text("Create Masked Email") },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onNavigateBack) {
|
||||
IconButton(
|
||||
onClick = {
|
||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
onNavigateBack()
|
||||
},
|
||||
modifier = Modifier.semantics {
|
||||
contentDescription = "Navigate back"
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
contentDescription = "Back"
|
||||
contentDescription = null
|
||||
)
|
||||
}
|
||||
},
|
||||
colors = TopAppBarDefaults.topAppBarColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
titleContentColor = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
)
|
||||
},
|
||||
snackbarHost = {
|
||||
SnackbarHost(hostState = snackbarHostState)
|
||||
}
|
||||
) { paddingValues ->
|
||||
Column(
|
||||
|
|
@ -156,7 +188,10 @@ fun CreateMaskedEmailScreen(
|
|||
) {
|
||||
RadioButton(
|
||||
selected = uiState.initialState == state,
|
||||
onClick = { viewModel.onStateChange(state) },
|
||||
onClick = {
|
||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
viewModel.onStateChange(state)
|
||||
},
|
||||
enabled = !uiState.isLoading
|
||||
)
|
||||
Text(
|
||||
|
|
@ -178,7 +213,10 @@ fun CreateMaskedEmailScreen(
|
|||
Spacer(modifier = Modifier.height(32.dp))
|
||||
|
||||
Button(
|
||||
onClick = viewModel::create,
|
||||
onClick = {
|
||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
viewModel.create()
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
enabled = !uiState.isLoading && uiState.prefixError == null
|
||||
) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue