Skip to content

Commit

Permalink
Some comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
RNicolasMarin committed Mar 19, 2024
1 parent 712de93 commit 9688694
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 11 deletions.
6 changes: 2 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ dependencies {
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

//test
//Test
testImplementation("junit:junit:4.13.2")
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("io.mockk:mockk:1.12.5")

//ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
Expand All @@ -97,7 +98,4 @@ dependencies {
implementation("androidx.room:room-ktx:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")

//Test
testImplementation("io.mockk:mockk:1.12.5")

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class TwoZeroFourEightViewModel @Inject constructor(
startNewGame()
}

////check where to get the size value from later on
fun startNewGame() = viewModelScope.launch {
_gameState.update {
it.copy(isLoading = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -24,6 +23,9 @@ import com.example.two_zero_four_eight.ui.theme.White
import com.example.two_zero_four_eight.ui.theme.dimens
import com.example.two_zero_four_eight.ui.utils.shimmerEffect

/**
* It shows the name, current and record values for number or score.
* **/
@Composable
fun CurrentRecord(
data: CurrentRecordData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import com.example.two_zero_four_eight.ui.utils.shimmerEffect
import com.example.two_zero_four_eight.use_cases.DEFAULT_VALUE

/**
* [currentDirection] allows to refresh the table. Check hot to fix this bug.
* It renders the board game with empty cells and cells with numbers.
* [currentDirection] is needed to refresh the board. Check how to fix this bug.
* **/
@Composable
fun BoardGame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import androidx.compose.ui.Modifier
import com.example.two_zero_four_eight.data.model.SingleGameState
import com.example.two_zero_four_eight.ui.screens.UiSectionSizesLandscape

/**
* Component used to render the left side of the screen on landscape.
* **/
@Composable
fun BoardGameLeft(
uiState: SingleGameState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import com.example.two_zero_four_eight.ui.components.AppName
import com.example.two_zero_four_eight.ui.components.CurrentRecord
import com.example.two_zero_four_eight.ui.theme.dimens

/**
* It renders the top part of the screen on Portrait mode or the top of the left side of the screen on Landscape mode.
* **/
@Composable
fun BoardGameTop(
singlePartHeight: Dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.example.two_zero_four_eight.ui.theme.Grey1
import com.example.two_zero_four_eight.ui.theme.White
import com.example.two_zero_four_eight.use_cases.DEFAULT_VALUE

/**It has the details to know how to render a cell on the boardGame**/
data class CellData(
val backgroundColor: Color,
val textColor: Color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import androidx.compose.ui.unit.IntSize
import com.example.two_zero_four_eight.ui.theme.Grey1
import com.example.two_zero_four_eight.ui.theme.Grey2


/**
* It shows the shimmer effect while something is loading.
* **/
fun Modifier.shimmerEffect(shape: Shape = RectangleShape): Modifier = composed {

var size by remember {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CombineAndMoveNumbersUseCase {
* If [movementDirection] was [NONE] (shouldn't apply changes, it's an extra case) or
* if even after applying a move the boardGame is the same would return null.
* In other case would return the the boardGame with the move applied.
* It also update the current and record (if needed) scores if there was a change.
* **/
fun combineAndMove(
movementDirection: MovementDirection,
Expand Down Expand Up @@ -57,6 +58,7 @@ class CombineAndMoveNumbersUseCase {
/**
* It returns the MutableList<MutableList<Int>> that represents the boardGame but with the
* numbers combined/moved to the left or right.
* It also update the score each time 2 of the same consecutive number are combined.
* **/
private fun applyLeftRightMove(
movementDirection: MovementDirection,
Expand Down Expand Up @@ -112,8 +114,9 @@ class CombineAndMoveNumbersUseCase {
/**
* It returns the MutableList<MutableList<Int>> that represents the boardGame but with the
* numbers combined/moved to up or down.
* It also update the score each time 2 of the same consecutive number are combined.
*
* It similar to getLeftRightMove but with some changes on the way to move on the Lists and
* It's similar to getLeftRightMove but with some changes on the way to move on the Lists and
* how it handle the numbers.
* **/
private fun applyUpDownMove(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class CreateBoardGameUseCase @Inject constructor(
* the rows and columns with [DEFAULT_VALUE] for each cell as a default value
*
* Then adds the first and second number to the boardGame and returns it.
*
* Finally makes the request to get the record number and record score for that
* board size and creates the current state.
* **/
suspend fun createBoardGame(previousState: SingleGameState, size: Int): GameState {
var boardGame = MutableList(size) { //rows
Expand All @@ -32,11 +35,13 @@ class CreateBoardGameUseCase @Inject constructor(
boardGame = useCase.addNumber(boardGame)
boardGame = useCase.addNumber(boardGame)

//small delay to show the simmer effect better
delay(400)

val individualBestValues = repository.getIndividualBestValues(size) ?: IndividualBestValues()

return GameState(
//iif there's a previous state it uses it, if not sets a null
previousState = if (previousState.board.isEmpty()) null else previousState,
currentState = SingleGameState(
board = boardGame,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ class UpdateCurrentRecordsUseCase @Inject constructor(

private lateinit var gameState: GameState

/**
* It receives [state], modifies it and returns it.
*
* In the changes are:
*
* 1) it updates the current and record number on memory if they surpass the previous current number
* and if the current number surpass the previous record respectively.
*
* 2) it checks if it should save a new record. That's if it's not GAME_OVER and the current record
* is higher than the original record for either number or score.
*
* 3) if it's needed gets the records for the board size and if there's less than 10 add the new
* one to the db. If not replace the less relevant one with the new one using the same id.
* **/
suspend fun updateValues(state: GameState): GameState = with(state) {
gameState = this

updateNumber()
updateNumberOnMemory()

val createNewRecord = shouldCreateNewRecord(currentState.numberCurrentRecord, originalBestValues.number)
|| shouldCreateNewRecord(currentState.scoreCurrentRecord, originalBestValues.score)
Expand Down Expand Up @@ -78,7 +92,7 @@ class UpdateCurrentRecordsUseCase @Inject constructor(
return currentRecord.recordValue > originalValue
}

private fun updateNumber() = with(gameState.currentState.numberCurrentRecord) {
private fun updateNumberOnMemory() = with(gameState.currentState.numberCurrentRecord) {
val newNumber = gameState.currentState.board.flatten().max()

if (newNumber > currentValue) {
Expand Down

0 comments on commit 9688694

Please sign in to comment.