Files
swiftcontrol/lib/utils/plan_format.dart
Jonas Acker a3ba15acf3 feat(plan): read whether the subscription renews from the store
The store's record of the Pro entitlement says whether it renews, has a
billing issue or is in a trial; the account entitlement only carries a
date, so it never claims a renewal. Restore purchases reports its outcome
in the app's language, the macOS manage action opens the App Store's
subscription settings, and the plan status line is localised.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 19:35:31 +02:00

32 lines
1.4 KiB
Dart

import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/models/subscription_term.dart';
import 'package:intl/intl.dart';
/// A plan date ("4.11.2026") in the app's language.
String formatPlanDate(DateTime date) => DateFormat.yMd(Intl.getCurrentLocale()).format(date.toLocal());
/// How long ago [time] was, in the app's language: "Just now", "5 min ago",
/// "3 h ago", "2 days ago".
String formatRelativeTime(AppLocalizations l10n, DateTime time, {DateTime? now}) {
final diff = (now ?? DateTime.now()).difference(time);
if (diff.inMinutes < 1) return l10n.justNow;
if (diff.inMinutes < 60) return l10n.relativeMinutesAgo(diff.inMinutes);
if (diff.inHours < 24) return l10n.relativeHoursAgo(diff.inHours);
return l10n.relativeDaysAgo(diff.inDays);
}
/// The one line about the subscription's end: "Renews on …" only when the
/// store says so, else "Active until …", "Trial until …" or the payment
/// problem. Null when there is no date to state.
String? subscriptionTermLine(AppLocalizations l10n, SubscriptionTerm term) {
if (term.renewal == SubscriptionRenewal.billingIssue) return l10n.subscriptionBillingIssue;
final until = term.until;
if (until == null) return null;
final date = formatPlanDate(until);
return switch (term.renewal) {
SubscriptionRenewal.renews => l10n.subscriptionRenewsOn(date),
SubscriptionRenewal.trial => l10n.subscriptionTrialUntil(date),
_ => l10n.subscriptionActiveUntil(date),
};
}