mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-25 19:05:56 +03:00
Using use cases inside component for endpoint testing
This commit is contained in:
parent
58efe90f7d
commit
b29191e892
1 changed files with 47 additions and 16 deletions
|
@ -17,14 +17,17 @@
|
|||
package im.vector.app.features.settings.troubleshoot
|
||||
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.work.WorkInfo
|
||||
import androidx.work.WorkManager
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
import im.vector.app.core.pushers.PushersManager
|
||||
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
|
||||
import im.vector.app.core.pushers.UnifiedPushHelper
|
||||
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import im.vector.app.features.session.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.android.sdk.api.session.pushers.PusherState
|
||||
import javax.inject.Inject
|
||||
|
||||
|
@ -34,6 +37,8 @@ class TestEndpointAsTokenRegistration @Inject constructor(
|
|||
private val pushersManager: PushersManager,
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
private val unifiedPushHelper: UnifiedPushHelper,
|
||||
private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase,
|
||||
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
|
||||
) : TroubleshootTest(R.string.settings_troubleshoot_test_endpoint_registration_title) {
|
||||
|
||||
override fun perform(testParameters: TestParameters) {
|
||||
|
@ -56,27 +61,53 @@ class TestEndpointAsTokenRegistration @Inject constructor(
|
|||
)
|
||||
quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_endpoint_registration_quick_fix) {
|
||||
override fun doFix() {
|
||||
unifiedPushHelper.forceRegister(
|
||||
context,
|
||||
pushersManager
|
||||
)
|
||||
val workId = pushersManager.enqueueRegisterPusherWithFcmKey(endpoint)
|
||||
WorkManager.getInstance(context).getWorkInfoByIdLiveData(workId).observe(context, Observer { workInfo ->
|
||||
if (workInfo != null) {
|
||||
if (workInfo.state == WorkInfo.State.SUCCEEDED) {
|
||||
manager?.retry(testParameters)
|
||||
} else if (workInfo.state == WorkInfo.State.FAILED) {
|
||||
manager?.retry(testParameters)
|
||||
}
|
||||
}
|
||||
})
|
||||
unregisterThenRegister(testParameters, endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
status = TestStatus.FAILED
|
||||
} else {
|
||||
description = stringProvider.getString(R.string.settings_troubleshoot_test_endpoint_registration_success)
|
||||
status = TestStatus.SUCCESS
|
||||
}
|
||||
}
|
||||
|
||||
private fun unregisterThenRegister(testParameters: TestParameters, pushKey: String) {
|
||||
activeSessionHolder.getSafeActiveSession()?.coroutineScope?.launch {
|
||||
unregisterUnifiedPushUseCase.execute(pushersManager)
|
||||
registerUnifiedPush(distributor = "", testParameters, pushKey)
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerUnifiedPush(
|
||||
distributor: String,
|
||||
testParameters: TestParameters,
|
||||
pushKey: String,
|
||||
) {
|
||||
when (val result = registerUnifiedPushUseCase.execute(distributor)) {
|
||||
is RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor ->
|
||||
askUserForDistributor(result.distributors, testParameters, pushKey)
|
||||
RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success -> {
|
||||
val workId = pushersManager.enqueueRegisterPusherWithFcmKey(pushKey)
|
||||
WorkManager.getInstance(context).getWorkInfoByIdLiveData(workId).observe(context) { workInfo ->
|
||||
if (workInfo != null) {
|
||||
if (workInfo.state == WorkInfo.State.SUCCEEDED) {
|
||||
manager?.retry(testParameters)
|
||||
} else if (workInfo.state == WorkInfo.State.FAILED) {
|
||||
manager?.retry(testParameters)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun askUserForDistributor(
|
||||
distributors: List<String>,
|
||||
testParameters: TestParameters,
|
||||
pushKey: String,
|
||||
) {
|
||||
unifiedPushHelper.showSelectDistributorDialog(context, distributors) { selection ->
|
||||
registerUnifiedPush(distributor = selection, testParameters, pushKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue