Prevent crashes on Wear when offline / server no longer available (#2825)

- Catch exceptions thrown when executing calls to change an entity state instead of crashing the app
 - When revoking a session fails continue with logout as the server might no longer be available and the user otherwise might be 'locked in' with no way to change the server
This commit is contained in:
Joris Pelgröm 2022-08-30 02:53:52 +02:00 committed by GitHub
parent 6e647cb436
commit 3908a07c1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 47 deletions

View file

@ -19,6 +19,7 @@ interface AuthenticationRepository {
suspend fun retrieveAccessToken(): String
suspend fun revokeSession()
suspend fun removeSessionData()
suspend fun getSessionState(): SessionState

View file

@ -135,6 +135,10 @@ class AuthenticationRepositoryImpl @Inject constructor(
session.refreshToken,
AuthenticationService.REVOKE_ACTION
)
removeSessionData()
}
override suspend fun removeSessionData() {
saveSession(null)
urlRepository.saveUrl("", true)
urlRepository.saveUrl("", false)

View file

@ -95,44 +95,60 @@ class HomePresenterImpl @Inject constructor(
in toggleDomains -> "toggle"
else -> "turn_on"
}
integrationUseCase.callService(
domain,
serviceName,
hashMapOf("entity_id" to entityId)
)
try {
integrationUseCase.callService(
domain,
serviceName,
hashMapOf("entity_id" to entityId)
)
} catch (e: Exception) {
Log.e(TAG, "Exception when toggling entity", e)
}
}
override suspend fun onFanSpeedChanged(entityId: String, speed: Float) {
integrationUseCase.callService(
entityId.split(".")[0],
"set_percentage",
hashMapOf(
"entity_id" to entityId,
"percentage" to speed.toInt()
try {
integrationUseCase.callService(
entityId.split(".")[0],
"set_percentage",
hashMapOf(
"entity_id" to entityId,
"percentage" to speed.toInt()
)
)
)
} catch (e: Exception) {
Log.e(TAG, "Exception when setting fan speed", e)
}
}
override suspend fun onBrightnessChanged(entityId: String, brightness: Float) {
integrationUseCase.callService(
entityId.split(".")[0],
"turn_on",
hashMapOf(
"entity_id" to entityId,
"brightness" to brightness.toInt()
try {
integrationUseCase.callService(
entityId.split(".")[0],
"turn_on",
hashMapOf(
"entity_id" to entityId,
"brightness" to brightness.toInt()
)
)
)
} catch (e: Exception) {
Log.e(TAG, "Exception when setting light brightness", e)
}
}
override suspend fun onColorTempChanged(entityId: String, colorTemp: Float) {
integrationUseCase.callService(
entityId.split(".")[0],
"turn_on",
hashMapOf(
"entity_id" to entityId,
"color_temp" to colorTemp.toInt()
try {
integrationUseCase.callService(
entityId.split(".")[0],
"turn_on",
hashMapOf(
"entity_id" to entityId,
"color_temp" to colorTemp.toInt()
)
)
)
} catch (e: Exception) {
Log.e(TAG, "Exception when setting light color temp", e)
}
}
override fun onInvalidAuthorization() = finishSession()
@ -141,7 +157,13 @@ class HomePresenterImpl @Inject constructor(
private fun finishSession() {
mainScope.launch {
authenticationUseCase.revokeSession()
try {
authenticationUseCase.revokeSession()
} catch (e: Exception) {
Log.e(TAG, "Exception while revoking session", e)
// Remove local data anyway, the user wants to sign out and we don't need the server for that
authenticationUseCase.removeSessionData()
}
view.displayOnBoarding()
}
}

View file

@ -158,6 +158,7 @@ fun MainView(
onClick = onRetryLoadEntitiesClicked,
colors = ChipDefaults.primaryChipColors()
)
Spacer(modifier = Modifier.height(32.dp))
}
}
}
@ -295,27 +296,29 @@ fun MainView(
)
}
}
}
}
// Settings
item {
Chip(
modifier = Modifier
.fillMaxWidth(),
icon = {
Image(
asset = CommunityMaterial.Icon.cmd_cog,
colorFilter = ColorFilter.tint(Color.White)
)
},
label = {
Text(
text = stringResource(id = commonR.string.settings)
)
},
onClick = onSettingsClicked,
colors = ChipDefaults.secondaryChipColors()
)
}
if (mainViewModel.loadingState.value != MainViewModel.LoadingState.LOADING) {
// Settings
item {
Chip(
modifier = Modifier
.fillMaxWidth(),
icon = {
Image(
asset = CommunityMaterial.Icon.cmd_cog,
colorFilter = ColorFilter.tint(Color.White)
)
},
label = {
Text(
text = stringResource(id = commonR.string.settings)
)
},
onClick = onSettingsClicked,
colors = ChipDefaults.secondaryChipColors()
)
}
}
}