summaryrefslogtreecommitdiff
path: root/source/ui/Nav.mc
blob: 222386a8094981952bea1788231ebaf0e45a16bd (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
// Author:  cirakg & claude ai
// Summary: Navigation between screens (README 13.2): opening entries, asking for the PIN first when needed, the menu.

import Toybox.Lang;
import Toybox.WatchUi;

module Nav {

    // Open an entry from the list: its detail screen, or the PIN screen first
    // when it is locked and the session is locked.
    function openEntry(id as String) as Void {
        var vault = AppState.theVault();
        var header = vault.header(id);
        if (header == null) {
            showInfo("Damaged entry", ["Damaged entry " + id + ". Delete it and add it again."]);
            return;
        }
        if (header.locked && !vault.session.isUnlocked()) {
            var action = new OpenAfterPin(id);
            WatchUi.pushView(new PinView(PinView.MODE_UNLOCK, action.method(:run)), new PinDelegate(), WatchUi.SLIDE_LEFT);
            return;
        }
        WatchUi.pushView(detailView(header), detailDelegate(header), WatchUi.SLIDE_LEFT);
    }

    function detailView(header as Entry) as WatchUi.View {
        if (header.kind == Entry.KIND_OTP) {
            return new OtpView(header);
        }
        return new CodeView(header);
    }

    function detailDelegate(header as Entry) as WatchUi.InputDelegate {
        if (header.kind == Entry.KIND_OTP) {
            return new OtpDelegate();
        }
        return new CodeDelegate();
    }

    // A simple text screen.
    function showInfo(title as String, lines as Array<String>) as Void {
        WatchUi.pushView(new InfoView(title, lines, "info"), new InfoDelegate(), WatchUi.SLIDE_LEFT);
    }

    function openMenu() as Void {
        WatchUi.pushView(MainMenu.build(), new MainMenuDelegate(), WatchUi.SLIDE_UP);
    }

    // Opens an entry once the PIN screen succeeded (replacing the PIN screen).
    class OpenAfterPin {
        private var _id as String;

        function initialize(id as String) {
            _id = id;
        }

        function run() as Void {
            var header = AppState.theVault().header(_id);
            if (header == null) {
                WatchUi.popView(WatchUi.SLIDE_RIGHT);
                return;
            }
            WatchUi.switchToView(detailView(header), detailDelegate(header), WatchUi.SLIDE_LEFT);
        }
    }
}