Convert PushersService to suspend functions

Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
Dominic Fischer 2021-03-27 20:44:07 +00:00
parent 2045a164c1
commit 7fbe485603
4 changed files with 31 additions and 48 deletions

View file

@ -16,8 +16,6 @@
package org.matrix.android.sdk.api.session.pushers
import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
import java.util.UUID
interface PushersService {
@ -75,16 +73,15 @@ interface PushersService {
* @param callback callback to know if the push gateway has accepted the request. In this case, the app should receive a Push with the provided eventId.
* In case of error, PusherRejected failure can happen. In this case it means that the pushkey is not valid.
*/
fun testPush(url: String,
appId: String,
pushkey: String,
eventId: String,
callback: MatrixCallback<Unit>): Cancelable
suspend fun testPush(url: String,
appId: String,
pushkey: String,
eventId: String)
/**
* Remove the http pusher
*/
fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback<Unit>): Cancelable
suspend fun removeHttpPusher(pushkey: String, appId: String)
/**
* Get the current pushers, as a LiveData

View file

@ -18,10 +18,8 @@ package org.matrix.android.sdk.internal.session.pushers
import androidx.lifecycle.LiveData
import androidx.work.BackoffPolicy
import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.pushers.Pusher
import org.matrix.android.sdk.api.session.pushers.PushersService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.database.mapper.asDomain
import org.matrix.android.sdk.internal.database.model.PusherEntity
import org.matrix.android.sdk.internal.database.query.where
@ -47,16 +45,11 @@ internal class DefaultPushersService @Inject constructor(
private val taskExecutor: TaskExecutor
) : PushersService {
override fun testPush(url: String,
appId: String,
pushkey: String,
eventId: String,
callback: MatrixCallback<Unit>): Cancelable {
return pushGatewayNotifyTask
.configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) {
this.callback = callback
}
.executeBy(taskExecutor)
override suspend fun testPush(url: String,
appId: String,
pushkey: String,
eventId: String) {
pushGatewayNotifyTask.execute(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId))
}
override fun refreshPushers() {
@ -102,14 +95,9 @@ internal class DefaultPushersService @Inject constructor(
return request.id
}
override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun removeHttpPusher(pushkey: String, appId: String) {
val params = RemovePusherTask.Params(pushkey, appId)
return removePusherTask
.configureWith(params) {
this.callback = callback
}
// .enableRetry() ??
.executeBy(taskExecutor)
removePusherTask.execute(params)
}
override fun getPushersLive(): LiveData<List<Pusher>> {

View file

@ -35,15 +35,14 @@ class PushersManager @Inject constructor(
private val stringProvider: StringProvider,
private val appNameProvider: AppNameProvider
) {
fun testPush(pushKey: String, callback: MatrixCallback<Unit>): Cancelable {
suspend fun testPush(pushKey: String) {
val currentSession = activeSessionHolder.getActiveSession()
return currentSession.testPush(
currentSession.testPush(
stringProvider.getString(R.string.pusher_http_url),
stringProvider.getString(R.string.pusher_app_id),
pushKey,
TEST_EVENT_ID,
callback
TEST_EVENT_ID
)
}
@ -64,9 +63,9 @@ class PushersManager @Inject constructor(
)
}
fun unregisterPusher(pushKey: String, callback: MatrixCallback<Unit>) {
suspend fun unregisterPusher(pushKey: String) {
val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id), callback)
currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id))
}
companion object {

View file

@ -39,7 +39,6 @@ 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
import org.matrix.android.sdk.api.pushrules.RuleKind
@ -295,20 +294,20 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
}
} else {
FcmHelper.getFcmToken(requireContext())?.let {
pushManager.unregisterPusher(it, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
session.refreshPushers()
}
override fun onFailure(failure: Throwable) {
if (!isAdded) {
return
}
// revert the check box
switchPref.isChecked = !switchPref.isChecked
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
}
})
lifecycleScope.launch {
runCatching { pushManager.unregisterPusher(it) }
.fold(
{ session.refreshPushers() },
{
if (!isAdded) {
return@fold
}
// revert the check box
switchPref.isChecked = !switchPref.isChecked
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
}
)
}
}
}
}