/// What the composer's send-button usage ring shows: the harness account's /// 5-hour window (the most constrained short window harnesses report), or the /// most used window when there is no 5-hour one, and how loudly to draw it. /// Only reported windows count; one whose reset has passed is stale and left /// out, and nothing is estimated. library; import 'chat_account_usage.dart'; /// The percentages at which the ring turns from quiet to a warning, then /// urgent. const int chatQuotaWarningLevel = 80; const int chatQuotaUrgentLevel = 95; /// How the ring is drawn: barely visible, a warning from the first warning /// level (80%), urgent from [chatQuotaUrgentLevel] (95%). enum ChatUsageRingTone { quiet, warning, urgent } class ChatUsageRing { const ChatUsageRing({ required this.shown, required this.windows, this.extraUsage, }); /// The window the ring's arc measures. final ChatQuotaWindow shown; /// Every live window, in the harness's order, for the tooltip. final List windows; /// "Extra usage available" or "Using extra usage" ([chatExtraUsageNote]). final String? extraUsage; /// The arc's share of the circle, 0–1 (past a cap stays full). double get fraction => (shown.usedPercent / 100).clamp(0.0, 1.0); ChatUsageRingTone get tone { final percent = shown.usedPercent; if (percent >= chatQuotaUrgentLevel) return ChatUsageRingTone.urgent; if (percent >= chatQuotaWarningLevel) return ChatUsageRingTone.warning; return ChatUsageRingTone.quiet; } } bool _fiveHour(ChatQuotaWindow window) => window.id == 'five_hour' || window.label.toLowerCase() == '5-hour'; /// The ring for [usage] at [now], or null when the harness reports no quota /// (unknown, unsupported, or every window already reset). ChatUsageRing? chatUsageRing(ChatAccountUsage? usage, DateTime now) { if (usage == null || usage.unsupported) return null; final at = now.millisecondsSinceEpoch; final live = [ for (final window in usage.windows) if (window.resetsAt <= 0 || window.resetsAt > at) window, ]; if (live.isEmpty) return null; var shown = live.firstWhere(_fiveHour, orElse: () => live.first); if (!_fiveHour(shown)) { for (final window in live) { if (window.usedPercent > shown.usedPercent) shown = window; } } return ChatUsageRing( shown: shown, windows: List.unmodifiable(live), extraUsage: chatExtraUsageNote(usage), ); } const List _weekdays = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', ]; String _two(int value) => value.toString().padLeft(2, '0'); /// A reset time in local 24-hour time: "17:20" today, "Thu 09:00" within the /// coming week, "28 Nov 09:00" further out. String chatUsageRingResetTime(DateTime at, DateTime now) { final local = at.toLocal(); final today = now.toLocal(); final time = '${_two(local.hour)}:${_two(local.minute)}'; final day = DateTime(local.year, local.month, local.day); final days = day.difference(DateTime(today.year, today.month, today.day)); if (days.inHours.abs() < 12) return time; if (!days.isNegative && days.inDays < 7) { return '${_weekdays[local.weekday - 1]} $time'; } const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', // 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ]; return '${local.day} ${months[local.month - 1]} $time'; } /// "5-hour 42% · resets 17:20 · Weekly 61% · resets Thu 09:00", then any /// extra usage note. String chatUsageRingSummary(ChatUsageRing ring, DateTime now) => [ for (final window in ring.windows) ...[ '${window.label} ${window.usedPercent.round()}%', if (window.resetsAt > 0) 'resets ${chatUsageRingResetTime(DateTime.fromMillisecondsSinceEpoch(window.resetsAt), now)}', ], ?ring.extraUsage, ].join(' · ');