Skip to content

Commit

Permalink
Revamped questions feature (#58)
Browse files Browse the repository at this point in the history
* Simplify questions feature

* Shorten Animated Countdown timer
  • Loading branch information
JackEblan authored Sep 20, 2024
1 parent 6b8810b commit 01112de
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 612 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.eblan.socialworkreviewer.core.cache

import com.eblan.socialworkreviewer.core.common.Dispatcher
import com.eblan.socialworkreviewer.core.common.SwrDispatchers.Default
import com.eblan.socialworkreviewer.core.model.AnsweredQuestion
import com.eblan.socialworkreviewer.core.model.Choice
import com.eblan.socialworkreviewer.core.model.Question
import com.eblan.socialworkreviewer.core.model.QuestionData
Expand Down Expand Up @@ -63,7 +62,6 @@ internal class DefaultInMemoryChoiceDataSource @Inject constructor(
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}
Expand All @@ -79,11 +77,29 @@ internal class DefaultInMemoryChoiceDataSource @Inject constructor(
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}

override suspend fun replaceChoice(oldChoice: Choice, newChoice: Choice) {
val oldChoiceIndex = _selectedChoices.indexOf(oldChoice)

if (oldChoiceIndex != -1) {
_selectedChoices[oldChoiceIndex] = newChoice

_currentQuestionData.emit(
QuestionData(
question = newChoice.question,
selectedChoices = getQuestionsWithSelectedChoices().getOrDefault(
key = newChoice.question,
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
),
)
}
}

override fun clearCache() {
_questions.clear()

Expand All @@ -101,7 +117,6 @@ internal class DefaultInMemoryChoiceDataSource @Inject constructor(
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}
Expand All @@ -119,15 +134,4 @@ internal class DefaultInMemoryChoiceDataSource @Inject constructor(
_selectedChoices.groupBy({ it.question }, { it.choice })
}
}

private suspend fun getAnsweredQuestions(): List<AnsweredQuestion> {
return withContext(defaultDispatcher) {
_questions.map { question ->
AnsweredQuestion(
question = question,
isAnswered = question in getQuestionsWithSelectedChoices(),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ interface InMemoryChoiceDataSource {

suspend fun deleteChoice(choice: Choice)

suspend fun replaceChoice(oldChoice: Choice, newChoice: Choice)

fun clearCache()

suspend fun addCurrentQuestion(question: Question)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ class InMemoryChoiceDataSourceTest {
assertNotNull(inMemoryChoiceDataSource.currentQuestionData.replayCache.firstOrNull())
}

@Test
fun replaceChoice() = runTest {
val oldChoice = Choice(
question = Question(
question = "0",
correctChoices = listOf(),
wrongChoices = listOf(),
choices = listOf(""),
),
choice = "",
)

val newChoice = Choice(
question = Question(
question = "1",
correctChoices = listOf(),
wrongChoices = listOf(),
choices = listOf(""),
),
choice = "",
)

inMemoryChoiceDataSource.addChoice(choice = oldChoice)

inMemoryChoiceDataSource.replaceChoice(oldChoice = oldChoice, newChoice = newChoice)

assertTrue {
inMemoryChoiceDataSource.selectedChoices.contains(newChoice)
}

assertTrue {
inMemoryChoiceDataSource.selectedChoices.contains(oldChoice).not()
}

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

@Test
fun clearCache() = runTest {
val questions = List(10) { index ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface ChoiceRepository {

suspend fun deleteChoice(choice: Choice)

suspend fun replaceChoice(oldChoice: Choice, newChoice: Choice)

fun clearCache()

suspend fun addCurrentQuestion(question: Question)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ internal class DefaultChoiceRepository @Inject constructor(
inMemoryChoiceDataSource.deleteChoice(choice)
}

override suspend fun replaceChoice(oldChoice: Choice, newChoice: Choice) {
inMemoryChoiceDataSource.replaceChoice(oldChoice = oldChoice, newChoice = newChoice)
}

override fun clearCache() {
inMemoryChoiceDataSource.clearCache()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class UpdateChoiceUseCase @Inject constructor(
}

private suspend fun singleChoice(choice: Choice) {
choiceRepository.selectedChoices.find { previousSelectedChoice ->
choice.question == previousSelectedChoice.question && choice != previousSelectedChoice
}?.let { previousSelectedChoice ->
choiceRepository.deleteChoice(previousSelectedChoice)
val oldChoice = choiceRepository.selectedChoices.find { oldChoice ->
choice.question == oldChoice.question && choice != oldChoice
}

if (choice !in choiceRepository.selectedChoices) {
if (oldChoice != null) {
choiceRepository.replaceChoice(oldChoice = oldChoice, newChoice = choice)
} else {
choiceRepository.addChoice(choice)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ data class QuestionData(
val question: Question,
val selectedChoices: List<String>,
val questionsWithSelectedChoices: Map<Question, List<String>>,
val answeredQuestions: List<AnsweredQuestion>,
)

data class AnsweredQuestion(val question: Question, val isAnswered: Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.eblan.socialworkreviewer.core.testing.repository

import com.eblan.socialworkreviewer.core.data.repository.ChoiceRepository
import com.eblan.socialworkreviewer.core.model.AnsweredQuestion
import com.eblan.socialworkreviewer.core.model.Choice
import com.eblan.socialworkreviewer.core.model.Question
import com.eblan.socialworkreviewer.core.model.QuestionData
Expand Down Expand Up @@ -60,7 +59,6 @@ class FakeChoiceRepository : ChoiceRepository {
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}
Expand All @@ -76,11 +74,29 @@ class FakeChoiceRepository : ChoiceRepository {
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}

override suspend fun replaceChoice(oldChoice: Choice, newChoice: Choice) {
val oldChoiceIndex = _selectedChoices.indexOf(oldChoice)

if (oldChoiceIndex != -1) {
_selectedChoices[oldChoiceIndex] = newChoice

_currentQuestionData.emit(
QuestionData(
question = newChoice.question,
selectedChoices = getQuestionsWithSelectedChoices().getOrDefault(
key = newChoice.question,
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
),
)
}
}

override fun clearCache() {
_selectedChoices.clear()

Expand All @@ -96,7 +112,6 @@ class FakeChoiceRepository : ChoiceRepository {
defaultValue = emptyList(),
),
questionsWithSelectedChoices = getQuestionsWithSelectedChoices(),
answeredQuestions = getAnsweredQuestions(),
),
)
}
Expand All @@ -110,13 +125,4 @@ class FakeChoiceRepository : ChoiceRepository {
private fun getQuestionsWithSelectedChoices(): Map<Question, List<String>> {
return _selectedChoices.groupBy({ it.question }, { it.choice })
}

private fun getAnsweredQuestions(): List<AnsweredQuestion> {
return _questions.map { question ->
AnsweredQuestion(
question = question,
isAnswered = question in getQuestionsWithSelectedChoices(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ sealed interface QuestionUiState {

data class CorrectChoices(
val questions: List<Question>,
val score: Int,
val lastCountDownTime: String?,
) : QuestionUiState

data class OnBoarding(val category: Category? = null, val statistics: Statistics) :
QuestionUiState

data class QuickQuestions(val questions: List<Question>) : QuestionUiState

data class Score(
val score: Int,
val questions: List<Question>,
val lastCountDownTime: String?,
) : QuestionUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class QuestionViewModel @Inject constructor(
),
selectedChoices = emptyList(),
questionsWithSelectedChoices = emptyMap(),
answeredQuestions = emptyList(),
),
)

Expand Down Expand Up @@ -145,24 +144,14 @@ class QuestionViewModel @Inject constructor(
}
}

fun showCorrectChoices(questions: List<Question>) {
viewModelScope.launch {
_questionUiState.update {
QuestionUiState.CorrectChoices(
questions = questions,
)
}
}
}

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

_questionUiState.update {
QuestionUiState.Score(
score = score,
QuestionUiState.CorrectChoices(
questions = questions,
score = score,
lastCountDownTime = countDownTime.value?.minutes,
)
}
Expand Down
Loading

0 comments on commit 01112de

Please sign in to comment.