summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/ShortcutsViewController.swift
blob: 953639dca3b1bc8737e5d1220b4dae46fab60041 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
//  ShortcutsViewController.swift
//  MullvadVPN
//
//  Created by Nikolay Davydov on 20.08.2022.
//  Copyright © 2022 Mullvad VPN AB. All rights reserved.
//

import IntentsUI
import UIKit

final class ShortcutsViewController: UITableViewController,
    ShortcutsDataSourceDelegate,
    INUIAddVoiceShortcutViewControllerDelegate,
    INUIEditVoiceShortcutViewControllerDelegate
{
    private let dataSource = ShortcutsDataSource()

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    init() {
        super.init(style: .grouped)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.backgroundColor = .secondaryColor
        tableView.separatorColor = .secondaryColor
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 60

        dataSource.tableView = tableView
        dataSource.delegate = self

        navigationItem.title = NSLocalizedString(
            "NAVIGATION_TITLE",
            tableName: "Shortcuts",
            value: "Shortcuts",
            comment: ""
        )
    }

    // MARK: - ShortcutsDataSourceDelegate

    func shortcutsDataSource(
        _ dataSource: ShortcutsDataSource,
        didSelectItem item: ShortcutsDataSource.Item
    ) {
        let controller: UIViewController
        if let voiceShortcut = item.voiceShortcut {
            let editShortcutController = INUIEditVoiceShortcutViewController(
                voiceShortcut: voiceShortcut
            )
            editShortcutController.delegate = self
            controller = editShortcutController
        } else {
            let addShortcutController = INUIAddVoiceShortcutViewController(
                shortcut: item.shortcut
            )
            addShortcutController.delegate = self
            controller = addShortcutController
        }
        controller.modalPresentationStyle = .formSheet
        present(controller, animated: true)
    }

    // MARK: - INUIAddVoiceShortcutViewControllerDelegate

    func addVoiceShortcutViewController(
        _ controller: INUIAddVoiceShortcutViewController,
        didFinishWith voiceShortcut: INVoiceShortcut?,
        error: Error?
    ) {
        if let voiceShortcut = voiceShortcut {
            ShortcutsManager.shared.addVoiceShortcut(voiceShortcut)
        }
        controller.dismiss(animated: true)
    }

    func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
        controller.dismiss(animated: true)
    }

    // MARK: - INUIEditVoiceShortcutViewControllerDelegate

    func editVoiceShortcutViewController(
        _ controller: INUIEditVoiceShortcutViewController,
        didUpdate voiceShortcut: INVoiceShortcut?,
        error: Error?
    ) {
        controller.dismiss(animated: true)
    }

    func editVoiceShortcutViewController(
        _ controller: INUIEditVoiceShortcutViewController,
        didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID
    ) {
        ShortcutsManager.shared.deleteVoiceShortcut(
            withIdentifier: deletedVoiceShortcutIdentifier
        )
        controller.dismiss(animated: true)
    }

    func editVoiceShortcutViewControllerDidCancel(
        _ controller: INUIEditVoiceShortcutViewController
    ) {
        controller.dismiss(animated: true)
    }
}