Skip to content

Commit

Permalink
Revamped quick questions
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan committed Sep 21, 2024
1 parent 19bf9b2 commit bd70ddd
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,6 @@ internal class DefaultInMemoryChoiceDataSource @Inject constructor(
)
}

override suspend fun getScore(): Int {
return withContext(defaultDispatcher) {
getQuestionsWithSelectedChoices().count {
it.value.toSet() == it.key.correctChoices.toSet()
}
}
}

private suspend fun getQuestionsWithSelectedChoices(): Map<Question, List<String>> {
return withContext(defaultDispatcher) {
_selectedChoices.groupBy({ it.question }, { it.choice })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ interface InMemoryChoiceDataSource {
fun clearCache()

suspend fun addCurrentQuestion(question: Question)

suspend fun getScore(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

Expand Down Expand Up @@ -179,25 +178,4 @@ class InMemoryChoiceDataSourceTest {

assertNotNull(inMemoryChoiceDataSource.currentQuestionData.replayCache.firstOrNull())
}

@Test
fun getScore() = runTest {
val question = Question(
question = "Question",
correctChoices = listOf(""),
wrongChoices = listOf(),
choices = listOf(""),
)

val choice = Choice(
question = question,
choice = "",
)

inMemoryChoiceDataSource.addChoice(choice = choice)

inMemoryChoiceDataSource.addCurrentQuestion(question = question)

assertEquals(expected = 1, actual = inMemoryChoiceDataSource.getScore())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,4 @@ interface ChoiceRepository {
fun clearCache()

suspend fun addCurrentQuestion(question: Question)

suspend fun getScore(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,4 @@ internal class DefaultChoiceRepository @Inject constructor(
override suspend fun addCurrentQuestion(question: Question) {
inMemoryChoiceDataSource.addCurrentQuestion(question)
}

override suspend fun getScore(): Int {
return inMemoryChoiceDataSource.getScore()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ class FakeChoiceRepository : ChoiceRepository {
)
}

override suspend fun getScore(): Int {
return getQuestionsWithSelectedChoices().count {
it.value.toSet() == it.key.correctChoices.toSet()
}
}

private fun getQuestionsWithSelectedChoices(): Map<Question, List<String>> {
return _selectedChoices.groupBy({ it.question }, { it.choice })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ class QuestionViewModel @Inject constructor(
}
}

fun showCorrectChoices(questionSettingIndex: Int, questions: List<Question>) {
fun showCorrectChoices(questionSettingIndex: Int, questions: List<Question>, score: Int) {
viewModelScope.launch {
val score = choiceRepository.getScore()

_questionUiState.update {
QuestionUiState.CorrectChoices(
questions = questions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal fun QuestionScreen(
countDownTime: CountDownTime?,
onAddCurrentQuestion: (Question) -> Unit,
onUpdateChoice: (Choice) -> Unit,
onShowCorrectChoices: (questionSettingIndex: Int, questions: List<Question>) -> Unit,
onShowCorrectChoices: (questionSettingIndex: Int, questions: List<Question>, score: Int) -> Unit,
onStartQuestions: (Int, QuestionSetting) -> Unit,
onStartQuickQuestions: () -> Unit,
onQuitQuestions: () -> Unit,
Expand Down Expand Up @@ -239,7 +239,7 @@ private fun Questions(
countDownTime: CountDownTime?,
onAddCurrentQuestion: (Question) -> Unit,
onUpdateChoice: (Choice) -> Unit,
onShowCorrectChoices: (questionSettingIndex: Int, questions: List<Question>) -> Unit,
onShowCorrectChoices: (questionSettingIndex: Int, questions: List<Question>, score: Int) -> Unit,
onQuitQuestions: () -> Unit,
) {
val pagerState = rememberPagerState(
Expand All @@ -258,13 +258,17 @@ private fun Questions(
mutableStateOf(false)
}

var correctScoreCount by rememberSaveable {
mutableIntStateOf(0)
}

BackHandler(enabled = true) {
showQuitAlertDialog = true
}

LaunchedEffect(key1 = countDownTime) {
if (countDownTime != null && countDownTime.isFinished) {
onShowCorrectChoices(questionSettingIndex, questions)
onShowCorrectChoices(questionSettingIndex, questions, correctScoreCount)
}
}

Expand All @@ -286,7 +290,7 @@ private fun Questions(
) {
FloatingActionButton(
onClick = {
onShowCorrectChoices(questionSettingIndex, questions)
onShowCorrectChoices(questionSettingIndex, questions, correctScoreCount)
},
) {
Icon(
Expand All @@ -303,7 +307,7 @@ private fun Questions(
.consumeWindowInsets(paddingValues)
.padding(paddingValues),
) {
QuestionScreenTopBar(
QuestionTopBar(
questionsWithSelectedChoicesSize = currentQuestionData.questionsWithSelectedChoices.size,
countDownTime = countDownTime,
)
Expand All @@ -327,7 +331,11 @@ private fun Questions(
page = page,
questions = questions,
currentQuestionData = currentQuestionData,
onUpdateChoice = onUpdateChoice,
onUpdateChoice = { choice, isCorrect ->
if (isCorrect) correctScoreCount++

onUpdateChoice(choice)
},
)
}
}
Expand All @@ -350,7 +358,7 @@ private fun Questions(
}

@Composable
private fun QuestionScreenTopBar(
private fun QuestionTopBar(
modifier: Modifier = Modifier,
questionsWithSelectedChoicesSize: Int,
countDownTime: CountDownTime?,
Expand Down Expand Up @@ -395,7 +403,7 @@ private fun QuestionPage(
page: Int,
questions: List<Question>,
currentQuestionData: QuestionData,
onUpdateChoice: (Choice) -> Unit,
onUpdateChoice: (Choice, Boolean) -> Unit,
) {
Column(
modifier = modifier
Expand All @@ -409,6 +417,7 @@ private fun QuestionPage(
QuestionChoicesSelection(
currentQuestion = questions[page],
choices = questions[page].choices,
correctChoices = questions[page].correctChoices,
currentQuestionData = currentQuestionData,
onUpdateChoice = onUpdateChoice,
)
Expand Down Expand Up @@ -436,8 +445,9 @@ private fun QuestionChoicesSelection(
modifier: Modifier = Modifier,
currentQuestion: Question,
choices: List<String>,
correctChoices: List<String>,
currentQuestionData: QuestionData,
onUpdateChoice: (Choice) -> Unit,
onUpdateChoice: (Choice, Boolean) -> Unit,
) {
val greenGradientColors = listOf(
Color(0xFF43A047),
Expand Down Expand Up @@ -488,6 +498,7 @@ private fun QuestionChoicesSelection(
question = currentQuestion,
choice = choice,
),
choice in correctChoices,
)
scope.launch {
choiceAnimation.animateTo(
Expand Down
Loading

0 comments on commit bd70ddd

Please sign in to comment.