diff --git a/app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt b/app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt index f712365..d3d49fd 100644 --- a/app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt +++ b/app/src/main/java/com/github/odaridavid/weatherapp/MainViewModel.kt @@ -21,14 +21,19 @@ class MainViewModel @Inject constructor( private val _state = MutableStateFlow(MainViewState()) val state: StateFlow = _state.asStateFlow() + private val _hasAppUpdate = MutableStateFlow(false) + val hasAppUpdate: StateFlow = _hasAppUpdate.asStateFlow() + 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, @@ -39,8 +44,15 @@ class MainViewModel @Inject constructor( } setState { copy(defaultLocation = defaultLocation) } } + is MainViewIntent.LogException -> { - logger.logException(mainViewIntent.throwable) + logger.logException(mainViewIntent.throwable) + } + + is MainViewIntent.UpdateApp -> { + viewModelScope.launch { + _hasAppUpdate.emit(true) + } } } } @@ -69,4 +81,6 @@ sealed class MainViewIntent { data class LogException(val throwable: Throwable) : MainViewIntent() + data object UpdateApp : MainViewIntent() + } diff --git a/app/src/main/java/com/github/odaridavid/weatherapp/designsystem/Dialogs.kt b/app/src/main/java/com/github/odaridavid/weatherapp/designsystem/Dialogs.kt index 90211c8..b0817aa 100644 --- a/app/src/main/java/com/github/odaridavid/weatherapp/designsystem/Dialogs.kt +++ b/app/src/main/java/com/github/odaridavid/weatherapp/designsystem/Dialogs.kt @@ -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 @@ -114,6 +113,22 @@ fun 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() { diff --git a/app/src/main/java/com/github/odaridavid/weatherapp/ui/MainActivity.kt b/app/src/main/java/com/github/odaridavid/weatherapp/ui/MainActivity.kt index dcac27f..6901c9c 100644 --- a/app/src/main/java/com/github/odaridavid/weatherapp/ui/MainActivity.kt +++ b/app/src/main/java/com/github/odaridavid/weatherapp/ui/MainActivity.kt @@ -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 @@ -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") @@ -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, @@ -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)) diff --git a/app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt b/app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt index 4ffc0c9..0e18c64 100644 --- a/app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt +++ b/app/src/main/java/com/github/odaridavid/weatherapp/ui/update/UpdateManager.kt @@ -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 @@ -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, ) { @@ -24,16 +22,19 @@ class UpdateManager @Inject constructor( } fun checkForUpdates( - activityResultLauncher: ActivityResultLauncher + activityResultLauncher: ActivityResultLauncher, + onUpdateDownloaded: () -> Unit, + onUpdateFailure: (Throwable) -> Unit, ) { val appUpdateInfoTask = appUpdateManager.appUpdateInfo - 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( + onDownloaded = { + onUpdateDownloaded() + } + ) + ) appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE @@ -46,7 +47,7 @@ class UpdateManager @Inject constructor( ) } }.addOnFailureListener { exception -> - logger.logException(UpdateAppException(exception)) + onUpdateFailure(UpdateAppException(exception)) } } @@ -54,6 +55,10 @@ class UpdateManager @Inject constructor( appUpdateManager.unregisterListener(updateStateFactory.getUpdateStateListener()) } + fun completeUpdate() { + appUpdateManager.completeUpdate() + } + private fun update( appUpdateManager: AppUpdateManager, appUpdateInfo: AppUpdateInfo, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f33a25..731b3f0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -33,4 +33,6 @@ Oops! Something is wrong on our end :( Something is happening that\'s disturbing the force :( Check your internet connection and try again + Update is available for install + Install