summaryrefslogtreecommitdiff
path: root/source/settings/SettingsProcessor.mc
blob: cf7684b0ae3b7c75cabe83cd45704195932a9970 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Author:  cirakg & claude ai
// Summary: Runs the settings forms (SettingsJob) on a timer from the app, then stores and shows the results (README 10.3).
//
// Rules (README 10.3):
//   * Runs from onSettingsChanged, at every start (after the first frame)
//     and after the PIN is entered (which may let "Encrypt names" finish).
//   * The work itself is SettingsJob; this class only drives it on a timer
//     (one step per tick), and when forms were looked at, stores the
//     results lines and opens the results screen.
//   * New settings arriving while a run is going start another run when it
//     ends, so nothing is missed.
// Only the full app runs this, never the glance.

import Toybox.Lang;
import Toybox.WatchUi;

class SettingsProcessor {

    private var _job as SettingsJob? = null;
    // The job's timer driver (kept so it isn't collected while it runs).
    var runner as Jobs.JobRunner? = null;
    private var _again as Boolean = false;

    function initialize() {
    }

    function isBusy() as Boolean {
        return _job != null;
    }

    // "Importing… 40%" or "Applying settings…" while a run goes, else null.
    function busyText() as String? {
        var job = _job;
        return job != null && job.forms > 0 ? job.busyText() : null;
    }

    // Start a run if any form asks for one (or the About fields are stale).
    function start() as Void {
        if (AppState.vault == null) {
            return;
        }
        if (_job != null) {
            _again = true;
            return;
        }
        var vault = AppState.theVault();
        var store = new Forms.PropertyStore();
        if (!SettingsJob.hasWork(vault, store)) {
            return;
        }
        var job = new SettingsJob(vault, store, Clock.nowUnix(0));
        _job = job;
        var r = new Jobs.JobRunner(job, 1, method(:onDone));
        runner = r;
        r.start();
        WatchUi.requestUpdate();
    }

    function onDone() as Void {
        var job = _job as SettingsJob;
        _job = null;
        runner = null;
        if (job.changed) {
            AppState.theCodes().clear();
        }
        if (job.forms > 0) {
            var lines = job.lines;
            var waiting = ChangeQueue.count(AppState.theVault());
            if (waiting > 0) {
                lines.add(waiting + (waiting == 1 ? " change waits" : " changes wait")
                    + " for your PIN: menu, Phone changes.");
            }
            Results.save(lines);
            WatchUi.pushView(new InfoView("Results", lines, "results"), new InfoDelegate(), WatchUi.SLIDE_UP);
            Results.markSeen();
        }
        WatchUi.requestUpdate();
        if (_again) {
            _again = false;
            start();
        }
    }
}