Skip to content

Commit aab6483

Browse files
committed
fix
1 parent 53c04d2 commit aab6483

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

composeApp/src/commonMain/kotlin/neth/iecal/trease/Constants.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Constants {
66
val preOwnedTrees = listOf("tree")
77

88
var default_quitter_text =
9-
"I am walking away from my promises. I admit my comfort is worth more than my future. I choose to be mediocre."
9+
"I am walking away from my promises."
1010
init {
1111
if (isDebugBuild) {
1212
default_quitter_text = ""

composeApp/src/commonMain/kotlin/neth/iecal/trease/models/FocusStats.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import kotlin.uuid.Uuid
77

88
@Serializable
99
data class FocusStats @OptIn(ExperimentalUuidApi::class) constructor(
10-
val duration:Long, // in seconds
10+
var duration:Long, // in seconds
1111
val treeId: String,
1212
val isFailed: Boolean,
1313
val failureTree: String = "weathered_0",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package neth.iecal.trease.utils
2+
3+
import kotlinx.serialization.encodeToString
4+
import kotlinx.serialization.json.Json
5+
import neth.iecal.trease.models.FocusStats
6+
7+
class FocusStateManager(){
8+
val cacheManager = CacheManager()
9+
10+
suspend fun setRunning(oversAt:Long) {
11+
cacheManager.saveFile("running_over_at",oversAt.toString())
12+
}
13+
suspend fun isRunning():Long? {
14+
return cacheManager.readFile("running_over_at")?.toLong()
15+
}
16+
17+
suspend fun setRunningTree(tree: FocusStats) {
18+
cacheManager.saveFile("running_for_tree",Json.encodeToString(tree))
19+
}
20+
suspend fun getRunningTree():FocusStats? {
21+
val rawTree = cacheManager.readFile("running_for_tree") ?: return null
22+
return Json.decodeFromString<FocusStats>(rawTree)
23+
}
24+
25+
26+
}

composeApp/src/commonMain/kotlin/neth/iecal/trease/viewmodels/HomeScreenViewModel.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import kotlinx.coroutines.flow.MutableStateFlow
1010
import kotlinx.coroutines.flow.StateFlow
1111
import kotlinx.coroutines.flow.asStateFlow
1212
import kotlinx.coroutines.launch
13+
import kotlinx.datetime.Clock
1314
import kotlinx.serialization.json.Json
1415
import neth.iecal.trease.models.FocusStats
1516
import neth.iecal.trease.models.TimerStatus
1617
import neth.iecal.trease.models.TreeData
1718
import neth.iecal.trease.onForceStopFocus
1819
import neth.iecal.trease.utils.CacheManager
1920
import neth.iecal.trease.utils.CoinManager
21+
import neth.iecal.trease.utils.FocusStateManager
2022
import neth.iecal.trease.utils.TreeStatsLodger
2123
import neth.iecal.trease.utils.randomBiased
2224
import kotlin.math.log2
@@ -65,10 +67,23 @@ class HomeScreenViewModel : ViewModel() {
6567
val currentTreeSeedVariant : StateFlow<Int> = _currentTreeSeedVariant.asStateFlow()
6668

6769
var treeList : MutableStateFlow<List<TreeData>> = MutableStateFlow(emptyList())
70+
71+
// caches the state if tree is growing or not along so if accidentally system kills the app, user can restart from last progress
72+
val focusStateManager = FocusStateManager()
6873
init {
6974
viewModelCoroutineScope.launch {
7075
coins.value = coinManager.reloadCoins()
7176
reloadTreeList()
77+
78+
val runDuration = focusStateManager.isRunning()
79+
if(runDuration!=null){
80+
val isNotOver = runDuration > Clock.System.now().epochSeconds
81+
if(isNotOver){
82+
val treeInfo = focusStateManager.getRunningTree()!!
83+
treeInfo.duration -= (runDuration - Clock.System.now().epochSeconds)
84+
selectWitheredTree(treeInfo)
85+
}
86+
}
7287
}
7388
}
7489
suspend fun reloadTreeList(){
@@ -136,6 +151,13 @@ class HomeScreenViewModel : ViewModel() {
136151
// We use the current remaining seconds as the "start line" for progress
137152
val initialSecondsAtStart = _remainingSeconds.value
138153
timerJob = viewModelScope.launch {
154+
focusStateManager.setRunning(Clock.System.now().epochSeconds + _remainingSeconds.value)
155+
focusStateManager.setRunningTree(FocusStats(
156+
selectedMinutes.value*60,
157+
selectedTree.value.id,
158+
false,
159+
treeSeed = currentTreeSeedVariant.value,
160+
))
139161
while (_remainingSeconds.value > 0) {
140162
delay(1000)
141163
_remainingSeconds.value -= 1
@@ -211,6 +233,7 @@ class HomeScreenViewModel : ViewModel() {
211233
)
212234
coins.value = coinManager.reloadCoins()
213235
}
236+
focusStateManager.setRunning(-1)
214237
}
215238

216239
}

0 commit comments

Comments
 (0)