Skip to content

Commit

Permalink
feat(study): bật tắt âm thanh trong khi học (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Daocon <[email protected]>
  • Loading branch information
VawnDao and VawnDao authored Dec 6, 2024
1 parent ac664b6 commit ed37355
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pwhs.quickmem.presentation.app.study_set.studies.component

import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons.Default
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.RestartAlt
Expand All @@ -11,9 +12,11 @@ import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import com.pwhs.quickmem.R
import com.pwhs.quickmem.ui.theme.QuickMemTheme

Expand All @@ -28,6 +31,8 @@ fun StudyTopAppBar(
onRestartClicked: () -> Unit = {},
shouldShowRestart: Boolean = true,
isGetAll: Boolean = false,
isPlaySound: Boolean = false,
onChangeIsPlaySound: (Boolean) -> Unit = {}
) {
CenterAlignedTopAppBar(
modifier = modifier,
Expand All @@ -53,6 +58,17 @@ fun StudyTopAppBar(
},
actions = {
if (shouldShowRestart && isGetAll) {
IconButton(
onClick = { onChangeIsPlaySound(!isPlaySound) }
) {
Icon(
painter = if (isPlaySound) painterResource(id = R.drawable.ic_sound) else painterResource(
id = R.drawable.ic_volume_off
),
contentDescription = "Sound",
modifier = Modifier.size(22.dp)
)
}
IconButton(
onClick = onRestartClicked
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ fun FlipFlashCardScreen(
isSwipingLeft = uiState.isSwipingLeft,
isSwipingRight = uiState.isSwipingRight,
isEndOfList = uiState.isEndOfList,
isPlaySound = uiState.isPlaySound,
onChangeIsPlaySound = {
viewModel.onEvent(FlipFlashCardUiAction.OnChangeIsPlaySound(it))
},
learningTime = uiState.learningTime,
onEndSessionClick = {
viewModel.onEvent(FlipFlashCardUiAction.OnBackClicked)
Expand Down Expand Up @@ -145,6 +149,8 @@ fun FlipFlashCard(
isSwipingLeft: Boolean = false,
isSwipingRight: Boolean = false,
isEndOfList: Boolean = false,
isPlaySound: Boolean = false,
onChangeIsPlaySound: (Boolean) -> Unit = { },
learningTime: Long = 0L,
onEndSessionClick: () -> Unit = { },
onUpdatedCardIndex: (Int) -> Unit = { },
Expand Down Expand Up @@ -202,7 +208,9 @@ fun FlipFlashCard(
stackState.reset()
},
shouldShowRestart = !isEndOfList,
isGetAll = isGetAll
isGetAll = isGetAll,
isPlaySound = isPlaySound,
onChangeIsPlaySound = onChangeIsPlaySound
)
}
) { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ sealed class FlipFlashCardUiAction {
data object OnRestartClicked : FlipFlashCardUiAction()
data object OnContinueLearningClicked : FlipFlashCardUiAction()
data object OnBackClicked : FlipFlashCardUiAction()
data class OnChangeIsPlaySound(val isPlaySound: Boolean) : FlipFlashCardUiAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ class FlipFlashCardViewModel @Inject constructor(
}
_uiEvent.trySend(FlipFlashCardUiEvent.Back)
}

is FlipFlashCardUiAction.OnChangeIsPlaySound -> {
_uiState.update {
it.copy(isPlaySound = event.isPlaySound)
}
viewModelScope.launch {
appManager.saveIsPlaySound(event.isPlaySound)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.Icons.Default
import androidx.compose.material.icons.automirrored.filled.NavigateNext
Expand Down Expand Up @@ -35,6 +36,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -116,7 +118,11 @@ fun LearnByQuizScreen(
onRestartClicked = {
viewModel.onEvent(LearnByQuizUiAction.RestartLearn)
},
isGetAll = uiState.isGetAll
isGetAll = uiState.isGetAll,
isPlaySound = uiState.isPlaySound,
onChangeIsPlaySound = {
viewModel.onEvent(LearnByQuizUiAction.OnChangeIsPlaySound(it))
}
)
}

Expand All @@ -139,7 +145,9 @@ fun LearnByQuiz(
isEndOfList: Boolean = false,
onContinueLearningClicked: () -> Unit = {},
onRestartClicked: () -> Unit = {},
isGetAll: Boolean = false
isGetAll: Boolean = false,
isPlaySound: Boolean = false,
onChangeIsPlaySound: (Boolean) -> Unit = {}
) {
var canResetState by remember { mutableStateOf(false) }
val showHintBottomSheet = remember { mutableStateOf(false) }
Expand Down Expand Up @@ -179,6 +187,17 @@ fun LearnByQuiz(
},
actions = {
if (!isEndOfList && isGetAll) {
IconButton(
onClick = { onChangeIsPlaySound(!isPlaySound) }
) {
Icon(
painter = if (isPlaySound) painterResource(id = R.drawable.ic_sound) else painterResource(
id = R.drawable.ic_volume_off
),
contentDescription = "Sound",
modifier = Modifier.size(22.dp)
)
}
if (!flashCard?.hint.isNullOrEmpty()) {
IconButton(
onClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ sealed class LearnByQuizUiAction {
data object ContinueLearnWrongAnswer : LearnByQuizUiAction()
data object RestartLearn : LearnByQuizUiAction()
data object OnBackClicked : LearnByQuizUiAction()
data class OnChangeIsPlaySound(val isPlaySound: Boolean) : LearnByQuizUiAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ class LearnByQuizViewModel @Inject constructor(
}
_uiEvent.trySend(LearnByQuizUiEvent.Back)
}

is LearnByQuizUiAction.OnChangeIsPlaySound -> {
_uiState.update {
it.copy(isPlaySound = event.isPlaySound)
}
viewModelScope.launch {
appManager.saveIsPlaySound(event.isPlaySound)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -112,7 +113,11 @@ fun LearnByTrueFalseScreen(
)
)
},
isGetAll = uiState.isGetAll
isGetAll = uiState.isGetAll,
isPlaySound = uiState.isPlaySound,
onChangeIsPlaySound = {
viewModel.onEvent(LearnByTrueFalseUiAction.OnChangeIsPlaySound(it))
}
)
}

Expand All @@ -133,7 +138,9 @@ fun LearnByTrueFalse(
learningTime: Long = 0,
listWrongAnswer: List<TrueFalseQuestion> = emptyList(),
onContinueLearningClicked: () -> Unit = {},
isGetAll: Boolean = false
isGetAll: Boolean = false,
isPlaySound: Boolean = false,
onChangeIsPlaySound: (Boolean) -> Unit = {}
) {
var isImageViewerOpen by remember { mutableStateOf(false) }
var definitionImageUri by remember { mutableStateOf("") }
Expand Down Expand Up @@ -170,6 +177,17 @@ fun LearnByTrueFalse(
},
actions = {
if (!isEndOfList && isGetAll) {
IconButton(
onClick = { onChangeIsPlaySound(!isPlaySound) }
) {
Icon(
painter = if (isPlaySound) painterResource(id = R.drawable.ic_sound) else painterResource(
id = R.drawable.ic_volume_off
),
contentDescription = "Sound",
modifier = Modifier.size(22.dp)
)
}
IconButton(
onClick = onRestart
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ sealed class LearnByTrueFalseUiAction {
data object ContinueLearnWrongAnswer : LearnByTrueFalseUiAction()
data object RestartLearn : LearnByTrueFalseUiAction()
data object OnBackClicked : LearnByTrueFalseUiAction()
data class OnChangeIsPlaySound(val isPlaySound: Boolean) : LearnByTrueFalseUiAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ class LearnByTrueFalseViewModel @Inject constructor(
}
_uiEvent.trySend(LearnByTrueFalseUiEvent.Back)
}

is LearnByTrueFalseUiAction.OnChangeIsPlaySound -> {
_uiState.update {
it.copy(isPlaySound = event.isPlaySound)
}
viewModelScope.launch {
appManager.saveIsPlaySound(event.isPlaySound)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
Expand Down Expand Up @@ -124,7 +125,11 @@ fun LearnByWriteScreen(
onSubmitAnswer = { id, status, answer ->
viewModel.onEvent(LearnByWriteUiAction.OnAnswer(id, status, answer))
},
isGetAll = uiState.isGetAll
isGetAll = uiState.isGetAll,
isPlaySound = uiState.isPlaySound,
onChangeIsPlaySound = {
viewModel.onEvent(LearnByWriteUiAction.OnChangeIsPlaySound(it))
}
)
}

Expand All @@ -145,7 +150,9 @@ fun LearnByWrite(
listWrongAnswer: List<WriteQuestion> = emptyList(),
onContinueLearningClicked: () -> Unit = {},
onSubmitAnswer: (String, WriteStatus, String) -> Unit = { _, _, _ -> },
isGetAll: Boolean = false
isGetAll: Boolean = false,
isPlaySound: Boolean = false,
onChangeIsPlaySound: (Boolean) -> Unit = { },
) {
var isImageViewerOpen by remember { mutableStateOf(false) }
var definitionImageUri by remember { mutableStateOf("") }
Expand Down Expand Up @@ -193,6 +200,17 @@ fun LearnByWrite(
},
actions = {
if (!isEndOfList && isGetAll) {
IconButton(
onClick = { onChangeIsPlaySound(!isPlaySound) }
) {
Icon(
painter = if (isPlaySound) painterResource(id = R.drawable.ic_sound) else painterResource(
id = R.drawable.ic_volume_off
),
contentDescription = "Sound",
modifier = Modifier.size(22.dp)
)
}
IconButton(
onClick = {
userAnswer = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ sealed class LearnByWriteUiAction {
data object ContinueLearnWrongAnswer : LearnByWriteUiAction()
data object RestartLearn : LearnByWriteUiAction()
data object OnBackClicked : LearnByWriteUiAction()
data class OnChangeIsPlaySound(val isPlaySound: Boolean) : LearnByWriteUiAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class LearnByWriteViewModel @Inject constructor(
}
_uiEvent.trySend(LearnByWriteUiEvent.Back)
}

is LearnByWriteUiAction.OnChangeIsPlaySound -> {
_uiState.update {
it.copy(isPlaySound = event.isPlaySound)
}
viewModelScope.launch {
appManager.saveIsPlaySound(event.isPlaySound)
}
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_volume_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M792,904 L671,783q-25,16 -53,27.5T560,829v-82q14,-5 27.5,-10t25.5,-12L480,592v208L280,600L120,600v-240h128L56,168l56,-56 736,736 -56,56ZM784,672 L726,614q17,-31 25.5,-65t8.5,-70q0,-94 -55,-168T560,211v-82q124,28 202,125.5T840,479q0,53 -14.5,102T784,672ZM650,538l-90,-90v-130q47,22 73.5,66t26.5,96q0,15 -2.5,29.5T650,538ZM480,368 L376,264l104,-104v208ZM400,606v-94l-72,-72L200,440v80h114l86,86ZM364,476Z"
android:fillColor="#e8eaed"/>
</vector>

0 comments on commit ed37355

Please sign in to comment.