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 pathInboxFilterController.swift
102 lines (88 loc) · 3.33 KB
/
InboxFilterController.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
//
// InboxFilterController.swift
// Freetime
//
// Created by Ryan Nystrom on 12/2/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import Foundation
import ContextMenu
protocol InboxFilterControllerClient {
func fetchSubscriptions(completion: @escaping (Result<[RepositoryDetails]>) -> Void)
}
protocol InboxFilterControllerListener: class {
func didUpdateSelectedFilter(for controller: InboxFilterController)
}
final class InboxFilterController {
let client: InboxFilterControllerClient
let announcer = Announcer<InboxFilterControllerListener>()
private static let filters = [
InboxFilterModel(type: .unread),
InboxFilterModel(type: .all),
InboxFilterModel(type: .mentioned),
InboxFilterModel(type: .assigned),
InboxFilterModel(type: .created)
]
private(set) var selected: InboxFilterModel = InboxFilterController.filters[0] {
didSet {
announcer.enumerate { $0.didUpdateSelectedFilter(for: self) }
}
}
private var fetchedFilters = [InboxFilterModel]()
init(client: InboxFilterControllerClient) {
self.client = client
}
func update(selection: InboxFilterModel) {
selected = selection
}
private func selected(model: InboxFilterModel) {
selected = model
}
private func showRepos(from viewController: UIViewController?) {
guard let viewController = viewController else { return }
ContextMenu.shared.show(
sourceViewController: viewController,
viewController: InboxFilterReposViewController(inboxFilterController: self),
options: ContextMenu.Options(
containerStyle: ContextMenu.ContainerStyle(
backgroundColor: Styles.Colors.menuBackgroundColor.color
)
)
)
}
func showMenu(from viewController: UIViewController) {
var items: [ContrastContextMenuItem] = InboxFilterController.filters.map { model in
ContrastContextMenuItem(
title: model.type.title,
iconName: model.type.iconName,
iconColor: Styles.Colors.Blue.medium.color,
separator: false,
action: { [weak self] menu in
menu.dismiss(animated: trueUnlessReduceMotionEnabled)
self?.selected(model: model)
})
}
items.append(ContrastContextMenuItem(
title: NSLocalizedString("Repos", comment: ""),
iconName: "repo",
iconColor: Styles.Colors.Blue.medium.color,
separator: true,
action: { [weak self, weak viewController] menu in
menu.dismiss(animated: trueUnlessReduceMotionEnabled)
self?.showRepos(from: viewController)
}))
ContextMenu.shared.show(
sourceViewController: viewController,
viewController: ContrastContextMenu(items: items),
options: ContextMenu.Options(
containerStyle: ContextMenu.ContainerStyle(
backgroundColor: Styles.Colors.menuBackgroundColor.color
),
menuStyle: .minimal,
hapticsStyle: .medium,
position: .centerX
),
sourceView: viewController.navigationItem.titleView
)
}
}