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 pathNotificationSectionController.swift
128 lines (113 loc) · 4.56 KB
/
NotificationSectionController.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
//
// NotificationSectionController.swift
// Freetime
//
// Created by Ryan Nystrom on 6/8/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import IGListKit
import GitHubAPI
import Squawk
final class NotificationSectionController: ListSwiftSectionController<NotificationViewModel>, NotificationCellDelegate {
private let modelController: NotificationModelController
private let generator = UIImpactFeedbackGenerator()
init(modelController: NotificationModelController) {
self.modelController = modelController
super.init()
}
override func createBinders(from value: NotificationViewModel) -> [ListBinder] {
return [
binder(
value,
cellType: ListCellType.class(NotificationCell.self),
size: {
return $0.collection.cellSize(with:
$0.value.title.viewSize(in: $0.collection.safeContentWidth()).height
)
},
configure: { [weak self] in
$0.configure(with: $1.value, delegate: self)
},
didSelect: { [weak self] context in
self?.showIssue(model: context.value)
})
]
}
func didTapRead(cell: NotificationCell) {
guard
let id = value?.id,
let model = modelController.githubClient.cache.get(id: id) as NotificationViewModel?,
!model.read
else {
return
}
cell.animateRead()
generator.impactOccurred()
modelController.markNotificationRead(id: id)
}
func didTapWatch(cell: NotificationCell) {
guard let value = self.value else { return }
modelController.toggleWatch(notification: value)
}
func didTapMore(cell: NotificationCell, sender: UIView) {
guard let value = self.value else { return }
let alert = UIAlertController.configured(preferredStyle: .actionSheet)
alert.addActions([
viewController?.action(owner: value.owner, icon: #imageLiteral(resourceName: "organization")),
viewController?.action(
owner: value.owner,
repo: value.repo,
icon: #imageLiteral(resourceName: "repo"),
client: modelController.githubClient
),
AlertAction.cancel()
])
alert.popoverPresentationController?.setSourceView(sender)
viewController?.present(alert, animated: trueUnlessReduceMotionEnabled)
}
private func showIssue(model: NotificationViewModel) {
if model.type == .ci_activity {
guard let url = URL(string: "https://github.com/" + model.owner + "/" + model.repo + "/actions" ) else { return }
viewController?.presentSafari(url: url)
} else {
if NotificationModelController.readOnOpen {
modelController.markNotificationRead(id: model.id)
}
BadgeNotifications.clear(for: model)
guard model.type != .securityVulnerability || model.type != .ci_activity else {
viewController?.route_push(to: RepositoryViewController(
client: modelController.githubClient,
repo: RepositoryDetails(owner: model.owner, name: model.repo)
))
return
}
switch model.number {
case .hash(let hash):
viewController?.presentCommit(owner: model.owner, repo: model.repo, hash: hash)
case .number(let number):
viewController?.route_detail(to: IssuesViewController(
client: modelController.githubClient,
model: IssueDetailsModel(owner: model.owner, repo: model.repo, number: number),
scrollToBottom: true
))
case .release(let release):
showRelease(release, model: model)
}
}
}
private func showRelease(_ release: String, model: NotificationViewModel) {
modelController.githubClient.client
.send(V3ReleaseRequest(owner: model.owner, repo: model.repo, id: release)) { [weak self] result in
switch result {
case .success(let response):
self?.viewController?.presentRelease(
owner: model.owner,
repo: model.repo,
release: response.data.tagName
)
case .failure(let error):
Squawk.show(error: error)
}
}
}
}