Skip to content

Commit

Permalink
correctly process immediate; also clear cache when updating goals
Browse files Browse the repository at this point in the history
  • Loading branch information
lalomartins committed Jul 7, 2020
1 parent 7d69983 commit 8fdebcf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,10 @@ class WeeklyGoalsDatabase extends _$WeeklyGoalsDatabase {
.watch();

Future<void> refreshGoals() async {
final newGoals = await Goal.goalsAsOf(db: this);
final newGoals = await Goal.goalsAsOf(db: this, clearCache: true);
await delete(cachedGoals).go();
for (final goal in newGoals) {
into(cachedGoals).insert(goal);
}
print('cached goals updated');
}
}
9 changes: 6 additions & 3 deletions lib/model/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
import 'package:yaml/yaml.dart';

import '../config.dart';
import '../date_util.dart';
import '../db.dart';

final _memCache = ExpireCache<DateTime, List<Goal>>(expireDuration: Duration(minutes: 15), sizeLimit: 52);
Expand Down Expand Up @@ -64,15 +65,17 @@ class Goal extends CachedGoal {
return map;
}

static Future<List<Goal>> goalsAsOf({DateTime when, @required WeeklyGoalsDatabase db}) async {
if (_memCache.containsKey(when)) return _memCache.get(when);
static Future<List<Goal>> goalsAsOf({DateTime when, @required WeeklyGoalsDatabase db, clearCache: false}) async {
if (clearCache) _memCache.clear();
else if (_memCache.containsKey(when)) return _memCache.get(when);

final events = await db.getEvents(type: 'set goal', until: when);
final map = Map<String, Map<String, Goal>>();
final goals = List<Goal>();
final effectDate = when ?? startOfWeek(weeksAgo: -1, midnight: true);
for (final event in events) {
final YamlMap data = loadYaml(event.additional);
if (when != null && data['immediate'] == false && event.timestamp.add(Duration(days: 7)).isAfter(when)) continue;
if (data['immediate'] == false && event.timestamp.add(Duration(days: 7)).isAfter(effectDate)) continue;
final goal = map.putIfAbsent(data['category'], () => Map()).putIfAbsent(data['name'], () {
final goal = Goal(
category: data['category'],
Expand Down

0 comments on commit 8fdebcf

Please sign in to comment.