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 379
Expand file tree
/
Copy pathLabelsViewController.swift
More file actions
125 lines (107 loc) · 3.74 KB
/
Copy pathLabelsViewController.swift
File metadata and controls
125 lines (107 loc) · 3.74 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
//
// 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 owner: String
private let repo: String
private let client: GithubClient
init(
selected: [RepositoryLabel],
client: GithubClient,
owner: String,
repo: String
) {
self.selectedLabels = Set(selected)
self.client = client
self.owner = owner
self.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.collectionView.indicatorStyle = .white
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.fetchRepositoryLabels(
owner: owner,
repo: repo,
nextPage: page as String?
) { [weak self] result in
guard let strongSelf = self else { return }
switch result {
case .success(let payload):
self?.labels = payload.labels.sorted { $0.name < $1.name }
strongSelf.update(page: payload.nextPage, animated: true)
case .error(let error):
Squawk.show(error: error)
}
}
}
// 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()
}
}