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

Small refactor UnifiedPushHelper #6936

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
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
1 change: 1 addition & 0 deletions changelog.d/6936.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Smaff refactor of UnifiedPushHelper
p1gp1g marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class UnifiedPushHelper @Inject constructor(
private val vectorFeatures: VectorFeatures,
private val fcmHelper: FcmHelper,
) {

// Called when the home activity starts
// or when notifications are enabled
fun register(
activity: FragmentActivity,
onDoneRunnable: Runnable? = null,
Expand All @@ -56,7 +59,14 @@ class UnifiedPushHelper @Inject constructor(
)
}

fun reRegister(
// If registration is forced:
// * the current distributor (if any) is removed
// * The dialog is opened
//
// The registration is forced in 2 cases :
// * in the settings
// * in the troubleshoot list (doFix)
fun forceRegister(
activity: FragmentActivity,
pushersManager: PushersManager,
onDoneRunnable: Runnable? = null
Expand Down Expand Up @@ -86,53 +96,35 @@ class UnifiedPushHelper @Inject constructor(
// Un-register first
unregister(pushersManager)
}
if (UnifiedPush.getDistributor(context).isNotEmpty()) {
// the !force should not be needed
if (!force && UnifiedPush.getDistributor(context).isNotEmpty()) {
UnifiedPush.registerApp(context)
onDoneRunnable?.run()
return@launch
}

val distributors = UnifiedPush.getDistributors(context)

if (distributors.size == 1 && !force) {
if (!force && distributors.size == 1) {
UnifiedPush.saveDistributor(context, distributors.first())
UnifiedPush.registerApp(context)
onDoneRunnable?.run()
} else {
openDistributorDialogInternal(
activity = activity,
pushersManager = pushersManager,
onDoneRunnable = onDoneRunnable,
distributors = distributors,
unregisterFirst = force,
cancellable = !force
distributors = distributors
)
}
}
}

fun openDistributorDialog(
activity: FragmentActivity,
pushersManager: PushersManager,
onDoneRunnable: Runnable,
) {
val distributors = UnifiedPush.getDistributors(activity)
openDistributorDialogInternal(
activity,
pushersManager,
onDoneRunnable, distributors,
unregisterFirst = true,
cancellable = true,
)
}

// There is no case where this function is called
// with a saved distributor and/or a pusher
private fun openDistributorDialogInternal(
activity: FragmentActivity,
pushersManager: PushersManager?,
onDoneRunnable: Runnable?,
distributors: List<String>,
unregisterFirst: Boolean,
cancellable: Boolean,
distributors: List<String>
) {
val internalDistributorName = stringProvider.getString(
if (fcmHelper.isFirebaseAvailable()) {
Expand All @@ -154,16 +146,8 @@ class UnifiedPushHelper @Inject constructor(
.setTitle(stringProvider.getString(R.string.unifiedpush_getdistributors_dialog_title))
.setItems(distributorsName.toTypedArray()) { _, which ->
val distributor = distributors[which]
if (distributor == UnifiedPush.getDistributor(context)) {
Timber.d("Same distributor selected again, no action")
return@setItems
}

activity.lifecycleScope.launch {
if (unregisterFirst) {
// Un-register first
unregister(pushersManager)
}
UnifiedPush.saveDistributor(context, distributor)
Timber.i("Saving distributor: $distributor")
UnifiedPush.registerApp(context)
Expand All @@ -176,7 +160,7 @@ class UnifiedPushHelper @Inject constructor(
UnifiedPush.registerApp(context)
onDoneRunnable?.run()
}
.setCancelable(cancellable)
.setCancelable(true)
.show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class VectorSettingsNotificationPreferenceFragment :
if (vectorFeatures.allowExternalUnifiedPushDistributors()) {
it.summary = unifiedPushHelper.getCurrentDistributorName()
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
unifiedPushHelper.openDistributorDialog(requireActivity(), pushersManager) {
unifiedPushHelper.forceRegister(requireActivity(), pushersManager) {
it.summary = unifiedPushHelper.getCurrentDistributorName()
session.pushersService().refreshPushers()
refreshBackgroundSyncPrefs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class TestEndpointAsTokenRegistration @Inject constructor(
)
quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_endpoint_registration_quick_fix) {
override fun doFix() {
unifiedPushHelper.reRegister(
unifiedPushHelper.forceRegister(
context,
pushersManager
)
Expand Down