Files
GroundedHelper/frontend/lib/Grounded/utils/CommonUtils.dart
alvocool 16bff634b5 Initial commit: Grounded Flutter frontend
A to-do app that doesn't believe you — an enforcement layer rather than a
neutral ledger.

Architecture ported from Autoreceptives/Frontend/Receptive: stacked MVVM with
the mandatory 4-file screen pattern, one ParentViewModel owning the loading /
network / error overlays and the handleError decision tree, one AppDataManager
gateway, dio comms carrying the three identity headers, secure storage with
random-suffixed keys, and a single-chokepoint Navigator. Package root and Dart
package name are both Grounded; org is nya.

The enforcement engine, one unit per formula in utils/:

- DebtEngine      w(class) x severity(d) x decay(t), sublinear severity so old
                  misses cannot swamp the score; abandonment 2x with 30-day
                  decay immunity; late complete retains 30%
- StandingEngine  Good -> Warned -> Grounded -> Lockdown, derived not set;
                  Grounded replaces home with the overdue queue
- CapacityEngine  blocks over-scheduling against p50 of historically completed
                  minutes, with a learned per-category estimation multiplier
- IntegrityEngine session integrity, weekly volume, plyometric contact ceiling
                  and enforced recovery gaps
- ExcuseAnalyser  on-device excuse clustering plus the confrontation copy
- GuardrailEngine distress detection and rationed amnesty
- ToneEngine      all enforcement copy, so the tone cap lives in one place

CommitmentEvent is append-only and is the source of truth rather than the
status field, which is what makes honest history and excuse analysis possible.

Goals contain commitments via parentId, and a task can be run from a
full-screen runner that derives elapsed time from wall-clock so screen-off
cannot lose time. Backgrounding pauses the clock and is counted. The runner is
mirrored into an ongoing notification, with alarm-class full-screen intents
reserved for non-negotiables.

Design language, fonts, icon and native splash are in place; Mason bricks are
retargeted to this project and verified end-to-end.

flutter analyze lib/ reports no errors.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mfu2gQLSFN21YRBcU2NrTt
2026-07-27 09:11:17 +03:00

189 lines
4.7 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../about/external/data/Commitment.dart';
import '../about/internal/application/CommitmentClass.dart';
import '../about/internal/application/Standing.dart';
import 'Colors.dart';
/// Formatting and small shared derivations. Anything a screen would otherwise
/// inline twice belongs here.
/// "Mon 06:0008:00" — the window, which is the whole point.
String formatWindow(Commitment commitment) {
if (commitment.dueStart == null || commitment.dueEnd == null) {
return "No window set";
}
final DateFormat day = DateFormat('EEE');
final DateFormat time = DateFormat('HH:mm');
final String startDay = day.format(commitment.dueStart!);
final String startTime = time.format(commitment.dueStart!);
final String endTime = time.format(commitment.dueEnd!);
final bool sameDay = commitment.dueStart!.day == commitment.dueEnd!.day &&
commitment.dueStart!.month == commitment.dueEnd!.month;
if (sameDay) {
return "$startDay $startTime$endTime";
}
return "$startDay $startTime ${day.format(commitment.dueEnd!)} $endTime";
}
String formatDate(DateTime? value) {
if (value == null) {
return "";
}
return DateFormat('d MMM yyyy').format(value);
}
String formatDateTime(DateTime? value) {
if (value == null) {
return "";
}
return DateFormat('d MMM, HH:mm').format(value);
}
/// "3h 20m" from raw minutes.
String formatMinutes(num minutes) {
final int total = minutes.round();
if (total < 60) {
return "${total}m";
}
final int hours = total ~/ 60;
final int remainder = total % 60;
if (remainder == 0) {
return "${hours}h";
}
return "${hours}h ${remainder}m";
}
/// "12:04" from raw seconds — for the proof timer and rest clocks.
String formatClock(int seconds) {
final int safe = seconds < 0 ? 0 : seconds;
final int minutes = safe ~/ 60;
final int remainder = safe % 60;
return "${minutes.toString().padLeft(2, '0')}:${remainder.toString().padLeft(2, '0')}";
}
/// Debt is shown to one decimal — precise enough to move visibly when you
/// clear something, coarse enough not to look like a lie.
String formatDebt(double debt) {
return debt.toStringAsFixed(1);
}
/// "2 days overdue", "Due in 40m", "Window closes in 3h".
String overdueLabel(Commitment commitment) {
if (commitment.dueEnd == null) {
return "";
}
final Duration difference = DateTime.now().difference(commitment.dueEnd!);
if (difference.isNegative) {
final Duration remaining = difference.abs();
if (remaining.inHours < 1) {
return "Closes in ${remaining.inMinutes}m";
}
if (remaining.inDays < 1) {
return "Closes in ${remaining.inHours}h";
}
return "Closes in ${remaining.inDays}d";
}
if (difference.inHours < 1) {
return "${difference.inMinutes}m overdue";
}
if (difference.inDays < 1) {
return "${difference.inHours}h overdue";
}
if (difference.inDays == 1) {
return "1 day overdue";
}
return "${difference.inDays} days overdue";
}
Color standingColor(Standing standing) {
switch (standing) {
case Standing.Good:
return colorStandingGood;
case Standing.Warned:
return colorStandingWarned;
case Standing.Grounded:
return colorStandingGrounded;
case Standing.Lockdown:
return colorStandingLockdown;
}
}
Color standingBackground(Standing standing) {
switch (standing) {
case Standing.Good:
return colorStandingGoodBg;
case Standing.Warned:
return colorStandingWarnedBg;
case Standing.Grounded:
return colorStandingGroundedBg;
case Standing.Lockdown:
return colorStandingLockdownBg;
}
}
Color classColor(CommitmentClass value) {
switch (value) {
case CommitmentClass.NonNegotiable:
return colorClassNonNegotiable;
case CommitmentClass.Standard:
return colorClassStandard;
case CommitmentClass.Elective:
return colorClassElective;
}
}
Color classBackground(CommitmentClass value) {
switch (value) {
case CommitmentClass.NonNegotiable:
return colorClassNonNegotiableBg;
case CommitmentClass.Standard:
return colorClassStandardBg;
case CommitmentClass.Elective:
return colorClassElectiveBg;
}
}
/// The weekday name for a 1..7 index.
String weekdayName(int weekday) {
const List<String> names = <String>[
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
];
if (weekday < 1 || weekday > 7) {
return "";
}
return names[weekday - 1];
}
/// "7pm" for an hour-of-day index.
String hourLabel(int hour) {
if (hour < 0) {
return "";
}
if (hour == 0) {
return "midnight";
}
if (hour < 12) {
return "${hour}am";
}
if (hour == 12) {
return "noon";
}
return "${hour - 12}pm";
}