Skip to content

Conversation

@0nko
Copy link
Contributor

@0nko 0nko commented May 22, 2023

Fixes #9060, a subtask of #8999.

This PR integrates the WP.com feature flag repository and fetches the flags on app initialization. LOCAL_NOTIFICATION_STORE_CREATION_READY is then used to check if a local notification should be scheduled 5 minutes after the store creation is started.

The WorkManager worker chain has been updated and it now contains 2 steps:

  1. PreconditionWorker - This worker is delayed by the configured amount of time, upon which a precondition check is performed. If the precondition is satisfied, the final step is executed, otherwise the work is canceled.
  2. LocalNotificationWorker - This is the point when a local notification is displayed.

To test:

  1. Start the store creation flow (either from the prologue or site picker)
  2. Complete all the steps and sign up for a free trial
  3. Put the app in the background and wait for > 5 minutes
  4. Notice a notification is displayed
  5. Tap on the notification and the new store is loaded
  6. Repeat the store creation flow but now keep the app running in the foreground
  7. Wait for > 5 minutes
  8. Notice the new store is automatically loaded and the notification is not displayed

Note
Please only merge after the target branch is set to trunk

@0nko 0nko added the feature: notifications Related to notifications or notifs. label May 22, 2023
@0nko 0nko added this to the 13.8 milestone May 22, 2023
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented May 22, 2023

You can test the changes on this Pull Request by downloading an installable build, or scanning this QR code:

Base automatically changed from issue/9061-notifications-permission to trunk May 22, 2023 15:30
@JorgeMucientes JorgeMucientes self-assigned this May 22, 2023
@woocommerce woocommerce deleted a comment from peril-woocommerce bot May 22, 2023
Comment on lines 24 to 45
override fun doWork(): Result {
return if (canDisplayNotifications) {
val type = inputData.getString(LOCAL_NOTIFICATION_TYPE)
if (type != null) {
when (type) {
LocalNotificationType.STORE_CREATION_FINISHED.value,
LocalNotificationType.STORE_CREATION_INCOMPLETE.value,
LocalNotificationType.FREE_TRIAL_EXPIRING.value,
LocalNotificationType.FREE_TRIAL_EXPIRED.value -> {
Result.success()
}
else -> {
cancelWork("Unknown notification $type. Cancelling work.")
}
}
} else {
cancelWork("Notification check data is invalid")
}
} else {
cancelWork("Notifications permission not granted. Cancelling work.")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, you can maybe reduce some nesting here with early return approach:

    override fun doWork(): Result {
        if (!canDisplayNotifications) cancelWork("Notifications permission not granted. Cancelling work.")
        val type = inputData.getString(LOCAL_NOTIFICATION_TYPE)
        if (type == null) cancelWork("Notification check data is invalid")
        return when (type) {
            LocalNotificationType.STORE_CREATION_FINISHED.value,
            LocalNotificationType.STORE_CREATION_INCOMPLETE.value,
            LocalNotificationType.FREE_TRIAL_EXPIRING.value,
            LocalNotificationType.FREE_TRIAL_EXPIRED.value -> {
                Result.success()
            }

            else -> {
                cancelWork("Unknown notification $type. Cancelling work.")
            }
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip, I like it! 👍 I modified it a bit because the !canDisplayNotifications and type == null cases did not return in your example, which would log multiple cancellation messages.

fun onTryForFreeButtonPressed() {
tracker.track(AnalyticsEvent.SITE_CREATION_TRY_FOR_FREE_TAPPED)

manageDeferredNotifications()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, I think I'd move this scheduling down to line 77-78. Once we know the new site was created successfully but before the installation phase begins. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, I wanted to do that but I missed it! 👍

wasPreviouslyStopped = false
}

myStoreViewModel.onStoreOpened()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this? Can we not handle everything from MyStoreViewModel. For example by calling notificationScheduler.cancelScheduledNotification(STORE_CREATION_FINISHED) from the viewmodel's init block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure but you're right, init works 👍

@JorgeMucientes
Copy link
Contributor

JorgeMucientes commented May 23, 2023

Excellent job @0nko 🥇 I like the preconditionWorker approach and naming 👍🏼.

I left a few minor nitpicks, but apart from that, the PR is good to go after conflicts are resolved.

# Conflicts:
#	WooCommerce/src/main/kotlin/com/woocommerce/android/AppInitializer.kt
@peril-woocommerce
Copy link

peril-woocommerce bot commented May 23, 2023

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 2 days Please, make sure to get it merged by then or assign it to a later expiring milestone
⚠️ PR has more than 300 lines of code changing. Consider splitting into smaller PRs if possible.

Generated by 🚫 dangerJS

@0nko 0nko enabled auto-merge May 23, 2023 22:40
@0nko 0nko requested a review from a team as a code owner May 24, 2023 09:56
@0nko 0nko merged commit 7dedc19 into trunk May 24, 2023
@0nko 0nko deleted the issue/9060-store-ready-notification branch May 24, 2023 10:15
@pachlava
Copy link
Contributor

Thanks for adding the request matcher for WireMock @0nko 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: notifications Related to notifications or notifs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send a scheduled notification when a user's store is ready.

5 participants