Cleanup and cancel request properly

This commit is contained in:
Benoit Marty 2020-10-08 21:40:53 +02:00 committed by Benoit Marty
parent dbb77d9dc1
commit ab9bbe9a48
6 changed files with 41 additions and 34 deletions

View File

@ -79,7 +79,7 @@ interface PushersService {
appId: String, appId: String,
pushkey: String, pushkey: String,
eventId: String, eventId: String,
callback: MatrixCallback<Unit>) callback: MatrixCallback<Unit>): Cancelable
/** /**
* Remove the http pusher * Remove the http pusher

View File

@ -51,8 +51,8 @@ internal class DefaultPushersService @Inject constructor(
appId: String, appId: String,
pushkey: String, pushkey: String,
eventId: String, eventId: String,
callback: MatrixCallback<Unit>) { callback: MatrixCallback<Unit>): Cancelable {
pushGatewayNotifyTask return pushGatewayNotifyTask
.configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) { .configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) {
this.callback = callback this.callback = callback
} }

View File

@ -26,6 +26,7 @@ import im.vector.app.features.settings.troubleshoot.TroubleshootTest
import im.vector.app.push.fcm.FcmHelper import im.vector.app.push.fcm.FcmHelper
import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.pushers.PushGatewayFailure import org.matrix.android.sdk.api.session.pushers.PushGatewayFailure
import org.matrix.android.sdk.api.util.Cancelable
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -37,12 +38,14 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat
private val pushersManager: PushersManager) private val pushersManager: PushersManager)
: TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) { : TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) {
private var action: Cancelable? = null
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) { override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
val fcmToken = FcmHelper.getFcmToken(context) ?: run { val fcmToken = FcmHelper.getFcmToken(context) ?: run {
status = TestStatus.FAILED status = TestStatus.FAILED
return return
} }
pushersManager.testPush(fcmToken, object : MatrixCallback<Unit> { action = pushersManager.testPush(fcmToken, object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
description = if (failure is PushGatewayFailure.PusherRejected) { description = if (failure is PushGatewayFailure.PusherRejected) {
stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed) stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed)
@ -64,4 +67,8 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat
description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_notification_clicked) description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_notification_clicked)
status = TestStatus.SUCCESS status = TestStatus.SUCCESS
} }
override fun cancel() {
action?.cancel()
}
} }

View File

@ -22,6 +22,7 @@ import im.vector.app.core.resources.AppNameProvider
import im.vector.app.core.resources.LocaleProvider import im.vector.app.core.resources.LocaleProvider
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
import java.util.UUID import java.util.UUID
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
@ -34,10 +35,10 @@ class PushersManager @Inject constructor(
private val stringProvider: StringProvider, private val stringProvider: StringProvider,
private val appNameProvider: AppNameProvider private val appNameProvider: AppNameProvider
) { ) {
fun testPush(pushKey: String, callback: MatrixCallback<Unit>) { fun testPush(pushKey: String, callback: MatrixCallback<Unit>): Cancelable {
val currentSession = activeSessionHolder.getActiveSession() val currentSession = activeSessionHolder.getActiveSession()
currentSession.testPush( return currentSession.testPush(
stringProvider.getString(R.string.pusher_http_url), stringProvider.getString(R.string.pusher_http_url),
stringProvider.getString(R.string.pusher_app_id), stringProvider.getString(R.string.pusher_app_id),
pushKey, pushKey,

View File

@ -41,6 +41,7 @@ import im.vector.app.features.rageshake.BugReporter
import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager
import im.vector.app.features.settings.troubleshoot.TroubleshootTest import im.vector.app.features.settings.troubleshoot.TroubleshootTest
import im.vector.app.push.fcm.NotificationTroubleshootTestManagerFactory import im.vector.app.push.fcm.NotificationTroubleshootTestManagerFactory
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import javax.inject.Inject import javax.inject.Inject
@ -92,8 +93,7 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor(
} }
private fun startUI() { private fun startUI() {
mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_running_status, mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_running_status, 0, 0)
0, 0)
testManager = testManagerFactory.create(this) testManager = testManagerFactory.create(this)
testManager?.statusListener = { troubleshootTestManager -> testManager?.statusListener = { troubleshootTestManager ->
if (isAdded) { if (isAdded) {
@ -105,9 +105,8 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor(
mRunButton.visibility = View.VISIBLE mRunButton.visibility = View.VISIBLE
} }
TroubleshootTest.TestStatus.RUNNING -> { TroubleshootTest.TestStatus.RUNNING -> {
// Forces int type because it's breaking lint val size = troubleshootTestManager.testListSize
val size: Int = troubleshootTestManager.testList.size val currentTestIndex = troubleshootTestManager.currentTestIndex
val currentTestIndex: Int = troubleshootTestManager.currentTestIndex
mSummaryDescription.text = getString( mSummaryDescription.text = getString(
R.string.settings_troubleshoot_diagnostic_running_status, R.string.settings_troubleshoot_diagnostic_running_status,
currentTestIndex, currentTestIndex,
@ -118,16 +117,7 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor(
} }
TroubleshootTest.TestStatus.FAILED -> { TroubleshootTest.TestStatus.FAILED -> {
// check if there are quick fixes // check if there are quick fixes
// TODO Rewrite using firstOrNull val hasQuickFix = testManager?.hasQuickFix().orFalse()
var hasQuickFix = false
testManager?.testList?.let {
for (test in it) {
if (test.status == TroubleshootTest.TestStatus.FAILED && test.quickFix != null) {
hasQuickFix = true
break
}
}
}
if (hasQuickFix) { if (hasQuickFix) {
mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_failure_status_with_quickfix) mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_failure_status_with_quickfix)
} else { } else {

View File

@ -23,13 +23,19 @@ import androidx.fragment.app.Fragment
import kotlin.properties.Delegates import kotlin.properties.Delegates
class NotificationTroubleshootTestManager(val fragment: Fragment) { class NotificationTroubleshootTestManager(val fragment: Fragment) {
private val testList = ArrayList<TroubleshootTest>()
val testListSize: Int
get() = testList.size
val testList = ArrayList<TroubleshootTest>()
var isCancelled = false var isCancelled = false
private set
var currentTestIndex by Delegates.observable(0) { _, _, _ -> var currentTestIndex by Delegates.observable(0) { _, _, _ ->
statusListener?.invoke(this) statusListener?.invoke(this)
} }
private set
val adapter = NotificationTroubleshootRecyclerViewAdapter(testList) val adapter = NotificationTroubleshootRecyclerViewAdapter(testList)
var statusListener: ((NotificationTroubleshootTestManager) -> Unit)? = null var statusListener: ((NotificationTroubleshootTestManager) -> Unit)? = null
@ -37,6 +43,7 @@ class NotificationTroubleshootTestManager(val fragment: Fragment) {
var diagStatus: TroubleshootTest.TestStatus by Delegates.observable(TroubleshootTest.TestStatus.NOT_STARTED) { _, _, _ -> var diagStatus: TroubleshootTest.TestStatus by Delegates.observable(TroubleshootTest.TestStatus.NOT_STARTED) { _, _, _ ->
statusListener?.invoke(this) statusListener?.invoke(this)
} }
private set
fun addTest(test: TroubleshootTest) { fun addTest(test: TroubleshootTest) {
testList.add(test) testList.add(test)
@ -79,25 +86,27 @@ class NotificationTroubleshootTestManager(val fragment: Fragment) {
} }
fun retry(activityResultLauncher: ActivityResultLauncher<Intent>) { fun retry(activityResultLauncher: ActivityResultLauncher<Intent>) {
for (test in testList) { testList.forEach {
test.cancel() it.cancel()
test.description = null it.description = null
test.quickFix = null it.quickFix = null
test.status = TroubleshootTest.TestStatus.NOT_STARTED it.status = TroubleshootTest.TestStatus.NOT_STARTED
} }
runDiagnostic(activityResultLauncher) runDiagnostic(activityResultLauncher)
} }
fun cancel() { fun hasQuickFix(): Boolean {
isCancelled = true return testList.any { test ->
for (test in testList) { test.status == TroubleshootTest.TestStatus.FAILED && test.quickFix != null
test.cancel()
} }
} }
fun onDiagnosticNotificationClicked() { fun cancel() {
testList.forEach { isCancelled = true
it.onNotificationClicked() testList.forEach { it.cancel() }
} }
fun onDiagnosticNotificationClicked() {
testList.forEach { it.onNotificationClicked() }
} }
} }