Files
GroundedHelper/frontend/lib/Grounded/see/commitment/ViewNewCommitment.dart
alvocool 1a5867a98f Add routines module and full goal management
Routines: workout, meditation and must-do routines with timed, rep and
tick steps, sets and rest. A library screen, a builder with templates
and drag-to-reorder steps, and a full-screen player that auto-advances
timed phases, counts backgrounding and logs broken chains as partial.

Goals: a goal editor (deadline, colour, category, default class, archive),
routines linked to goals, and a goal field on tasks and routines. Tasks
added from inside a goal are now actually attached to it via parentId.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KhQh614DMeYoQEH9SoMRUw
2026-11-02 08:45:41 +03:00

118 lines
3.7 KiB
Dart

import '../../about/external/data/Commitment.dart';
import '../../about/external/data/pages/request/CommitmentsRequest.dart';
import '../../about/external/data/pages/request/HistoryRequest.dart';
import '../../about/external/data/pages/request/PageAndSort.dart';
import '../../about/external/data/pages/request/Pageable.dart';
import '../../about/external/data/pages/request/Sort.dart';
import '../../about/external/data/pages/response/CommitmentPage.dart';
import '../../about/external/initial/CommitmentRequest.dart';
import '../../about/internal/application/CapacityProfile.dart';
import '../../utils/CapacityEngine.dart';
import '../../utils/ObjectConvertors.dart';
import '../parent/ParentViewModel.dart';
import 'ConnectNewCommitment.dart';
class ViewNewCommitment extends ParentViewModel {
ConnectNewCommitment connection;
ViewNewCommitment(super.context, this.connection);
/// Surfaces the learned multiplier for the category so the estimate field
/// can show what the app actually expects, rather than silently overriding.
void resolveMultiplier(String category) async {
final CapacityProfile profile =
await getDataManager().getCapacityProfile();
connection.onMultiplierResolved(profile.multiplierFor(category));
}
/// Goals for the picker — loaded on tap, never up front.
void loadGoals() async {
if (!await hasNetwork(() => loadGoals())) return;
showLoading("Loading goals");
try {
final response = await getDataManager().getMyGoals(HistoryRequest(
query: PageAndSort(
sort: Sort('desc', 'startDate'),
page: Pageable(0, 0, 50, 0),
),
));
closeLoading();
connection.onGoalsReady(getGoalList(response.data));
} catch (e) {
handleError(e, () => loadGoals(), () => dismissError(), "Retry");
}
}
/// The capacity gate. Chronic overdue is usually overcommitment misdiagnosed
/// as laziness, so the plan is checked against what history says actually
/// gets done before anything is accepted.
void save(CommitmentRequest request, Commitment candidate) async {
if (!await hasNetwork(() => save(request, candidate))) return;
showLoading("Checking your day");
try {
final DateTime day = candidate.dueStart ?? DateTime.now();
final response = await getDataManager().getTodayPlan(CommitmentsRequest(
day: day.toIso8601String(),
query: PageAndSort(
sort: Sort('asc', 'dueStart'),
page: Pageable(0, 0, 100, 0),
),
));
final CommitmentPage page = CommitmentPage.fromJson(response.data);
final CapacityProfile profile =
await getDataManager().getCapacityProfile();
final List<Commitment> proposed = <Commitment>[
...page.content,
candidate,
];
final CapacityVerdict verdict =
CapacityEngine.check(proposed, profile, day.weekday);
if (verdict.blocked) {
closeLoading();
connection.onCapacityBlocked(verdict);
return;
}
await getDataManager().saveCommitmentEntry(request);
closeLoading();
connection.onSaved();
} catch (e) {
handleError(
e, () => save(request, candidate), () => dismissError(), "Retry");
}
}
/// Saving past a capacity block, which is only reachable after the user has
/// seen exactly how much they are over by.
void saveAnyway(CommitmentRequest request) async {
if (!await hasNetwork(() => saveAnyway(request))) return;
showLoading("Saving");
try {
await getDataManager().saveCommitmentEntry(request);
closeLoading();
connection.onSaved();
} catch (e) {
handleError(e, () => saveAnyway(request), () => dismissError(), "Retry");
}
}
}