- Drift 21 tables (8 catalog + 11 user + habit_dose_variants + meta_kv) with R1~R10 CHECK constraints and 19 indexes - 8 hand-crafted seed JSON catalogs in app/assets/seed/ (refs 84, protocols 34, methodologies 21, frame_patterns 30, reward_menu_items 30, break_protocols 8, common_frames 5, diet_patterns 5) - 6 domain functions: recommend_variant, compute_streak, validate_frame_level, active_habit_quota, weekly_minimum_ratio, seed_importer (transactional, idempotent) - 4 vertical-slice Riverpod screens: HabitList, HabitCreate, CheckIn, Streak - 31 unit tests passing; flutter analyze clean - OQ-5 streak semantics: missing entry ≠ explicit blank (missing = end of history; only TrackerValue.blank triggers Never-miss-twice) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
2.3 KiB
Dart
81 lines
2.3 KiB
Dart
import 'package:drift/drift.dart';
|
|
|
|
import '../../../core/id.dart';
|
|
import '../../../core/time.dart';
|
|
import '../app_database.dart';
|
|
import '../tables/user_tables.dart';
|
|
|
|
part 'tracker_dao.g.dart';
|
|
|
|
class TrackerEntryDraft {
|
|
final String habitId;
|
|
final String date; // YYYY-MM-DD
|
|
final String value; // done | blank
|
|
final String? variantId;
|
|
final String? ctxLocation;
|
|
final String? ctxCondition;
|
|
final String? note;
|
|
|
|
const TrackerEntryDraft({
|
|
required this.habitId,
|
|
required this.date,
|
|
required this.value,
|
|
this.variantId,
|
|
this.ctxLocation,
|
|
this.ctxCondition,
|
|
this.note,
|
|
});
|
|
}
|
|
|
|
@DriftAccessor(tables: [TrackerEntries])
|
|
class TrackerDao extends DatabaseAccessor<AppDatabase> with _$TrackerDaoMixin {
|
|
TrackerDao(super.db);
|
|
|
|
Future<String> recordCheckIn(TrackerEntryDraft draft) async {
|
|
final id = generateUlid('te');
|
|
await into(trackerEntries).insert(TrackerEntriesCompanion.insert(
|
|
id: id,
|
|
habitId: draft.habitId,
|
|
date: draft.date,
|
|
value: draft.value,
|
|
loggedAt: Value(nowKst().toIso8601String()),
|
|
variantId: Value(draft.variantId),
|
|
ctxLocation: Value(draft.ctxLocation),
|
|
ctxCondition: Value(draft.ctxCondition),
|
|
note: Value(draft.note),
|
|
));
|
|
return id;
|
|
}
|
|
|
|
Future<List<TrackerEntry>> entriesForHabit(String habitId) {
|
|
return (select(trackerEntries)
|
|
..where((t) => t.habitId.equals(habitId))
|
|
..orderBy([(t) => OrderingTerm.asc(t.date)]))
|
|
.get();
|
|
}
|
|
|
|
/// Done entries in [start, end) for one user.
|
|
/// [start]/[end] are YYYY-MM-DD strings.
|
|
Future<List<TrackerEntry>> findDoneInRangeForUser({
|
|
required String userId,
|
|
required String startDate,
|
|
required String endDate,
|
|
String? habitId,
|
|
}) async {
|
|
final habitIds = await (select(db.habits)
|
|
..where((t) => t.userId.equals(userId)))
|
|
.map((h) => h.id)
|
|
.get();
|
|
if (habitIds.isEmpty) return const [];
|
|
final query = select(trackerEntries)
|
|
..where((t) => t.habitId.isIn(habitIds))
|
|
..where((t) => t.value.equals('done'))
|
|
..where((t) =>
|
|
t.date.isBiggerOrEqualValue(startDate) & t.date.isSmallerThanValue(endDate));
|
|
if (habitId != null) {
|
|
query.where((t) => t.habitId.equals(habitId));
|
|
}
|
|
return query.get();
|
|
}
|
|
}
|