Skip to content

Commit

Permalink
◀️ resolve #42
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Apr 4, 2021
1 parent 9851e68 commit 5f06eb4
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "com.theapache64"
version = "1.0.0-rc1" // TODO : Change in App.kt also
version = "1.0.0-rc2" // TODO : Change in App.kt also

repositories {
// mavenLocal()
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/theapache64/stackzy/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fun main() {
//TODO: This should be read from properties file generated by build-process/gradle
val appArgs = AppArgs(
appName = "Stackzy",
version = "v1.0.0-rc1",
version = "v1.0.0-rc2",
versionCode = 20210404
)

// Passing args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.theapache64.stackzy.data.local

data class AppArgs(
val appName: String,
val version: String
val version: String,
val versionCode: Int
)
2 changes: 2 additions & 0 deletions src/main/kotlin/com/theapache64/stackzy/data/remote/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ data class Config(
val shouldConsiderResultCache: Boolean,
@Json(name = "latest_stackzy_lib_version")
val latestStackzyLibVersion: Int,
@Json(name = "mandatory_version_code")
val mandatoryVersionCode: Int,
)
3 changes: 2 additions & 1 deletion src/main/kotlin/com/theapache64/stackzy/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.theapache64.stackzy.ui.feature.pathway.PathwayScreenComponent
import com.theapache64.stackzy.ui.feature.selectapp.SelectAppScreenComponent
import com.theapache64.stackzy.ui.feature.selectdevice.SelectDeviceScreenComponent
import com.theapache64.stackzy.ui.feature.splash.SplashScreenComponent
import com.theapache64.stackzy.ui.feature.update.UpdateScreenComponent
import dagger.Component
import javax.inject.Singleton

Expand All @@ -30,7 +31,7 @@ interface AppComponent {
fun inject(selectAppScreenComponent: SelectAppScreenComponent)
fun inject(appDetailScreenComponent: AppDetailScreenComponent)
fun inject(selectDeviceScreenComponent: SelectDeviceScreenComponent)

fun inject(updateScreenComponent: UpdateScreenComponent)
// bind repo to this component
fun bind(): AdbRepo
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ import com.theapache64.stackzy.ui.common.Logo
@Composable
fun SplashScreen(
splashViewModel: SplashViewModel,
onSyncFinished: () -> Unit
onSyncFinished: () -> Unit,
onUpdateNeeded: () -> Unit
) {

val isSyncFinished by splashViewModel.isSyncFinished.collectAsState()
val syncFailedReason by splashViewModel.syncFailedMsg.collectAsState()
val syncMessage by splashViewModel.syncMsg.collectAsState()
val shouldUpdate by splashViewModel.shouldUpdate.collectAsState()

if (shouldUpdate) {
onUpdateNeeded()
return
}

if (isSyncFinished) {
onSyncFinished()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import javax.inject.Inject
class SplashScreenComponent(
appComponent: AppComponent,
private val componentContext: ComponentContext,
private val onSyncFinished: () -> Unit
private val onSyncFinished: () -> Unit,
private val onUpdateNeeded: () -> Unit,
) : Component, ComponentContext by componentContext {

@Inject
Expand All @@ -38,7 +39,8 @@ class SplashScreenComponent(

SplashScreen(
splashViewModel = splashViewModel,
onSyncFinished = onSyncFinished
onSyncFinished = onSyncFinished,
onUpdateNeeded = onUpdateNeeded
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.theapache64.stackzy.ui.feature.splash

import com.theapache64.stackzy.App
import com.theapache64.stackzy.data.remote.Config
import com.theapache64.stackzy.data.repo.AdbRepo
import com.theapache64.stackzy.data.repo.ConfigRepo
import com.theapache64.stackzy.data.repo.LibrariesRepo
Expand Down Expand Up @@ -98,8 +100,12 @@ class SplashViewModel @Inject constructor(
_syncMsg.value = "Syncing config..."
}
is Resource.Success -> {
configRepo.saveConfigToLocal(it.data)
onSuccess()
val config = it.data
val isConfigVerified = verifyConfig(config)
if (isConfigVerified) {
configRepo.saveConfigToLocal(config)
onSuccess()
}
}
is Resource.Error -> {
_syncFailedMsg.value = it.errorData
Expand All @@ -108,6 +114,20 @@ class SplashViewModel @Inject constructor(
}
}


private val _shouldUpdate = MutableStateFlow(false)
val shouldUpdate: StateFlow<Boolean> = _shouldUpdate

private fun verifyConfig(config: Config): Boolean {
val isUpdateNeeded = App.appArgs.versionCode < config.mandatoryVersionCode // currentVersion < mandatoryVersion
return if (isUpdateNeeded) {
_shouldUpdate.value = true
false
} else {
true
}
}

private suspend fun checkAndFixAdb(
onSuccess: suspend () -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.theapache64.stackzy.ui.feature.update

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import com.theapache64.stackzy.App
import com.theapache64.stackzy.ui.common.FullScreenError

@Composable
fun UpdateScreen(
viewModel: UpdateScreenViewModel
) {
FullScreenError(
title = "Update",
message = "Looks like you're using an older version of ${App.appArgs.appName}." +
"Please download the latest version and update. Thank you :)",
action = {
Button(
onClick = {
viewModel.onUpdateClicked()
}
) {
Text(text = "UPDATE")
}
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.theapache64.stackzy.ui.feature.update

import androidx.compose.runtime.Composable
import com.arkivanov.decompose.ComponentContext
import com.theapache64.stackzy.di.AppComponent
import com.theapache64.stackzy.ui.navigation.Component
import javax.inject.Inject

class UpdateScreenComponent(
appComponent: AppComponent,
private val componentContext: ComponentContext
) : Component, ComponentContext by componentContext {

@Inject
lateinit var viewModel: UpdateScreenViewModel

init {
appComponent.inject(this)
}

@Composable
override fun render() {
UpdateScreen(
viewModel
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.theapache64.stackzy.ui.feature.update

import java.awt.Desktop
import java.net.URI
import javax.inject.Inject

class UpdateScreenViewModel @Inject constructor(

) {
companion object {
private const val LATEST_VERSION_URL = "https://github.com/theapache64/stackzy/releases/latest"
}

fun onUpdateClicked() {
Desktop.getDesktop().browse(URI(LATEST_VERSION_URL))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.theapache64.stackzy.ui.feature.pathway.PathwayScreenComponent
import com.theapache64.stackzy.ui.feature.selectapp.SelectAppScreenComponent
import com.theapache64.stackzy.ui.feature.selectdevice.SelectDeviceScreenComponent
import com.theapache64.stackzy.ui.feature.splash.SplashScreenComponent
import com.theapache64.stackzy.ui.feature.update.UpdateScreenComponent
import com.theapache64.stackzy.util.ApkSource
import com.toxicbakery.logging.Arbor
import java.awt.Desktop
Expand Down Expand Up @@ -45,6 +46,8 @@ class NavHostComponent(
val apkSource: ApkSource<AndroidDevice, Account>,
val androidApp: AndroidApp
) : Config()

object Update : Config()
}

private val appComponent: AppComponent = DaggerAppComponent
Expand All @@ -66,7 +69,8 @@ class NavHostComponent(
is Config.Splash -> SplashScreenComponent(
appComponent = appComponent,
componentContext = componentContext,
onSyncFinished = ::onSplashSyncFinished
onSyncFinished = ::onSplashSyncFinished,
onUpdateNeeded = ::onUpdateNeeded
)
is Config.SelectPathway -> PathwayScreenComponent(
appComponent = appComponent,
Expand Down Expand Up @@ -104,6 +108,11 @@ class NavHostComponent(
onLibrarySelected = ::onLibrarySelected,
onBackClicked = ::onBackClicked
)

is Config.Update -> UpdateScreenComponent(
appComponent = appComponent,
componentContext = componentContext
)
}
}

Expand Down Expand Up @@ -198,6 +207,13 @@ class NavHostComponent(
Desktop.getDesktop().browse(URI(library.website))
}

/**
* Invoked when an update is necessary
*/
private fun onUpdateNeeded() {
router.push(Config.Update)
}

/**
* Invoked when back arrow pressed
*/
Expand Down

0 comments on commit 5f06eb4

Please sign in to comment.