Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-magda committed Nov 9, 2023
1 parent adab561 commit 4371388
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.hyperskill.app.share_streak.cache

import com.russhwolf.settings.Settings
import kotlinx.datetime.Instant
import org.hyperskill.app.share_streak.data.source.ShareStreakCacheDataSource

internal class ShareStreakCacheDataSourceImpl(private val settings: Settings) : ShareStreakCacheDataSource {
override fun getLastTimeShareStreakShownEpochMilliseconds(): Long? =
settings.getLongOrNull(ShareStreakCacheKeyValues.SHARE_STREAK_LAST_TIME_SHOWN)
override fun getLastTimeShareStreakShown(): Instant? =
settings
.getLongOrNull(ShareStreakCacheKeyValues.SHARE_STREAK_LAST_TIME_SHOWN)
?.let(Instant.Companion::fromEpochMilliseconds)

override fun setLastTimeShareStreakShown(epochMilliseconds: Long) {
settings.putLong(ShareStreakCacheKeyValues.SHARE_STREAK_LAST_TIME_SHOWN, epochMilliseconds)
override fun setLastTimeShareStreakShown(instant: Instant) {
settings.putLong(ShareStreakCacheKeyValues.SHARE_STREAK_LAST_TIME_SHOWN, instant.toEpochMilliseconds())
}

override fun clearCache() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.hyperskill.app.share_streak.data.repository

import kotlinx.datetime.Instant
import org.hyperskill.app.share_streak.data.source.ShareStreakCacheDataSource
import org.hyperskill.app.share_streak.domain.repository.ShareStreakRepository

internal class ShareStreakRepositoryImpl(
private val shareStreakCacheDataSource: ShareStreakCacheDataSource
) : ShareStreakRepository {
override fun getLastTimeShareStreakShownEpochMilliseconds(): Long? =
shareStreakCacheDataSource.getLastTimeShareStreakShownEpochMilliseconds()
override fun getLastTimeShareStreakShown(): Instant? =
shareStreakCacheDataSource.getLastTimeShareStreakShown()

override fun setLastTimeShareStreakShown(epochMilliseconds: Long) {
shareStreakCacheDataSource.setLastTimeShareStreakShown(epochMilliseconds)
override fun setLastTimeShareStreakShown(instant: Instant) {
shareStreakCacheDataSource.setLastTimeShareStreakShown(instant)
}

override fun clearCache() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.hyperskill.app.share_streak.data.source

import kotlinx.datetime.Instant

internal interface ShareStreakCacheDataSource {
fun getLastTimeShareStreakShownEpochMilliseconds(): Long?
fun setLastTimeShareStreakShown(epochMilliseconds: Long)
fun getLastTimeShareStreakShown(): Instant?
fun setLastTimeShareStreakShown(instant: Instant)
fun clearCache()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.hyperskill.app.share_streak.domain.interactor

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hyperskill.app.share_streak.domain.repository.ShareStreakRepository
Expand All @@ -13,8 +12,7 @@ class ShareStreakInteractor(private val shareStreakRepository: ShareStreakReposi
}

fun setLastTimeShareStreakShown() {
val nowEpochMilliseconds = Clock.System.now().toEpochMilliseconds()
shareStreakRepository.setLastTimeShareStreakShown(epochMilliseconds = nowEpochMilliseconds)
shareStreakRepository.setLastTimeShareStreakShown()
}

fun shouldShareStreakAfterStepSolved(streak: Int, streakState: StreakState): Boolean {
Expand All @@ -31,11 +29,8 @@ class ShareStreakInteractor(private val shareStreakRepository: ShareStreakReposi
}

private fun isUserSawShareStreakToday(): Boolean {
val lastTimeShownEpochMilliseconds =
shareStreakRepository.getLastTimeShareStreakShownEpochMilliseconds() ?: return false
val lastTimeShownLocalDateTime = Instant
.fromEpochMilliseconds(lastTimeShownEpochMilliseconds)
.toLocalDateTime(TimeZone.UTC)
val lastTimeShownLocalDateTime =
shareStreakRepository.getLastTimeShareStreakShown()?.toLocalDateTime(TimeZone.UTC) ?: return false

val nowLocalDateTime = Clock.System.now().toLocalDateTime(TimeZone.UTC)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.hyperskill.app.share_streak.domain.repository

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

interface ShareStreakRepository {
fun getLastTimeShareStreakShownEpochMilliseconds(): Long?
fun setLastTimeShareStreakShown(epochMilliseconds: Long)
fun getLastTimeShareStreakShown(): Instant?
fun setLastTimeShareStreakShown(instant: Instant = Clock.System.now())
fun clearCache()
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,27 @@ class StepCompletionActionDispatcher(
gemsEarned
)

val streakText = if (shouldShareStreak && streakToShare != null) {
val shareStreakData = if (shouldShareStreak && streakToShare != null) {
val daysText = resourceProvider.getQuantityString(
SharedResources.plurals.days,
streakToShare,
streakToShare
)
resourceProvider.getString(
SharedResources.strings.step_quiz_problem_of_day_solved_modal_streak_text,
daysText
StepCompletionFeature.ShareStreakData.Content(
streakText = resourceProvider.getString(
SharedResources.strings.step_quiz_problem_of_day_solved_modal_streak_text,
daysText
),
streak = streakToShare
)
} else {
null
StepCompletionFeature.ShareStreakData.Empty
}

onNewMessage(
Message.ProblemOfDaySolved(
earnedGemsText = earnedGemsText,
streakText = streakText,
streak = if (shouldShareStreak) streakToShare else null
shareStreakData = shareStreakData
)
)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ interface StepCompletionFeature {
object CheckTopicCompletion : ContinueButtonAction
}

/**
* Helper class to show streak text and streak value
*
* @see Message.ProblemOfDaySolved
* @see Action.ViewAction.ShowProblemOfDaySolvedModal
*/
sealed interface ShareStreakData {
object Empty : ShareStreakData
data class Content(val streakText: String, val streak: Int) : ShareStreakData
}

sealed interface Message {
object StartPracticingClicked : Message

Expand All @@ -57,7 +68,6 @@ interface StepCompletionFeature {
/**
* Topic completed modal
*/

sealed interface CheckTopicCompletionStatus : Message {
data class Completed(
val topicId: Long,
Expand All @@ -81,8 +91,7 @@ interface StepCompletionFeature {
*/
data class ProblemOfDaySolved(
val earnedGemsText: String,
val streakText: String?,
val streak: Int?
val shareStreakData: ShareStreakData
) : Message
object ProblemOfDaySolvedModalGoBackClicked : Message
data class ProblemOfDaySolvedModalShareStreakClicked(val streak: Int) : Message
Expand Down Expand Up @@ -134,8 +143,7 @@ interface StepCompletionFeature {

data class ShowProblemOfDaySolvedModal(
val earnedGemsText: String,
val streakText: String?,
val streak: Int?
val shareStreakData: ShareStreakData
) : ViewAction

data class ShowShareStreakModal(val streak: Int) : ViewAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ class StepCompletionReducer(private val stepRoute: StepRoute) : StateReducer<Sta
state to setOf(
Action.ViewAction.ShowProblemOfDaySolvedModal(
earnedGemsText = message.earnedGemsText,
streakText = message.streakText,
streak = message.streak
shareStreakData = message.shareStreakData
)
)
is Message.ProblemOfDaySolvedModalGoBackClicked -> {
Expand Down

0 comments on commit 4371388

Please sign in to comment.