feat: replace json tools with local data controls
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
@@ -168,22 +166,41 @@ class FocusSeedController extends ChangeNotifier {
|
||||
return _commit(current.copyWith(reflections: reflections));
|
||||
}
|
||||
|
||||
Future<bool> importState(String raw) async {
|
||||
try {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! Map<String, dynamic>) {
|
||||
return false;
|
||||
}
|
||||
return _commit(AppState.fromJson(decoded));
|
||||
} on Object {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> clearData() async {
|
||||
return _commit(AppState.starter(defaultHabitTitles: _defaultHabitTitles));
|
||||
}
|
||||
|
||||
Future<bool> clearToday() async {
|
||||
final current = _state;
|
||||
if (current == null) {
|
||||
return false;
|
||||
}
|
||||
final today = todayKey();
|
||||
final habits = current.habits
|
||||
.map(
|
||||
(habit) => habit.copyWith(
|
||||
completedDates: Set<String>.from(habit.completedDates)
|
||||
..remove(today),
|
||||
),
|
||||
)
|
||||
.toList(growable: false);
|
||||
final sessions = current.sessions
|
||||
.where((session) => dateKey(session.completedAt) != today)
|
||||
.toList(growable: false);
|
||||
final reflections = current.reflections
|
||||
.where((reflection) => reflection.dateKey != today)
|
||||
.toList(growable: false);
|
||||
_analytics.track('today_cleared');
|
||||
return _commit(
|
||||
current.copyWith(
|
||||
anchor: current.anchorDateKey == today ? '' : current.anchor,
|
||||
habits: habits,
|
||||
sessions: sessions,
|
||||
reflections: reflections,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _commit(AppState nextState) async {
|
||||
final previousState = _state;
|
||||
_state = nextState;
|
||||
|
||||
Reference in New Issue
Block a user