This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathNotificationsViewController.swift
323 lines (273 loc) · 10.6 KB
/
NotificationsViewController.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// NotificationsViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 6/8/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
import IGListKit
import FlatCache
import Squawk
import DropdownTitleView
import ContextMenu
final class NotificationsViewController: BaseListViewController<Int>,
BaseListViewControllerDataSource,
FlatCacheListener,
TabNavRootViewControllerType,
BaseListViewControllerEmptyDataSource,
NewFeaturesSectionControllerDelegate,
InboxFilterControllerListener {
private let modelController: NotificationModelController
private var modelIDs = [String]()
private var newFeaturesController: NewFeaturesController? = NewFeaturesController()
private let inboxFilterController: InboxFilterController
private let navigationTitle = DropdownTitleView()
private var notifications: [NotificationViewModel] {
return modelIDs.compactMap { modelController.githubClient.cache.get(id: $0) }
}
init(modelController: NotificationModelController) {
self.modelController = modelController
self.inboxFilterController = InboxFilterController(client: modelController.githubClient)
super.init(
emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""),
backgroundThreshold: 5 * 60
)
self.dataSource = self
self.emptyDataSource = self
inboxFilterController.announcer.add(listener: self)
updateTitle()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
makeBackBarItemEmpty()
resetRightBarItem()
navigationController?.tabBarItem.badgeColor = Styles.Colors.Red.medium.color
navigationItem.titleView = navigationTitle
navigationTitle.addTouchEffect()
navigationTitle.titleFont = UIFont.preferredFont(forTextStyle: .headline)
navigationTitle.addTarget(self, action: #selector(onNavigationTitle), for: .touchUpInside)
// @ Fetches for new features
newFeaturesController?.fetch { [weak self] in
self?.update()
}
}
override func fetch(page: Int?) {
let type = inboxFilterController.selected.type
let width = view.safeContentWidth(with: feed.collectionView)
let fetchPage = page ?? 1
// append model ids if paging
let append = page != nil
// do not animate paged updates, only head loads
let animated = page == nil && trueUnlessReduceMotionEnabled
switch type {
case .unread, .all, .repo:
let repo: Repository?
if case .repo(let owner, let name) = type {
repo = Repository(owner: owner, name: name)
} else {
repo = nil
}
modelController.fetchNotifications(
repo: repo,
// always fetch all for repos, otherwise use selection
all: repo != nil || type == .all,
page: fetchPage,
width: width
) { [weak self] result in
self?.handle(result: result, append: append, animated: animated, page: fetchPage)
}
case .assigned:
modelController.fetch(for: .assigned, page: fetchPage, width: width) { [weak self] result in
self?.handle(result: result, append: append, animated: animated, page: fetchPage)
}
case .created:
modelController.fetch(for: .created, page: fetchPage, width: width) { [weak self] result in
self?.handle(result: result, append: append, animated: animated, page: fetchPage)
}
case .mentioned:
modelController.fetch(for: .mentioned, page: fetchPage, width: width) { [weak self] result in
self?.handle(result: result, append: append, animated: animated, page: fetchPage)
}
}
}
private func handle<T: Cachable>(
result: Result<([T], Int?)>,
append: Bool,
animated: Bool,
page: Int
) {
switch result {
case .success(let models, let next):
var ids = [String]()
models.forEach {
modelController.githubClient.cache.add(listener: self, value: $0)
ids.append($0.id)
}
rebuildAndUpdate(ids: ids, append: append, page: next, animated: animated)
case .error(let err):
error(animated: trueUnlessReduceMotionEnabled)
Squawk.show(error: err)
}
// set after updating so self.models has already been changed
updateUnreadState()
}
private func updateUnreadState() {
var unread = 0
for id in modelIDs {
guard let model = modelController.githubClient.cache.get(id: id) as NotificationViewModel?,
!model.read
else { continue }
unread += 1
}
let hasUnread = unread > 0
navigationItem.rightBarButtonItem?.isEnabled = hasUnread
navigationController?.tabBarItem.badgeValue = hasUnread ? "\(unread)" : nil
BadgeNotifications.updateBadge(count: unread)
}
func resetRightBarItem(updatingState updateState: Bool = true) {
let item = UIBarButtonItem(
image: UIImage(named: "check"),
style: .plain,
target: self,
action: #selector(onMarkAll)
)
item.accessibilityLabel = NSLocalizedString("Mark notifications read", comment: "")
navigationItem.rightBarButtonItem = item
if updateState {
updateUnreadState()
}
}
@objc private func onMarkAll() {
let type = inboxFilterController.selected.type
let message: String
switch type {
case .unread, .all:
message = NSLocalizedString("Mark all notifications as read?", comment: "")
case .assigned, .created, .mentioned:
message = NSLocalizedString(
"Mark all notifications as read? This includes notifications not currently visible.",
comment: ""
)
case let .repo(owner, name):
let messageFormat = NSLocalizedString("Mark %@/%@ notifications as read?", comment: "")
message = String(format: messageFormat, owner, name)
}
let alert = UIAlertController.configured(
title: NSLocalizedString("Notifications", comment: ""),
message: message,
preferredStyle: .alert
)
alert.addActions([
UIAlertAction(
title: Constants.Strings.markRead,
style: .destructive,
handler: { [weak self] _ in
self?.markRead(type: type)
}),
AlertAction.cancel()
])
present(alert, animated: trueUnlessReduceMotionEnabled)
}
private func markRead(type: InboxFilterModel.FilterType) {
self.setRightBarItemSpinning()
let block: (Bool) -> Void = { success in
let generator = UINotificationFeedbackGenerator()
if success {
generator.notificationOccurred(.success)
// clear all badges
BadgeNotifications.updateBadge(count: 0)
// change the spinner to the mark all item
// don't update state here; it is managed by `fetch`
self.resetRightBarItem(updatingState: false)
} else {
generator.notificationOccurred(.error)
}
self.fetch(page: nil)
// "mark all" is an engaging action, system prompt on it
RatingController.prompt(.system)
}
switch type {
case .unread, .all, .assigned, .created, .mentioned:
modelController.markAllNotifications(completion: block)
case let .repo(owner, name):
modelController.markRepoNotifications(owner: owner, name: name, completion: block)
}
}
private func rebuildAndUpdate(
ids: [String],
append: Bool,
page: Int?,
animated: Bool
) {
if append {
modelIDs += ids
} else {
modelIDs = ids
}
update(page: page, animated: animated)
}
private func updateTitle() {
navigationTitle.configure(
title: inboxFilterController.selected.type.title,
subtitle: inboxFilterController.selected.type.subtitle
)
}
@objc private func onNavigationTitle() {
inboxFilterController.showMenu(from: self)
}
// MARK: BaseListViewControllerDataSource
func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
var models = [ListSwiftPair]()
if let markdown = newFeaturesController?.latestMarkdown {
models.append(ListSwiftPair.pair(markdown, { [weak self] in
NewFeaturesSectionController(delegate: self)
}))
}
models += modelIDs.compactMap { id in
if let model = modelController.githubClient.cache.get(id: id) as NotificationViewModel? {
return ListSwiftPair.pair(model) { [modelController] in
NotificationSectionController(modelController: modelController)
}
} else if let model = modelController.githubClient.cache.get(id: id) as InboxDashboardModel? {
return ListSwiftPair.pair(model) {
InboxDashboardSectionController()
}
}
return nil
}
return models
}
// MARK: BaseListViewControllerEmptyDataSource
func emptyModel(for adapter: ListSwiftAdapter) -> ListSwiftPair {
let layoutInsets = view.safeAreaInsets
return ListSwiftPair.pair("empty-notification-value", {
return NoNewNotificationSectionController(layoutInsets: layoutInsets)
})
}
// MARK: FlatCacheListener
func flatCacheDidUpdate(cache: FlatCache, update: FlatCache.Update) {
self.update(animated: trueUnlessReduceMotionEnabled)
updateUnreadState()
}
// MARK: TabNavRootViewControllerType
func didSingleTapTab() {
feed.collectionView.scrollToTop(animated: true)
}
func didDoubleTapTab() {
didSingleTapTab()
}
// MARK: NewFeaturesSectionControllerDelegate
func didTapClose(for sectionController: NewFeaturesSectionController) {
newFeaturesController = nil
update()
}
// MARK: InboxFilterControllerListener
func didUpdateSelectedFilter(for controller: InboxFilterController) {
updateTitle()
feed.refreshHead()
}
}