Skip to content

Commit

Permalink
use vm to control update state
Browse files Browse the repository at this point in the history
  • Loading branch information
odaridavid committed Mar 5, 2024
1 parent b5f4e62 commit 7f70aae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ class MainViewModel @Inject constructor(
private val _state = MutableStateFlow(MainViewState())
val state: StateFlow<MainViewState> = _state.asStateFlow()

private val _hasAppUpdate = MutableStateFlow(false)
val hasAppUpdate: StateFlow<Boolean> = _hasAppUpdate.asStateFlow()

Check warning on line 25 in app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt#L24-L25

Added lines #L24 - L25 were not covered by tests

fun processIntent(mainViewIntent: MainViewIntent) {
when (mainViewIntent) {
is MainViewIntent.GrantPermission -> {
setState { copy(isPermissionGranted = mainViewIntent.isGranted) }
}

is MainViewIntent.CheckLocationSettings -> {
setState { copy(isLocationSettingEnabled = mainViewIntent.isEnabled) }
}

is MainViewIntent.ReceiveLocation -> {
val defaultLocation = DefaultLocation(
longitude = mainViewIntent.longitude,
Expand All @@ -39,8 +44,15 @@ class MainViewModel @Inject constructor(
}
setState { copy(defaultLocation = defaultLocation) }
}

is MainViewIntent.LogException -> {
logger.logException(mainViewIntent.throwable)
logger.logException(mainViewIntent.throwable)

Check warning on line 49 in app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt#L49

Added line #L49 was not covered by tests
}

is MainViewIntent.UpdateApp -> {
viewModelScope.launch {
_hasAppUpdate.emit(true)
}

Check warning on line 55 in app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt#L53-L55

Added lines #L53 - L55 were not covered by tests
}
}
}
Expand Down Expand Up @@ -69,4 +81,6 @@ sealed class MainViewIntent {

data class LogException(val throwable: Throwable) : MainViewIntent()

data object UpdateApp : MainViewIntent()

Check warning on line 84 in app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt#L84

Added line #L84 was not covered by tests

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.github.odaridavid.weatherapp.designsystem
import android.Manifest
import androidx.activity.result.ActivityResultLauncher
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
Expand Down Expand Up @@ -114,6 +113,22 @@ fun <T> SettingOptionsDialog(
}
}

@Composable
fun UpdateDialog(
onDismiss: () -> Unit,
onConfirm: () -> Unit,
) {
Dialog(onDismissRequest = { onDismiss() }) {
Box {
Text(text = stringResource(R.string.update_available))
Button(onClick = { onConfirm() }) {
Text(text = stringResource(R.string.install_update))
}
}
}

}

@Preview
@Composable
fun SettingOptionsDialogPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.github.odaridavid.weatherapp.common.createLocationRequest
import com.github.odaridavid.weatherapp.designsystem.EnableLocationSettingScreen
import com.github.odaridavid.weatherapp.designsystem.LoadingScreen
import com.github.odaridavid.weatherapp.designsystem.RequiresPermissionsScreen
import com.github.odaridavid.weatherapp.designsystem.UpdateDialog
import com.github.odaridavid.weatherapp.designsystem.theme.WeatherAppTheme
import com.github.odaridavid.weatherapp.ui.update.UpdateManager
import com.google.android.gms.location.FusedLocationProviderClient
Expand Down Expand Up @@ -55,7 +56,7 @@ class MainActivity : ComponentActivity() {
private val updateRequestLauncher =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
if (result.resultCode == RESULT_OK) {
// TODO Trigger a UI event
// TODO Trigger a UI event ,is this even necessary since we already have a listener?
Log.d("MainActivity", "Update successful")
} else {
Log.e("MainActivity", "Update failed")
Expand All @@ -66,7 +67,15 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

updateManager.checkForUpdates(activityResultLauncher = updateRequestLauncher)
updateManager.checkForUpdates(
activityResultLauncher = updateRequestLauncher,
onUpdateDownloaded = {
mainViewModel.processIntent(MainViewIntent.UpdateApp)
},
onUpdateFailure = { exception ->
mainViewModel.processIntent(MainViewIntent.LogException(throwable = exception))
}
)

createLocationRequest(
activity = this@MainActivity,
Expand All @@ -86,6 +95,20 @@ class MainActivity : ComponentActivity() {
) {
val state = mainViewModel.state.collectAsState().value

// TODO test this with internal testing track
mainViewModel.hasAppUpdate.collectAsState().value.let { hasAppUpdate ->
if (hasAppUpdate) {
UpdateDialog(
onDismiss = {
// TODO dismiss it
},
onConfirm = {
updateManager.completeUpdate()
}
)
}
}

CheckForPermissions(
onPermissionGranted = {
mainViewModel.processIntent(MainViewIntent.GrantPermission(isGranted = true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.odaridavid.weatherapp.ui.update
import android.content.Context
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import com.github.odaridavid.weatherapp.core.api.Logger
import com.google.android.play.core.appupdate.AppUpdateInfo
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
Expand All @@ -15,7 +14,6 @@ import javax.inject.Inject

class UpdateManager @Inject constructor(
@ApplicationContext private val context: Context,
private val logger: Logger,
private val updateStateFactory: UpdateStateFactory,

Check warning on line 17 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L15-L17

Added lines #L15 - L17 were not covered by tests
) {

Expand All @@ -24,16 +22,19 @@ class UpdateManager @Inject constructor(
}

fun checkForUpdates(
activityResultLauncher: ActivityResultLauncher<IntentSenderRequest>
activityResultLauncher: ActivityResultLauncher<IntentSenderRequest>,
onUpdateDownloaded: () -> Unit,
onUpdateFailure: (Throwable) -> Unit,
) {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo

Check warning on line 29 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L29

Added line #L29 was not covered by tests

appUpdateManager.registerListener(updateStateFactory.getUpdateStateListener(
onDownloaded = {
// TODO: Notify the user that the update is ready to be installed.Don't do it this way.
appUpdateManager.completeUpdate()
}
))
appUpdateManager.registerListener(
updateStateFactory.getUpdateStateListener(

Check warning on line 32 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L31-L32

Added lines #L31 - L32 were not covered by tests
onDownloaded = {
onUpdateDownloaded()
}

Check warning on line 35 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L34-L35

Added lines #L34 - L35 were not covered by tests
)
)

appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->

Check warning on line 39 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L39

Added line #L39 was not covered by tests
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
Expand All @@ -46,14 +47,18 @@ class UpdateManager @Inject constructor(
)
}
}.addOnFailureListener { exception ->
logger.logException(UpdateAppException(exception))
onUpdateFailure(UpdateAppException(exception))
}
}

Check warning on line 52 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L49-L52

Added lines #L49 - L52 were not covered by tests

fun unregisterListeners() {
appUpdateManager.unregisterListener(updateStateFactory.getUpdateStateListener())
}

Check warning on line 56 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L55-L56

Added lines #L55 - L56 were not covered by tests

fun completeUpdate() {
appUpdateManager.completeUpdate()
}

Check warning on line 60 in app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt#L59-L60

Added lines #L59 - L60 were not covered by tests

private fun update(
appUpdateManager: AppUpdateManager,
appUpdateInfo: AppUpdateInfo,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@
<string name="error_server">Oops! Something is wrong on our end :(</string>
<string name="error_generic">Something is happening that\'s disturbing the force :(</string>
<string name="error_connection">Check your internet connection and try again</string>
<string name="update_available">Update is available for install</string>
<string name="install_update">Install</string>
</resources>

0 comments on commit 7f70aae

Please sign in to comment.