-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathCustomListViewController.swift
198 lines (170 loc) · 6.35 KB
/
CustomListViewController.swift
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// CustomListViewController.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-02-15.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import Combine
import MullvadSettings
import UIKit
protocol CustomListViewControllerDelegate: AnyObject {
func customListDidSave(_ list: CustomList)
func customListDidDelete(_ list: CustomList)
func showLocations(_ list: CustomList)
}
class CustomListViewController: UIViewController {
typealias DataSource = UITableViewDiffableDataSource<CustomListSectionIdentifier, CustomListItemIdentifier>
private let interactor: any CustomListInteractorProtocol
private let tableView = UITableView(frame: .zero, style: .insetGrouped)
private let subject: CurrentValueSubject<CustomListViewModel, Never>
private var cancellables = Set<AnyCancellable>()
private var dataSource: DataSource?
private let alertPresenter: AlertPresenter
private var validationErrors: Set<CustomListFieldValidationError> = []
private lazy var cellConfiguration: CustomListCellConfiguration = {
CustomListCellConfiguration(tableView: tableView, subject: subject)
}()
private lazy var dataSourceConfiguration: CustomListDataSourceConfiguration? = {
dataSource.flatMap { dataSource in
CustomListDataSourceConfiguration(dataSource: dataSource)
}
}()
lazy var saveBarButton: UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(
title: NSLocalizedString(
"CUSTOM_LIST_NAVIGATION_SAVE_BUTTON",
tableName: "CustomLists",
value: "Save",
comment: ""
),
primaryAction: UIAction { [weak self] _ in
self?.onSave()
}
)
barButtonItem.style = .done
return barButtonItem
}()
weak var delegate: (any CustomListViewControllerDelegate)?
init(
interactor: any CustomListInteractorProtocol,
subject: CurrentValueSubject<CustomListViewModel, Never>,
alertPresenter: AlertPresenter
) {
self.subject = subject
self.interactor = interactor
self.alertPresenter = alertPresenter
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.directionalLayoutMargins = UIMetrics.contentLayoutMargins
view.backgroundColor = .secondaryColor
isModalInPresentation = true
addSubviews()
configureNavigationItem()
configureDataSource()
configureTableView()
subject.sink { [weak self] viewModel in
self?.saveBarButton.isEnabled = !viewModel.name.isEmpty
self?.validationErrors.removeAll()
}.store(in: &cancellables)
}
private func configureNavigationItem() {
navigationItem.rightBarButtonItem = saveBarButton
}
private func configureTableView() {
tableView.delegate = dataSourceConfiguration
tableView.backgroundColor = .secondaryColor
tableView.registerReusableViews(from: CustomListItemIdentifier.CellIdentifier.self)
}
private func configureDataSource() {
cellConfiguration.onDelete = { [weak self] in
self?.onDelete()
}
dataSource = DataSource(
tableView: tableView,
cellProvider: { [weak self] _, indexPath, itemIdentifier in
guard let self else { return nil }
return cellConfiguration.dequeueCell(
at: indexPath,
for: itemIdentifier,
validationErrors: self.validationErrors
)
}
)
dataSourceConfiguration?.didSelectItem = { [weak self] item in
guard let self else { return }
self.view.endEditing(false)
switch item {
case .name, .deleteList:
break
case .addLocations, .editLocations:
delegate?.showLocations(self.subject.value.customList)
}
}
dataSourceConfiguration?.updateDataSource(
sections: subject.value.tableSections,
validationErrors: validationErrors,
animated: false
)
}
private func addSubviews() {
view.addConstrainedSubviews([tableView]) {
tableView.pinEdgesToSuperview()
}
}
private func onSave() {
do {
try interactor.save(viewModel: subject.value)
delegate?.customListDidSave(subject.value.customList)
} catch {
validationErrors.insert(.name)
dataSourceConfiguration?.set(validationErrors: validationErrors)
}
}
private func onDelete() {
let message = NSMutableAttributedString(
markdownString: NSLocalizedString(
"CUSTOM_LISTS_DELETE_PROMPT",
tableName: "CustomLists",
value: "Do you want to delete the list **\(subject.value.name)**?",
comment: ""
),
options: MarkdownStylingOptions(font: .preferredFont(forTextStyle: .body))
)
let presentation = AlertPresentation(
id: "api-custom-lists-delete-list-alert",
icon: .alert,
attributedMessage: message,
buttons: [
AlertAction(
title: NSLocalizedString(
"CUSTOM_LISTS_DELETE_BUTTON",
tableName: "CustomLists",
value: "Delete list",
comment: ""
),
style: .destructive,
handler: {
self.interactor.delete(id: self.subject.value.id)
self.delegate?.customListDidDelete(self.subject.value.customList)
}
),
AlertAction(
title: NSLocalizedString(
"CUSTOM_LISTS_CANCEL_BUTTON",
tableName: "CustomLists",
value: "Cancel",
comment: ""
),
style: .default
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
}