Merge pull request #2414 from Dominaezzz/suspend_functions_2

Convert PushRuleService to suspend functions
This commit is contained in:
Benoit Marty 2020-11-19 15:32:01 +01:00 committed by GitHub
commit d5f83acaf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 84 deletions

View File

@ -15,11 +15,9 @@
*/
package org.matrix.android.sdk.api.pushrules
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.pushrules.rest.PushRule
import org.matrix.android.sdk.api.pushrules.rest.RuleSet
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.util.Cancelable
interface PushRuleService {
/**
@ -29,13 +27,13 @@ interface PushRuleService {
fun getPushRules(scope: String = RuleScope.GLOBAL): RuleSet
fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable
suspend fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean)
fun addPushRule(kind: RuleKind, pushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable
suspend fun addPushRule(kind: RuleKind, pushRule: PushRule)
fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable
suspend fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule)
fun removePushRule(kind: RuleKind, pushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable
suspend fun removePushRule(kind: RuleKind, pushRule: PushRule)
fun addPushRuleListener(listener: PushRuleListener)

View File

@ -16,7 +16,6 @@
package org.matrix.android.sdk.internal.session.notification
import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.pushrules.PushRuleService
import org.matrix.android.sdk.api.pushrules.RuleKind
import org.matrix.android.sdk.api.pushrules.RuleSetKey
@ -24,7 +23,6 @@ import org.matrix.android.sdk.api.pushrules.getActions
import org.matrix.android.sdk.api.pushrules.rest.PushRule
import org.matrix.android.sdk.api.pushrules.rest.RuleSet
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.database.mapper.PushRulesMapper
import org.matrix.android.sdk.internal.database.model.PushRulesEntity
import org.matrix.android.sdk.internal.database.query.where
@ -103,37 +101,21 @@ internal class DefaultPushRuleService @Inject constructor(
)
}
override fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean) {
// The rules will be updated, and will come back from the next sync response
return updatePushRuleEnableStatusTask
.configureWith(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled)) {
this.callback = callback
}
.executeBy(taskExecutor)
updatePushRuleEnableStatusTask.execute(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled))
}
override fun addPushRule(kind: RuleKind, pushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable {
return addPushRuleTask
.configureWith(AddPushRuleTask.Params(kind, pushRule)) {
this.callback = callback
}
.executeBy(taskExecutor)
override suspend fun addPushRule(kind: RuleKind, pushRule: PushRule) {
addPushRuleTask.execute(AddPushRuleTask.Params(kind, pushRule))
}
override fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable {
return updatePushRuleActionsTask
.configureWith(UpdatePushRuleActionsTask.Params(kind, oldPushRule, newPushRule)) {
this.callback = callback
}
.executeBy(taskExecutor)
override suspend fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule) {
updatePushRuleActionsTask.execute(UpdatePushRuleActionsTask.Params(kind, oldPushRule, newPushRule))
}
override fun removePushRule(kind: RuleKind, pushRule: PushRule, callback: MatrixCallback<Unit>): Cancelable {
return removePushRuleTask
.configureWith(RemovePushRuleTask.Params(kind, pushRule)) {
this.callback = callback
}
.executeBy(taskExecutor)
override suspend fun removePushRule(kind: RuleKind, pushRule: PushRule) {
removePushRuleTask.execute(RemovePushRuleTask.Params(kind, pushRule))
}
override fun removePushRuleListener(listener: PushRuleService.PushRuleListener) {

View File

@ -15,12 +15,13 @@
*/
package im.vector.app.features.settings
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import im.vector.app.R
import im.vector.app.core.preference.PushRulePreference
import im.vector.app.core.preference.VectorPreference
import im.vector.app.core.utils.toast
import org.matrix.android.sdk.api.MatrixCallback
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.pushrules.RuleIds
import org.matrix.android.sdk.api.pushrules.rest.PushRuleAndKind
import javax.inject.Inject
@ -50,29 +51,25 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor()
if (newRule != null) {
displayLoadingView()
session.updatePushRuleActions(
ruleAndKind.kind,
lifecycleScope.launch {
val result = runCatching {
session.updatePushRuleActions(ruleAndKind.kind,
preference.ruleAndKind?.pushRule ?: ruleAndKind.pushRule,
newRule,
object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
if (!isAdded) {
return
newRule)
}
if (!isAdded) {
return@launch
}
hideLoadingView()
result.onSuccess {
preference.setPushRule(ruleAndKind.copy(pushRule = newRule))
hideLoadingView()
}
override fun onFailure(failure: Throwable) {
if (!isAdded) {
return
}
hideLoadingView()
result.onFailure { failure ->
// Restore the previous value
refreshDisplay()
activity?.toast(errorFormatter.toHumanReadable(failure))
}
})
}
}
false
}

View File

@ -23,6 +23,7 @@ import android.media.RingtoneManager
import android.net.Uri
import android.os.Parcelable
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import im.vector.app.R
@ -37,6 +38,7 @@ import im.vector.app.core.utils.isIgnoringBatteryOptimizations
import im.vector.app.core.utils.requestDisablingBatteryOptimization
import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.push.fcm.FcmHelper
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.pushrules.RuleIds
@ -318,24 +320,22 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
?.let {
// Trick, we must enable this room to disable notifications
lifecycleScope.launch {
try {
pushRuleService.updatePushRuleEnableStatus(RuleKind.OVERRIDE,
it,
!switchPref.isChecked,
object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
!switchPref.isChecked)
// Push rules will be updated from the sync
}
override fun onFailure(failure: Throwable) {
} catch (failure: Throwable) {
if (!isAdded) {
return
return@launch
}
// revert the check box
switchPref.isChecked = !switchPref.isChecked
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
}
})
}
}
}
}

View File

@ -20,7 +20,8 @@ import androidx.activity.result.ActivityResultLauncher
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.resources.StringProvider
import org.matrix.android.sdk.api.MatrixCallback
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.pushrules.RuleIds
import org.matrix.android.sdk.api.pushrules.RuleKind
import javax.inject.Inject
@ -48,16 +49,12 @@ class TestAccountSettings @Inject constructor(private val stringProvider: String
override fun doFix() {
if (manager?.diagStatus == TestStatus.RUNNING) return // wait before all is finished
session.updatePushRuleEnableStatus(RuleKind.OVERRIDE, defaultRule, !defaultRule.enabled,
object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
GlobalScope.launch {
runCatching {
session.updatePushRuleEnableStatus(RuleKind.OVERRIDE, defaultRule, !defaultRule.enabled)
}
manager?.retry(activityResultLauncher)
}
override fun onFailure(failure: Throwable) {
manager?.retry(activityResultLauncher)
}
})
}
}
status = TestStatus.FAILED