Skip to content
Source
Sign inSign up
Open History to see the last commit

publishSubscriptionUsage.android.ts

30 lines1.2 KB

1import { requireOptionalNativeModule } from "expo";
2import * as Linking from "expo-linking";
3import type { SubscriptionUsageSnapshot } from "./subscriptionUsageSnapshot";
4​
5let tapUrl: string | undefined;
6let listening = false;
7​
8export async function publishSubscriptionUsage(snapshot: SubscriptionUsageSnapshot) {
9 if (!requireOptionalNativeModule("ExpoWidgets")) return;
10 const [{ default: widget }, { addUserInteractionListener }] = await Promise.all([
11 import("./SubscriptionUsage"),
12 import("expo-widgets"),
13 ]);
14 tapUrl = snapshot.url;
15 if (!listening) {
16 // expo-widgets lacks Android widgetURL. Button events need a live JS runtime to open the app.
17 listening = true;
18 addUserInteractionListener((event) => {
19 if (event.source === "SubscriptionUsage" && tapUrl) void Linking.openURL(tapUrl);
20 });
21 }
22 widget.updateSnapshot(snapshot);
23 // Android lacks timelines; alarms re-render snapshots at expiry to mark readings stale.
24 requireOptionalNativeModule<{ schedule: (name: string, deadlines: number[]) => void }>(
25 "T3WidgetExpiry",
26 )?.schedule(
27 "SubscriptionUsage",
28 snapshot.providers.map((provider) => provider.expiresAt).filter((at) => at > 0),
29 );