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

Convert PushersService to suspend functions #3077

Merged
merged 3 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
)
}
}
}
}
Expand Down