Skip to content

Commit 772c79c

Browse files
committed
πŸ› fix update crash
1 parent cb165ac commit 772c79c

5 files changed

Lines changed: 45 additions & 8 deletions

File tree

β€Žsrc/main/kotlin/com/theapache64/stackzy/App.ktβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fun main() {
4040
val appArgs = AppArgs(
4141
appName = "Stackzy",
4242
version = "v1.2.2",
43-
versionCode = 20220124
43+
versionCode = 20210724
4444
)
4545

4646
// Passing args

β€Žsrc/main/kotlin/com/theapache64/stackzy/ui/feature/splash/SplashScreen.ktβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize
66
import androidx.compose.foundation.layout.padding
77
import androidx.compose.foundation.layout.size
88
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.LaunchedEffect
910
import androidx.compose.runtime.collectAsState
1011
import androidx.compose.runtime.getValue
1112
import androidx.compose.ui.Alignment
@@ -29,11 +30,12 @@ fun SplashScreen(
2930
val isSyncFinished by splashViewModel.isSyncFinished.collectAsState()
3031
val syncFailedReason by splashViewModel.syncFailedMsg.collectAsState()
3132
val syncMessage by splashViewModel.syncMsg.collectAsState()
32-
val shouldUpdate by splashViewModel.shouldUpdate.collectAsState()
33+
val shouldUpdate by splashViewModel.shouldUpdate.collectAsState(initial = false)
3334

34-
if (shouldUpdate) {
35-
onUpdateNeeded()
36-
return
35+
LaunchedEffect(shouldUpdate) {
36+
if (shouldUpdate) {
37+
onUpdateNeeded()
38+
}
3739
}
3840

3941
if (isSyncFinished) {

β€Žsrc/main/kotlin/com/theapache64/stackzy/ui/feature/splash/SplashViewModel.ktβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import com.theapache64.stackzy.data.repo.ConfigRepo
77
import com.theapache64.stackzy.data.repo.FunFactsRepo
88
import com.theapache64.stackzy.data.repo.LibrariesRepo
99
import com.theapache64.stackzy.data.util.calladapter.flow.Resource
10+
import com.theapache64.stackzy.util.flow.mutableEventFlow
1011
import com.toxicbakery.logging.Arbor
1112
import kotlinx.coroutines.CoroutineScope
1213
import kotlinx.coroutines.flow.MutableStateFlow
14+
import kotlinx.coroutines.flow.SharedFlow
1315
import kotlinx.coroutines.flow.StateFlow
1416
import kotlinx.coroutines.launch
1517
import kotlinx.coroutines.flow.collect
@@ -150,13 +152,13 @@ class SplashViewModel @Inject constructor(
150152
}
151153

152154

153-
private val _shouldUpdate = MutableStateFlow(false)
154-
val shouldUpdate: StateFlow<Boolean> = _shouldUpdate
155+
private val _shouldUpdate = mutableEventFlow<Boolean>()
156+
val shouldUpdate: SharedFlow<Boolean> = _shouldUpdate
155157

156158
private fun verifyConfig(config: Config): Boolean {
157159
val isUpdateNeeded = App.appArgs.versionCode < config.mandatoryVersionCode // currentVersion < mandatoryVersion
158160
return if (isUpdateNeeded) {
159-
_shouldUpdate.value = true
161+
_shouldUpdate.tryEmit(true)
160162
false
161163
} else {
162164
true

β€Žsrc/main/kotlin/com/theapache64/stackzy/ui/navigation/NavHostComponent.ktβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class NavHostComponent(
257257
* Invoked when an update is necessary
258258
*/
259259
private fun onUpdateNeeded() {
260+
println("Update needed")
260261
router.push(Config.Update)
261262
}
262263

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2021 Boil (https://github.com/theapache64/boil)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.theapache64.stackzy.util.flow
17+
18+
import kotlinx.coroutines.flow.MutableSharedFlow
19+
20+
/**
21+
* To fire events.
22+
* This flow won't fire the last value for each collect call.
23+
* This observer will only be invoked on `tryEmit` calls.
24+
* (replacement for SingleLiveEvent :D)
25+
* Created by theapache64 : Jan 08 Fri,2021 @ 01:40
26+
*/
27+
fun <T> mutableEventFlow(): MutableSharedFlow<T> {
28+
return MutableSharedFlow(
29+
replay = 0,
30+
extraBufferCapacity = 1
31+
)
32+
}

0 commit comments

Comments
Β (0)