summaryrefslogtreecommitdiff
path: root/source/settings/Results.mc
blob: 37c932fd86f003a0cbd8e7d520b392bd67d1b1fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Author:  cirakg & claude ai
// Summary: The latest settings results (README 10.3): kept in storage for the results screen, with an unseen flag for the list.

import Toybox.Application;
import Toybox.Lang;

module Results {

    const KEY = "res";
    const MAX_LINES = 60;

    // Replace the stored results with these lines (marked unseen).
    function save(lines as Array<String>) as Void {
        var kept = lines.size() > MAX_LINES ? lines.slice(0, MAX_LINES) as Array<String> : lines;
        Application.Storage.setValue(KEY, {"l" => kept, "u" => true} as Dictionary<Application.Storage.KeyType, Application.Storage.ValueType>);
    }

    // The stored lines, or a short explanation when there are none.
    function lines() as Array<String> {
        var stored = Application.Storage.getValue(KEY);
        if (stored instanceof Dictionary && stored["l"] instanceof Array) {
            return stored["l"] as Array<String>;
        }
        return ["Nothing yet. Results appear here after you import or change entries in the phone settings."];
    }

    function unseen() as Boolean {
        var stored = Application.Storage.getValue(KEY);
        return stored instanceof Dictionary && isTrue(stored["u"]);
    }

    function markSeen() as Void {
        var stored = Application.Storage.getValue(KEY);
        if (stored instanceof Dictionary && isTrue(stored["u"])) {
            stored["u"] = false;
            Application.Storage.setValue(KEY, stored as Dictionary<Application.Storage.KeyType, Application.Storage.ValueType>);
        }
    }

    function isTrue(value as Application.Storage.ValueType) as Boolean {
        return value instanceof Boolean && value;
    }
}