Files
swiftcontrol/lib/widgets/home/trial_card.dart
Jonas Bark ca885c79d9 fix(trial): commands are capped during the trial too, and say so
"Unlimited commands until the trial ends" was not true.
RevenueCatService.canExecuteCommand is dailyCommandCount <
getDailyCommandLimit() and returns early only for purchased users — never
for trial users. What actually happens is setDailyCommandLimit(80) while
the trial runs, falling back to 15 once it expires. So the cap is 80/day
during the trial and 15/day after, and the card now shows the command
meter in both states rather than promising something the code does not do.

The same falsehood was in trialPeriodDescription, which is worse: it is
rendered only while the trial is live (iap_status_widget's !isTrialExpired
branch) and handed the *current* limit, so it read "unlimited now, 80 per
day later" — where 80 is precisely the limit that applies now. Corrected
in all six locales rather than leaving five of them asserting it, each
keeping the informal register its neighbouring strings already use.

More Options opens by default — what is in it is short, and collapsing it
on a screen the rider is already scrolling only hides it — and the quit
row now says what it does to the connections as well as to the app.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-11 14:07:29 +02:00

234 lines
8.5 KiB
Dart

import 'package:bike_control/utils/i18n_extension.dart';
import 'package:bike_control/widgets/home/ampel.dart';
import 'package:bike_control/pages/home/chain_state.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
/// What the licence card has to say, as plain data — so the numbers can be
/// checked without a store connection.
class TrialCardState {
const TrialCardState({
required this.daysRemaining,
required this.daysTotal,
required this.expired,
required this.commandsRemaining,
required this.commandsTotal,
this.bridgeMinutesRemaining,
this.bridgeMinutesTotal,
});
final int daysRemaining;
final int daysTotal;
final bool expired;
final int commandsRemaining;
final int commandsTotal;
/// Only set when a smart trainer is actually present — virtual shifting
/// minutes are meaningless otherwise, and an idle meter is just noise.
final int? bridgeMinutesRemaining;
final int? bridgeMinutesTotal;
bool get showsBridgeMeter => bridgeMinutesRemaining != null && (bridgeMinutesTotal ?? 0) > 0;
bool get _bridgeLow =>
showsBridgeMeter && bridgeMinutesRemaining! / bridgeMinutesTotal! <= 0.25;
/// Urgency is earned, not constant: the card only turns amber when one of
/// the budgets is genuinely close to running out.
bool get urgent => expired || daysRemaining <= 1 || _bridgeLow;
}
/// The licence card, deliberately built from the same parts as the chain cards
/// — same surface, same 1.5px border, same tile + title + action header — so it
/// belongs to the stack instead of sitting in it as a foreign slab.
///
/// Both limits are shown as meters. "80 commands left" is meaningless without
/// its ceiling; "80 of 100" tells a rider how much runway they have.
class TrialCard extends StatelessWidget {
const TrialCard({super.key, required this.state, this.onUpgrade, this.onRestore});
final TrialCardState state;
final VoidCallback? onUpgrade;
final VoidCallback? onRestore;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final l = context.i18n;
final warning = AmpelStyle.of(context, LinkStatus.attention);
return Container(
decoration: ShapeDecoration(
color: theme.colorScheme.card,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: state.urgent ? warning.color : theme.colorScheme.border, width: 1.5),
),
),
clipBehavior: Clip.antiAlias,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(13, 13, 13, 12),
child: Row(
children: [
Container(
width: 38,
height: 38,
decoration: BoxDecoration(color: warning.wash, borderRadius: BorderRadius.circular(11)),
child: Icon(LucideIcons.award, size: 19, color: warning.color),
),
const Gap(12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
state.expired ? l.chainTrialExpiredTitle : l.chainTrialTitle,
style: const TextStyle(fontSize: 15.5, fontWeight: FontWeight.w700),
),
const Gap(2),
Text(
state.expired
? l.chainTrialCommandsLimited(state.commandsTotal)
: l.chainTrialDaysLeft(state.daysRemaining),
style: TextStyle(
fontSize: 12.5,
fontWeight: state.urgent ? FontWeight.w700 : FontWeight.w500,
color: state.urgent ? warning.color : theme.colorScheme.mutedForeground,
),
),
],
),
),
const Gap(8),
PrimaryButton(
size: ButtonSize.small,
onPressed: onUpgrade,
child: Text(l.chainUpgrade),
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(14, 0, 14, 13),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Commands are capped during the trial too — RevenueCatService
// enforces the daily limit whatever the trial state, it just
// raises the ceiling to 80 while the trial runs and drops it
// afterwards. So the meter is always honest, and always shown.
if (!state.expired) ...[
_Meter(label: l.chainTrialDaysMeter, value: state.daysRemaining, total: state.daysTotal),
const Gap(11),
],
_Meter(
label: l.chainTrialCommandsMeter,
value: state.commandsRemaining,
total: state.commandsTotal,
),
if (state.showsBridgeMeter) ...[
const Gap(11),
_Meter(
label: l.chainTrialBridgeMeter,
value: state.bridgeMinutesRemaining!,
total: state.bridgeMinutesTotal!,
suffix: l.chainTrialBridgeMeterSuffix,
),
],
],
),
),
if (onRestore != null)
Button.ghost(
onPressed: onRestore,
child: Container(
decoration: BoxDecoration(
border: Border(top: BorderSide(color: theme.colorScheme.border, width: 0.5)),
),
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
child: Row(
children: [
Icon(LucideIcons.rotateCcw, size: 14, color: theme.colorScheme.mutedForeground),
const Gap(7),
Expanded(
child: Text(
l.chainTrialRestoreRow,
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: theme.colorScheme.mutedForeground,
),
),
),
Icon(LucideIcons.chevronRight, size: 14, color: theme.colorScheme.mutedForeground),
],
),
),
),
],
),
);
}
}
/// A budget-of-total bar. Turns amber in the last quarter — the point at which
/// the number starts to matter.
class _Meter extends StatelessWidget {
const _Meter({required this.label, required this.value, required this.total, this.suffix});
final String label;
final int value;
final int total;
final String? suffix;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final fraction = total > 0 ? (value / total).clamp(0.0, 1.0) : 0.0;
final low = fraction <= 0.25;
final warning = AmpelStyle.of(context, LinkStatus.attention).color;
final muted = theme.colorScheme.mutedForeground;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: Text(label, style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500, color: muted)),
),
const Gap(6),
Text(
'$value',
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w700,
color: low ? warning : theme.colorScheme.foreground,
),
),
const Gap(4),
Text(
suffix == null ? '/ $total' : '/ $total $suffix',
style: TextStyle(fontSize: 11.5, color: muted),
),
],
),
const Gap(5),
ClipRRect(
borderRadius: BorderRadius.circular(3),
child: LinearProgressIndicator(
value: fraction,
minHeight: 4,
backgroundColor: theme.colorScheme.muted,
color: low ? warning : theme.colorScheme.primary,
),
),
],
);
}
}