Skip to content

Commit

Permalink
♻️ Refactor code for button color
Browse files Browse the repository at this point in the history
  • Loading branch information
prime2do committed Nov 11, 2023
1 parent b1037f2 commit 306de3f
Showing 1 changed file with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -54,6 +55,14 @@ fun TextToSpeechScreen(
) {
val uiState by viewModel.uiState.collectAsState()
val context = LocalContext.current
val activatedButtonColor: ButtonColors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onBackground
)
val deactivatedButtonColor: ButtonColors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.outline
)

Surface(
modifier = modifier.fillMaxSize()
Expand Down Expand Up @@ -101,13 +110,17 @@ fun TextToSpeechScreen(
Row {
TextToSpeechButton(
buttonStatus = uiState.buttonStatus,
activatedButtonColor = activatedButtonColor,
deactivatedButtonColor = deactivatedButtonColor,
onPlay = { viewModel.ttsStart(context) },
onStop = { viewModel.ttsStop() },
viewModel = viewModel
)

TextClearButton(
buttonStatus = uiState.buttonStatus,
activatedButtonColor = activatedButtonColor,
deactivatedButtonColor = deactivatedButtonColor,
onPlay = { viewModel.clearText() },
onStop = {},
viewModel = viewModel
Expand All @@ -121,23 +134,20 @@ fun TextToSpeechScreen(
@Composable
private fun TextToSpeechButton(
buttonStatus: ButtonStatusType,
activatedButtonColor: ButtonColors,
deactivatedButtonColor: ButtonColors,
onPlay: () -> Unit,
onStop: () -> Unit,
viewModel: TextToSpeechViewModel
) {
val textToSpeechButtonColors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onBackground
)

if (buttonStatus == ButtonStatusType.PLAY && !viewModel.isEmptyText()) {
Button(
onClick = onPlay,
modifier = Modifier.size(
width = 200.dp,
height = 50.dp
),
colors = textToSpeechButtonColors
colors = activatedButtonColor
) {
Text(
text = stringResource(id = R.string.play_text),
Expand All @@ -156,10 +166,7 @@ private fun TextToSpeechButton(
width = 200.dp,
height = 50.dp
),
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.outline
)
colors = deactivatedButtonColor
) {
Text(
text = stringResource(id = R.string.play_text),
Expand All @@ -178,7 +185,7 @@ private fun TextToSpeechButton(
width = 200.dp,
height = 50.dp
),
colors = textToSpeechButtonColors
colors = activatedButtonColor
) {
Text(
text = stringResource(id = R.string.stop_text),
Expand All @@ -196,6 +203,8 @@ private fun TextToSpeechButton(
@Composable
fun TextClearButton(
buttonStatus: ButtonStatusType,
activatedButtonColor: ButtonColors,
deactivatedButtonColor: ButtonColors,
onPlay: () -> Unit,
onStop: () -> Unit,
viewModel: TextToSpeechViewModel
Expand All @@ -207,10 +216,7 @@ fun TextClearButton(
width = 200.dp,
height = 50.dp
),
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onBackground
)
colors = activatedButtonColor
) {
Text(
text = stringResource(id = R.string.clear_text),
Expand All @@ -229,10 +235,7 @@ fun TextClearButton(
width = 200.dp,
height = 50.dp
),
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.outline
)
colors = deactivatedButtonColor
) {
Text(
text = stringResource(id = R.string.clear_text),
Expand Down

0 comments on commit 306de3f

Please sign in to comment.