Skip to content

Commit

Permalink
Merge pull request #27 from marcopla99/process_refactorings_from_todo…
Browse files Browse the repository at this point in the history
…_list

Refactor
  • Loading branch information
marcopla99 authored Apr 26, 2023
2 parents 9934dff + f24d33a commit 1a6213a
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 187 deletions.
39 changes: 39 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
../.idea/androidTestResultsUserPreferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class FlashCardRepositoryTest {
}

@Test
fun whenAddingResult_thenIsSaved() {
fun whenAddingResult_thenIsSaved() = runTest {
val quizResult = QuizResult(
FlashCard("Engels", "English"),
"Dutch",
Expand All @@ -130,7 +130,7 @@ class FlashCardRepositoryTest {

repository.addResult(quizResult)

val currentResults = repository.getCurrentResults()
val currentResults = repository.getCurrentResults().first()
assertEquals(listOf(quizResult), currentResults)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.marcopla.flashcards.presentation.add

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.*
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsFocused
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.marcopla.flashcards.R
import com.marcopla.flashcards.data.repository.FlashCardRepository
import com.marcopla.flashcards.domain.usecase.AddUseCase
import com.marcopla.flashcards.presentation.screen.add.AddScreen
import com.marcopla.flashcards.presentation.screen.add.AddRoute
import com.marcopla.flashcards.presentation.screen.add.AddViewModel
import com.marcopla.testing_shared.TestFlashCardRepository

Expand All @@ -19,9 +24,8 @@ fun launchAddScreen(
repository: FlashCardRepository = TestFlashCardRepository(),
block: AddScreenRobot.() -> Unit
): AddScreenRobot {
composeRule.setContent {
AddScreen(viewModel = AddViewModel(AddUseCase(repository)))
}
val viewModel = AddViewModel(AddUseCase(repository))
composeRule.setContent { AddRoute(viewModel = viewModel) }
return AddScreenRobot(composeRule).apply(block)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.marcopla.flashcards.presentation.carousel

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.*
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.marcopla.flashcards.R
import com.marcopla.flashcards.data.model.FlashCard
import com.marcopla.flashcards.data.repository.FlashCardRepositoryImpl
import com.marcopla.flashcards.domain.usecase.LoadUseCase
import com.marcopla.flashcards.domain.usecase.SubmitQuizUseCase
import com.marcopla.flashcards.presentation.screen.carousel.CarouselScreen
import com.marcopla.flashcards.presentation.screen.carousel.CarouselRoute
import com.marcopla.flashcards.presentation.screen.carousel.CarouselViewModel
import com.marcopla.testing_shared.FakeFlashCardDao

Expand All @@ -21,15 +25,12 @@ fun launchCarouselScreen(
flashCards: List<FlashCard>,
block: CarouselScreenRobot.() -> Unit
): CarouselScreenRobot {
composeRule.setContent {
val repository = FlashCardRepositoryImpl(FakeFlashCardDao(flashCards))
CarouselScreen(
viewModel = CarouselViewModel(
LoadUseCase(repository),
SubmitQuizUseCase(repository)
)
) {}
}
val repository = FlashCardRepositoryImpl(FakeFlashCardDao(flashCards))
val viewModel = CarouselViewModel(
LoadUseCase(repository),
SubmitQuizUseCase(repository)
)
composeRule.setContent { CarouselRoute(viewModel) {} }
return CarouselScreenRobot(composeRule).apply(block)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.marcopla.flashcards.presentation.edit

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.*
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.marcopla.flashcards.R
Expand All @@ -13,7 +17,7 @@ import com.marcopla.flashcards.domain.usecase.DeleteUseCase
import com.marcopla.flashcards.domain.usecase.EditUseCase
import com.marcopla.flashcards.domain.usecase.LoadUseCase
import com.marcopla.flashcards.presentation.navigation.FLASH_CARD_ID_ARG_KEY
import com.marcopla.flashcards.presentation.screen.edit.EditScreen
import com.marcopla.flashcards.presentation.screen.edit.EditRoute
import com.marcopla.flashcards.presentation.screen.edit.EditViewModel
import com.marcopla.testing_shared.FakeFlashCardDao
import com.marcopla.testing_shared.TestFlashCardRepository
Expand All @@ -27,19 +31,16 @@ fun launchEditScreenFor(
flashCardRepository: FlashCardRepository = TestFlashCardRepository(),
block: EditScreenRobot.() -> Unit
): EditScreenRobot {
val viewModel = EditViewModel(
SavedStateHandle(mapOf(FLASH_CARD_ID_ARG_KEY to selectedFlashCard.id)),
EditUseCase(flashCardRepository),
LoadUseCase(
FlashCardRepositoryImpl(FakeFlashCardDao(listOf(selectedFlashCard)))
),
DeleteUseCase(TestFlashCardRepository())
)
composeRule.setContent {
EditScreen(
viewModel = EditViewModel(
SavedStateHandle(mapOf(FLASH_CARD_ID_ARG_KEY to selectedFlashCard.id)),
EditUseCase(flashCardRepository),
LoadUseCase(
FlashCardRepositoryImpl(FakeFlashCardDao(listOf(selectedFlashCard)))
),
DeleteUseCase(TestFlashCardRepository())
),
onFlashCardEdited = {},
onFlashCardDeleted = {}
)
EditRoute(viewModel) {}
}
return EditScreenRobot(composeRule).apply(block)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.marcopla.flashcards.R
import com.marcopla.flashcards.data.model.FlashCard
import com.marcopla.flashcards.data.repository.FlashCardRepositoryImpl
import com.marcopla.flashcards.domain.usecase.LoadUseCase
import com.marcopla.flashcards.presentation.screen.home.HomeScreen
import com.marcopla.flashcards.presentation.screen.home.HomeViewModel
import com.marcopla.testing_shared.FakeFlashCardDao
import com.marcopla.flashcards.presentation.screen.home.HomeScreenState

typealias ComponentActivityTestRule =
AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity>

suspend fun launchHomeScreen(
rule: ComponentActivityTestRule,
flashCards: List<FlashCard> = emptyList(),
screenState: HomeScreenState,
block: suspend HomeScreenRobot.() -> Unit
): HomeScreenRobot {
val repository = FlashCardRepositoryImpl(FakeFlashCardDao(flashCards))
rule.setContent {
HomeScreen(
viewModel = HomeViewModel(LoadUseCase(repository)),
screenState = screenState,
onNavigateToAddScreen = {},
onItemClicked = {},
onNavigateToCarouselScreen = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.marcopla.flashcards.presentation.home
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.marcopla.flashcards.data.model.FlashCard
import com.marcopla.flashcards.presentation.screen.home.HomeScreenState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Rule
Expand All @@ -16,7 +17,7 @@ class HomeScreenTest {

@Test
fun homeScreen_whenGettingEmptyState_thenShowEmptyMessage() = runTest {
launchHomeScreen(composeRule, emptyList()) {
launchHomeScreen(composeRule, HomeScreenState.Empty) {
// Do nothing
} verify {
emptyMessageIsDisplayed()
Expand All @@ -31,7 +32,7 @@ class HomeScreenTest {
FlashCard(frontText = "front3", backText = "back3")
)

launchHomeScreen(composeRule, flashCards) {
launchHomeScreen(composeRule, HomeScreenState.Cards(flashCards)) {
// Do nothing
} verify {
listOfFlashCardsIsDisplayed(flashCards)
Expand All @@ -40,7 +41,7 @@ class HomeScreenTest {

@Test
fun homeScreen_whenNoFlashCards_thenDoNotShowCarouselButton() = runTest {
launchHomeScreen(composeRule, emptyList()) {
launchHomeScreen(composeRule, HomeScreenState.Empty) {
// Empty
} verify {
carouselButtonIsNotDisplayed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface FlashCardRepository {

fun addResult(quizResult: QuizResult)

fun getCurrentResults(): List<QuizResult>
fun getCurrentResults(): Flow<List<QuizResult>>

fun clearResults()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.marcopla.flashcards.data.datasource.FlashCardDao
import com.marcopla.flashcards.data.model.FlashCard
import com.marcopla.flashcards.data.model.QuizResult
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class FlashCardRepositoryImpl(
private val flashCardDao: FlashCardDao
Expand Down Expand Up @@ -49,8 +50,8 @@ class FlashCardRepositoryImpl(
* Get the stored results for this session.
* Note: results are currently just saved in memory.
*/
override fun getCurrentResults(): List<QuizResult> {
return currentResults
override fun getCurrentResults(): Flow<List<QuizResult>> {
return flow { emit(currentResults) }
}

override fun clearResults() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.marcopla.flashcards.domain.usecase
import com.marcopla.flashcards.data.model.QuizResult
import com.marcopla.flashcards.data.repository.FlashCardRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

// TODO: Currently the results are stored only in memory. Would be better to store them on disk.
class LoadResultsUseCase @Inject constructor(private val repository: FlashCardRepository) {

operator fun invoke(): List<QuizResult> {
operator fun invoke(): Flow<List<QuizResult>> {
return repository.getCurrentResults()
}

Expand Down
Loading

0 comments on commit 1a6213a

Please sign in to comment.