Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface AuthenticationRepository {
suspend fun retrieveAccessToken(): String

suspend fun revokeSession()
suspend fun removeSessionData()

suspend fun getSessionState(): SessionState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ fun MainView(
onClick = onRetryLoadEntitiesClicked,
colors = ChipDefaults.primaryChipColors()
)
Spacer(modifier = Modifier.height(32.dp))
}
}
}
Expand Down Expand Up @@ -300,27 +301,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()
)
}
}
}
Expand Down