App type changed to widget for glance support. GlanceView shows event type, date/time, and street+housenumber (or coordinates as fallback). Addresses are cached in Event storage on first view in history. Glance-required modules annotated with (:glance). Address distance threshold raised to 70m. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
83 lines
2.5 KiB
MonkeyC
83 lines
2.5 KiB
MonkeyC
import Toybox.Application;
|
|
import Toybox.Lang;
|
|
import Toybox.Time;
|
|
|
|
// Persists events in Application.Storage as an array of dictionaries,
|
|
// ordered oldest → newest. Prunes entries older than Config.RETENTION_SEC
|
|
// whenever loaded.
|
|
(:glance)
|
|
module EventStore {
|
|
|
|
const KEY = "events";
|
|
|
|
function getAll() as Array<Event> {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
if (raw == null || !(raw instanceof Array)) {
|
|
return [];
|
|
}
|
|
var out = [];
|
|
for (var i = 0; i < raw.size(); i++) {
|
|
out.add(Event.fromDict(raw[i] as Dictionary));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function add(event as Event) as Void {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
var arr = (raw instanceof Array) ? raw : [];
|
|
arr.add(event.toDict());
|
|
Application.Storage.setValue(KEY, arr);
|
|
}
|
|
|
|
function deleteLast() as Boolean {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
if (!(raw instanceof Array) || raw.size() == 0) {
|
|
return false;
|
|
}
|
|
raw = raw.slice(0, raw.size() - 1);
|
|
Application.Storage.setValue(KEY, raw);
|
|
return true;
|
|
}
|
|
|
|
function count() as Number {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
return (raw instanceof Array) ? raw.size() : 0;
|
|
}
|
|
|
|
function latest() as Event or Null {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
if (!(raw instanceof Array) || raw.size() == 0) {
|
|
return null;
|
|
}
|
|
return Event.fromDict(raw[raw.size() - 1] as Dictionary);
|
|
}
|
|
|
|
function updateAt(index as Number, event as Event) as Void {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
if (!(raw instanceof Array) || index < 0 || index >= raw.size()) {
|
|
return;
|
|
}
|
|
raw[index] = event.toDict();
|
|
Application.Storage.setValue(KEY, raw);
|
|
}
|
|
|
|
// Drops events with timestamp < (now - retention).
|
|
function pruneOld() as Void {
|
|
var raw = Application.Storage.getValue(KEY);
|
|
if (!(raw instanceof Array) || raw.size() == 0) {
|
|
return;
|
|
}
|
|
var cutoff = Time.now().value() - Config.RETENTION_SEC;
|
|
var kept = [];
|
|
for (var i = 0; i < raw.size(); i++) {
|
|
var d = raw[i] as Dictionary;
|
|
var ts = d["ts"];
|
|
if (ts != null && (ts as Number) >= cutoff) {
|
|
kept.add(d);
|
|
}
|
|
}
|
|
if (kept.size() != raw.size()) {
|
|
Application.Storage.setValue(KEY, kept);
|
|
}
|
|
}
|
|
}
|