Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.thechance.qurio.data.local
import android.content.Context
import com.thechance.qurio.R
import com.thechance.qurio.domain.entity.Character


Expand All @@ -11,7 +9,6 @@ object UserDataSource{
private var lives: Int = 4
private var points: Int = 700
private var awards: Int = 0
private var currentStreak: Int = 0

fun getUserCharacter() = currentCharacter
fun updateUserCharacter(newCharacter: Character) { currentCharacter = newCharacter }
Expand All @@ -23,9 +20,4 @@ object UserDataSource{
fun addPoints(pointsToAdd: Int) { points += pointsToAdd }
fun updateAwards(newAwards: Int) { awards = newAwards }
fun incrementAwards() { awards++ }

fun getUserStreak() = currentStreak
fun updateStreak(newStreak: Int) { currentStreak = newStreak }
fun incrementStreak() { currentStreak++ }
fun resetStreak() { currentStreak = 0 }
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.thechance.qurio.data.repository

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import com.thechance.qurio.data.local.CharactersDataSource
import com.thechance.qurio.data.local.UserDataSource
import com.thechance.qurio.domain.entity.Character
import com.thechance.qurio.domain.repository.user.UserRepository
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val context: Context,
private val preferences: DataStore<Preferences>
) : UserRepository {


Expand All @@ -25,15 +37,31 @@ class UserRepositoryImpl @Inject constructor(
}

override suspend fun getUserStreak(): Int {
return UserDataSource.getUserStreak()
return preferences.data.map {
it[KEY_CURRENT_STREAK] ?: 0
}.first()
}

override suspend fun updateStreak(streak: Int) {
UserDataSource.updateStreak(streak)
override suspend fun getLastPlayedDate(): LocalDate {
return preferences.data.map {
it[KEY_LAST_PLAYED_DATE]?.let { dateString ->
LocalDate.parse(dateString)
} ?: LocalDate(1970, 1, 1)
}.first()
}

override suspend fun resetStreak() {
preferences.edit {
it[KEY_CURRENT_STREAK] = 1
it[KEY_LAST_PLAYED_DATE] = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toString()
}
}

override suspend fun incrementStreak() {
UserDataSource.incrementStreak()
preferences.edit {
it[KEY_CURRENT_STREAK] = (it[KEY_CURRENT_STREAK] ?: 0) + 1
it[KEY_LAST_PLAYED_DATE] = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toString()
}
}

override suspend fun addPoints(points: Int) {
Expand All @@ -52,4 +80,8 @@ class UserRepositoryImpl @Inject constructor(
UserDataSource.updateAwards(awards)
}

private companion object {
val KEY_CURRENT_STREAK = intPreferencesKey("current_streak")
val KEY_LAST_PLAYED_DATE = stringPreferencesKey("last_played_date")
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.thechance.qurio.domain.repository.user

import com.thechance.qurio.domain.entity.Character
import kotlinx.datetime.LocalDate

interface UserRepository {
suspend fun getUserCharacter(): Character
suspend fun updateUserCharacter(character: Character)
suspend fun getUserStatistics(): Triple<Int, Int, Int>
suspend fun getUserStreak(): Int
suspend fun updateStreak(streak: Int)
suspend fun getLastPlayedDate(): LocalDate
suspend fun resetStreak()
suspend fun incrementStreak()
suspend fun addPoints(points: Int)
suspend fun updateLives(lives: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class HomePresenter @Inject constructor(
}

private fun onGetUserStatisticsSuccess(statistics: Triple<Int, Int, Int>) {
println("get$statistics")
view.setUserStatistics(statistics)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class StartPlayFragment :
binding.skipButton.setOnClickListener {
startPlayPresenter.nextQuestion()
}

binding.backButton.setOnClickListener {
findNavController().navigate(StartPlayFragmentDirections.actionStartPlayFragmentToHomeFragment())
}
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.thechance.qurio.domain.repository.user.UserRepository
import com.thechance.qurio.presentation.base.BasePresenter
import com.thechance.qurio.presentation.screen.achievements.AchievementsManager
import com.thechance.qurio.presentation.screen.difficulty.DifficultyLevel
import kotlinx.datetime.Clock
import kotlinx.datetime.daysUntil
import kotlinx.datetime.toLocalDateTime
import javax.inject.Inject

class StartPlayPresenter @Inject constructor(
Expand Down Expand Up @@ -184,6 +187,7 @@ class StartPlayPresenter @Inject constructor(
totalTimeSeconds = session.totalTimeSeconds,
earnedCoins = session.earnedCoins
)
updateStreak()
if (stars == 0) {
tryToExecute(
callee = { decreaseLives() },
Expand All @@ -203,6 +207,18 @@ class StartPlayPresenter @Inject constructor(
)
}

private suspend fun updateStreak() {
val lastPlayedGame = userRepository.getLastPlayedDate()
val today = Clock.System.now()
.toLocalDateTime(kotlinx.datetime.TimeZone.currentSystemDefault()).date
val daysBetween = today.daysUntil(lastPlayedGame)
when (daysBetween) {
0 -> Unit
1 -> { userRepository.incrementStreak() }
else -> { userRepository.resetStreak() }
}
}

private suspend fun decreaseLives() {
val stats = userRepository.getUserStatistics()
val currentLives = stats.first
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/fragment_start_play.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/back_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:rotationY="180"
android:src="@drawable/ic_arrow_right_circle" />
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/navigation/qurio_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<argument
android:name="difficultyLevel"
app:argType="com.thechance.qurio.presentation.screen.difficulty.DifficultyLevel" />
<action
android:id="@+id/action_startPlayFragment_to_homeFragment"
app:destination="@id/homeFragment" />
</fragment>

<fragment
Expand Down
Loading