Skip to content

Commit

Permalink
Merge pull request #3077 from Dominaezzz/suspend_functions_8
Browse files Browse the repository at this point in the history
Convert PushersService to suspend functions
  • Loading branch information
bmarty authored Mar 31, 2021
2 parents 67c2b58 + 6226938 commit f2f735a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 67 deletions.
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 @@ -24,9 +24,8 @@ import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.settings.troubleshoot.TroubleshootTest
import im.vector.app.push.fcm.FcmHelper
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.pushers.PushGatewayFailure
import org.matrix.android.sdk.api.util.Cancelable
import kotlinx.coroutines.*
import javax.inject.Inject

/**
Expand All @@ -38,29 +37,31 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat
private val pushersManager: PushersManager)
: TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) {

private var action: Cancelable? = null
private var action: Job? = null

override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
val fcmToken = FcmHelper.getFcmToken(context) ?: run {
status = TestStatus.FAILED
return
}
action = pushersManager.testPush(fcmToken, object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) {
description = if (failure is PushGatewayFailure.PusherRejected) {
stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed)
} else {
errorFormatter.toHumanReadable(failure)
}
status = TestStatus.FAILED
}

override fun onSuccess(data: Unit) {
// Wait for the push to be received
description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push)
status = TestStatus.RUNNING
}
})
action = GlobalScope.launch {
runCatching { pushersManager.testPush(fcmToken) }
.fold(
{
// Wait for the push to be received
description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push)
status = TestStatus.RUNNING
},
{
description = if (it is PushGatewayFailure.PusherRejected) {
stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed)
} else {
errorFormatter.toHumanReadable(it)
}
status = TestStatus.FAILED
}
)
}
}

override fun onPushReceived() {
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

0 comments on commit f2f735a

Please sign in to comment.