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 380
Expand file tree
/
Copy pathLabelsViewController.swift
More file actions
129 lines (109 loc) · 3.9 KB
/
Copy pathLabelsViewController.swift
File metadata and controls
129 lines (109 loc) · 3.9 KB
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
//
// LabelsViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 4/21/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
import IGListKit
import Squawk
final class LabelsViewController: BaseListViewController<String>,
BaseListViewControllerDataSource,
LabelSectionControllerDelegate {
private let selectedLabels: Set<RepositoryLabel>
private var labels = [RepositoryLabel]()
private let client: GithubClient
private let request: RepositoryLabelsQuery
var wasDismissedByDone: Bool = false
init(
selected: [RepositoryLabel],
client: GithubClient,
owner: String,
repo: String
) {
self.selectedLabels = Set(selected)
self.client = client
self.request = RepositoryLabelsQuery(owner: owner, repo: repo)
super.init(emptyErrorMessage: NSLocalizedString("No labels found", comment: ""))
preferredContentSize = Styles.Sizes.contextMenuSize
title = Constants.Strings.labels
feed.collectionView.backgroundColor = Styles.Colors.menuBackgroundColor.color
feed.setLoadingSpinnerColor(to: .white)
dataSource = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
addMenuDoneButton()
addMenuClearButton()
}
// MARK: Public API
var selected: [RepositoryLabel] {
return labels.filter {
if let sectionController: LabelSectionController = feed.swiftAdapter.sectionController(for: $0) {
return sectionController.selected
}
return false
}
}
func addMenuClearButton() {
navigationItem.leftBarButtonItem = UIBarButtonItem(
title: Constants.Strings.clear,
style: .plain,
target: self,
action: #selector(onMenuClear)
)
navigationItem.leftBarButtonItem?.tintColor = Styles.Colors.Gray.light.color
navigationItem.leftBarButtonItem?.isEnabled = selectedLabels.count > 0
}
func updateClearButtonEnabled() {
navigationItem.leftBarButtonItem?.isEnabled = selected.count > 0
}
@objc func onMenuClear() {
self.selected.forEach {
if let sectionController: LabelSectionController = feed.swiftAdapter.sectionController(for: $0) {
sectionController.didSelectItem(at: 0)
}
}
updateClearButtonEnabled()
}
// MARK: Overrides
override func fetch(page: String?) {
client.client.query(request, result: { data in
data.repository?.labels?.nodes
}, completion: { [weak self] result in
switch result {
case .success(let nodes):
self?.labels = nodes.compactMap {
guard let node = $0 else { return nil }
return RepositoryLabel(color: node.color, name: node.name)
}.sorted { $0.name < $1.name }
self?.update(animated: true)
case .failure(let error):
Squawk.show(error: error)
}
})
}
override func onMenuDone() {
self.wasDismissedByDone = true
super.onMenuDone()
}
// MARK: BaseListViewControllerDataSource
func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
return labels.map { [selectedLabels] label in
ListSwiftPair.pair(label) {
let controller = LabelSectionController(selected: selectedLabels.contains(label))
controller.delegate = self
return controller
}
}
}
// MARK: LabelSectionControllerDelegate
func didSelect(controller: LabelSectionController) {
updateClearButtonEnabled()
}
}