Skip to content

Refresh the plan when the app resumes and when Plans & Billing opens - #26181

Merged
kodjima33 merged 5 commits into
mainfrom
fix/plan-refresh-on-resume
Nov 24, 2026
Merged

kodjima33 merged 5 commits into
mainfrom
fix/plan-refresh-on-resume

Conversation

@kodjima33

@kodjima33 kodjima33 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

On Android build 1.0.598 (1620), a Google Play-billed subscriber cancelled in Google Play's subscription page (Plans & Billing → Manage subscription) and came back with Back. The sheet still said "Your Omi Pro plan will renew on ." Closing and reopening the sheet did not help; only relaunching the app showed "ends". Resubscribing had the same problem the other way round. The server row was right within seconds both times, so the app was simply not reading GET /v1/users/me/subscription again.

The app now reads the plan again:

  1. When it comes back to the foreground, on every platform. This also covers an iOS Stripe reader returning from the portal in the browser.
    • A small observer widget, PlanRefreshOnResume, wraps Home's content and calls UsageProvider.refreshOnResume.
    • At most one successful read every 2 minutes. A failed read does not start the window.
    • No read when signed out, and none while a Google Play purchase is on Play's sheet or being confirmed (PlayBilling.busy). The Play sheet closing is not the reader coming back, and the purchase reads on its own.
    • No read on cold start: Flutter sends no resume event on launch.
  2. When Plans & Billing opens (PlansSheet.initState, so every host that shows the sheet gets it). The last answer stays on screen while the read runs and updates in place. A failed read keeps the working sheet, with no skeleton and no error state.
  3. Once more about 5 seconds after returning from Google Play's subscription page. This happens only after the "Manage subscription" tap for a Play-billed plan, because the server learns of the change from Play's notification a few seconds after the reader is back.
    • This resume skips the 2-minute window.
    • There is exactly one follow-up and no polling.
    • A Google Play page that fails to open does not arm it.
    • Signing out cancels it.

Race this PR introduced, and its fix

fetchSubscription() shares a read that is already in flight. With the resume read added, the Play purchase sheet closing fired a GET at once, while the verify POST started alongside it. If that GET was still in flight when verify returned, the post-purchase fetchSubscription() joined it and got the stale Free answer. A reader who had just paid then saw Free under the success toast. The reviewer reproduced it: 1 GET, plan still basic.

The fix has two parts:

  • fetchSubscription(fresh: true). Use it when the server just changed. A read already in flight may have started before the change, so the call waits for it and then issues one more read. Its future completes with that later read, and fresh callers arriving meanwhile share the same later read.
  • No resume read while a Play purchase or verify is in flight (see 1 above).

Callers that read after a server change, all now fresh: true (from grep -rn fetchSubscription app/lib):

  • pages/paywall/paywall_checkout.dart:
    • reactivated / resubscribe without checkout (:85)
    • Stripe checkout return (:107)
    • Play purchase verified (:146)
  • pages/settings/widgets/plans_sheet.dart:
    • upgrade / plan switch applied (:610)
    • Stripe checkout return (:619)
    • customer portal return (:719)
  • providers/usage_provider.dart:
    • Play pending-token delivery (_deliverPendingPlayTokens)
    • restorePurchases (Play restore)
    • redeemUserSubscriptionCode (code applied)
    • cancelUserSubscription
    • upgradeUserSubscription
  • pages/settings/widgets/plans/prepaid_code_sheet.dart:83-84: prepaid code redeemed (the read now and the one 4 s later)
  • pages/settings/widgets/cancel_subscription_sheet.dart:223: retention offer accepted
  • pages/settings/widgets/yearly_switch.dart:217: yearly switch
  • pages/home/widgets/monthly_switch_sheet.dart:82, 91: payment-issue monthly switch, after an answer and after no answer
  • pages/home/widgets/payment_issue_row.dart:180: customer portal return
  • the Google Play manage follow-up read

Plain reads, unchanged: startup, screen opens, paywall gates, quota invalidation and the backlog poll.

Also in this PR, to get the analyzer green:

  • integration_test/visual_audit/demo_billing.dart takes the fresh flag.
  • test/widgets/finish_opens_conversation_test.dart's forceRefreshConversations override now accepts reconcileHeldRecordings. The analyzer on main fails without it, because Show manual finish progress on Home v3 #26080's override predates the parameter added in 9e3cc7c.

Product invariants affected

none

How it was verified

  • app/test/widgets/plan_refresh_on_resume_test.dart (13 tests) uses a fake server whose GET answers with what the server held when the request arrived:
    • The reviewer's probe, end to end through the real Play purchase path (PlayPlansContent → PaywallCheckout → PlayBilling). A resume GET is held open carrying Free, the purchase is verified, and then the stale answer lands. The app ends on Pro and exactly 2 GETs were issued. Without fresh: true the same test sees 1 GET.
    • A resume while a Play purchase is being confirmed issues no GET. Without the guard it issues one.
    • The cases from the first round:
      • a resume reads once, and the sheet goes from "will renew" to "ends" and back without a relaunch;
      • opening the sheet reads and updates in place;
      • signed out: no read;
      • the Play manage path reads on resume plus exactly one follow-up 5 s later.
    • The 2-minute window (1:59 reads once, 2:00 reads again).
    • A failed resume read does not block the next one.
    • A failed read on sheet open keeps the plan on screen, with no error state.
    • Two fresh reads requested during a stale read share one later read.
    • A Google Play page that fails to open does not arm the follow-up.
    • Signing out cancels the pending follow-up.
    • PlanRefreshOnResume driven by handleAppLifecycleStateChanged: 1 read on resume when signed in, 0 when signed out, 0 when leaving.
  • Updated fixtures:
    • Test fakes that override fetchSubscription now accept fresh.
    • Four suites' scripted answers allow for the sheet's opening read. Assertions are unchanged.
  • 1117 tests pass across 94 files: every test touching the changed callers, plans sheet, paywall, Play billing, UsageProvider, Home widgets and test/pages/home. flutter analyze reports no errors.
  • Device evidence comes from the Android emulator test on the internal build after merge, including buying Pro on a Free test account and checking that a GET follows the verify call.

UI evidence

UI-Evidence: none -- no layout or copy change; the existing Plans & Billing line now switches between "Your Omi Pro plan will renew on ." and "Your Omi Pro plan ends ." after a resume, after reopening the sheet, and after Google Play's follow-up read, and a just-bought plan shows as Pro instead of Free; device evidence comes from the Android emulator test on the internal build after merge

Failure class

No fix: commits; no declaration required.

View guided diff Turn on auto-fix

After cancelling or resubscribing in Google Play (or the Stripe portal), the
sheet kept the old renew/ends line until the app was relaunched. Re-read the
subscription on resume (at most once per 30 s, never signed out), when the
plans sheet opens, and once more 5 s after returning from Google Play's
subscription page, since Play notifies the server a few seconds later.
@kodjima33
kodjima33 marked this pull request as ready for review November 24, 2026 00:56
A resume read that left before a Google Play verify could still be in
flight when verify returned, and the post-purchase read joined it, so a
reader who had just paid saw Free. fetchSubscription(fresh: true) now waits
for an in-flight read and reads once more; every caller that reads after a
server change uses it. The resume read is skipped while a Play purchase is
being confirmed, its window is 2 minutes and starts only after a successful
read, a Google Play page that fails to open no longer arms the follow-up,
and the lifecycle hook is a small observer widget with its own tests.
DemoBillingUsage.fetchSubscription takes the new fresh flag. The manual
finish test's forceRefreshConversations override predates the
reconcileHeldRecordings parameter on main, which breaks the analyzer there.
@kodjima33
kodjima33 merged commit a75daeb into main Nov 24, 2026
52 checks passed
@kodjima33
kodjima33 deleted the fix/plan-refresh-on-resume branch November 24, 2026 02:40
kodjima33 added a commit that referenced this pull request Nov 24, 2026
…nds the purchase, runbook cancel step (#26189)

## What changed and why

Five small items the reviewers of #26165 and #26181 left open. None
changes a price, a purchase path or an entitlement rule.

1. **Plans sheet keeps Retry after a failed plan read**
(`play_plans_content.dart`, `PlayPlansLoading`). The loading state
decided "failed" from the provider's shared error. A successful usage
read clears that error, so the sheet went back to a skeleton with no
Retry while the plan read was still known to have failed. It now uses
`billingProviderFailed`, the same flag the paywalls and the paid app
button use since #26165.
2. **A token store error ends the purchase as failed**
(`play_billing.dart`, `_deliver`). If reading or writing the
pending-token store threw, the purchase was never finished: the paywall
waited forever and `PlayBilling.busy` stayed true, which since #26181
also switches the resume read off for the session. `_deliver` now
catches, logs and returns a failed result. The cleanup write after the
server has answered has its own catch, so a plan the server granted is
still reported as bought; the token is then sent once more on the next
launch.
3. **Trial paywall's "was something bought before the reader left" check
reads fresh** (`trial_paywall_page.dart`). It could join a resume read
that started before the purchase and offer the monthly trial to someone
who had just paid. The 4 s timeout still bounds it.
4. **Runbook** (`backend/docs/runbooks/play-billing-deletion.md`): the
step for a purchase stuck more than a day told the operator to find the
order in Play Console by the user's order. By then the account's email
is deleted and the purchase document holds no order id. It now points at
the stored `purchase_token` and the cancel call the wipe makes.
5. **Test that Home mounts `PlanRefreshOnResume`**, so removing the
wrapper fails a test.

## Product invariants affected

none

## How it was verified

- New tests, each checked to fail without its fix:
- `play_billing_plans_sheet_test.dart`: the plans sheet loading state
shows the error state, a usage read clears the shared error, and the
error state with Retry is still there (shimmer before the fix).
- `play_billing_test.dart`: a purchase with a token store that throws
returns `failed` and `busy` is false (hung before the fix). A store that
fails only on the cleanup write still returns `purchased`.
- `plan_refresh_on_resume_test.dart`: Home's source mounts
`PlanRefreshOnResume`.
- `flutter test test/unit/play_billing_test.dart
test/widgets/play_billing_plans_sheet_test.dart
test/widgets/plan_refresh_on_resume_test.dart`: 70 passed. `flutter
analyze` on the changed files: no new issues.

## UI evidence

UI-Evidence: none -- no layout or copy change; the existing "Unable to
load plans" state with Retry now stays on screen in a case where it used
to fall back to the existing skeleton

## Failure class

No `fix:` commits; no declaration required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant