Skip to content

Commit

Permalink
refresh cached goals when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
lalomartins committed Jul 7, 2020
1 parent 011b5d5 commit 7d69983
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/add_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class _AddEventFormState extends EventEditorState<AddEventForm> {
backgroundColor: Theme.of(context).primaryColor,
)..show(context);
try {
await Provider.of<WeeklyGoalsDatabase>(context).createEventFromMap(event);
final db = Provider.of<WeeklyGoalsDatabase>(context);
await db.createEventFromMap(event);
if (event['type'] == 'set goal') await db.refreshGoals();
savingBar.dismiss();
Navigator.pop(context);
Flushbar(
Expand Down
17 changes: 13 additions & 4 deletions lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ class WeeklyGoalsDatabase extends _$WeeklyGoalsDatabase {
data['uuid'] = uuid.v1();
}
if (data['synced'] != null) {
data = data.map((k, v) => MapEntry(k, v));
if (!copied) data = data.map((k, v) => MapEntry(k, v));
copied = true;
data['synced'] = null;
}
if (data['timestamp'] is DateTime) {
data = data.map((k, v) => MapEntry(k, v));
if (!copied) data = data.map((k, v) => MapEntry(k, v));
copied = true;
data['timestamp'] = (data['timestamp'] as DateTime).millisecondsSinceEpoch;
}
Expand All @@ -169,10 +169,19 @@ class WeeklyGoalsDatabase extends _$WeeklyGoalsDatabase {
Future<void> updateEvent(Event event) => update(events).replace(event);
Future<void> upsertEvent(EventsCompanion event) => into(events).insertOnConflictUpdate(event);

Future<void> deleteEvent(String uuid) => (delete(events)..where((e) => e.uuid.equals(uuid))).go();

Stream<List<Goal>> watchCurrentGoals() =>
(select(cachedGoals)..orderBy([(u) => OrderingTerm(expression: u.name, mode: OrderingMode.asc)]))
.map((cached) => Goal.copy(cached))
.watch();

Future<void> deleteEvent(String uuid) => (delete(events)..where((e) => e.uuid.equals(uuid))).go();

Future<void> refreshGoals() async {
final newGoals = await Goal.goalsAsOf(db: this);
await delete(cachedGoals).go();
for (final goal in newGoals) {
into(cachedGoals).insert(goal);
}
print('cached goals updated');
}
}
12 changes: 9 additions & 3 deletions lib/edit_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ class _EditEventFormState extends EventEditorState<EditEventForm> {
try {
final data = event.map((k, v) => MapEntry(k, v));
data['timestamp'] = event['timestamp'].millisecondsSinceEpoch;
await Provider.of<WeeklyGoalsDatabase>(context).updateEvent(Event.fromJson(data));
final db = Provider.of<WeeklyGoalsDatabase>(context);
await db.updateEvent(Event.fromJson(data));
if (event['type'] == 'set goal' || widget.event.type == 'set goal') await db.refreshGoals();
savingBar.dismiss();
Navigator.pop(context);
Flushbar(
Expand Down Expand Up @@ -159,7 +161,9 @@ class _EditEventFormState extends EventEditorState<EditEventForm> {
)..show(context);
final savedEvent = widget.event;
try {
await Provider.of<WeeklyGoalsDatabase>(context).deleteEvent(widget.event.uuid);
final db = Provider.of<WeeklyGoalsDatabase>(context);
await db.deleteEvent(widget.event.uuid);
if (widget.event.type == 'set goal') await db.refreshGoals();
deletingBar.dismiss();
Navigator.pop(context);
Flushbar(
Expand Down Expand Up @@ -208,7 +212,9 @@ class _RestoreButton extends StatelessWidget {
backgroundColor: Theme.of(context).primaryColor,
)..show(context);
try {
await Provider.of<WeeklyGoalsDatabase>(context).createEventFromMap(savedEvent.toJson());
final db = Provider.of<WeeklyGoalsDatabase>(context);
await db.createEventFromMap(savedEvent.toJson());
if (savedEvent.type == 'set goal') await db.refreshGoals();
restoringBar.dismiss();
Flushbar(
message: 'Restored',
Expand Down
2 changes: 1 addition & 1 deletion lib/model/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Goal extends CachedGoal {
final goals = List<Goal>();
for (final event in events) {
final YamlMap data = loadYaml(event.additional);
if (data['immediate'] == false && event.timestamp.add(Duration(days: 7)).isAfter(when)) continue;
if (when != null && data['immediate'] == false && event.timestamp.add(Duration(days: 7)).isAfter(when)) continue;
final goal = map.putIfAbsent(data['category'], () => Map()).putIfAbsent(data['name'], () {
final goal = Goal(
category: data['category'],
Expand Down
1 change: 1 addition & 0 deletions lib/server_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ServerClient {
}
i++;
}
db.refreshGoals();
print('sync finished');
} finally {
syncing = false;
Expand Down

0 comments on commit 7d69983

Please sign in to comment.