|
| 1 | +// |
| 2 | +// InboxFilterController.swift |
| 3 | +// Freetime |
| 4 | +// |
| 5 | +// Created by Ryan Nystrom on 12/2/18. |
| 6 | +// Copyright © 2018 Ryan Nystrom. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import ContextMenu |
| 11 | + |
| 12 | +protocol InboxFilterControllerClient { |
| 13 | + func fetchSubscriptions(completion: @escaping (Result<[RepositoryDetails]>) -> Void) |
| 14 | +} |
| 15 | + |
| 16 | +protocol InboxFilterControllerListener: class { |
| 17 | + func didUpdateSelectedFilter(for controller: InboxFilterController) |
| 18 | +} |
| 19 | + |
| 20 | +final class InboxFilterController { |
| 21 | + |
| 22 | + let client: InboxFilterControllerClient |
| 23 | + let announcer = Announcer<InboxFilterControllerListener>() |
| 24 | + |
| 25 | + private static let filters = [ |
| 26 | + InboxFilterModel(type: .unread), |
| 27 | + InboxFilterModel(type: .all), |
| 28 | + InboxFilterModel(type: .mentioned), |
| 29 | + InboxFilterModel(type: .assigned), |
| 30 | + InboxFilterModel(type: .created) |
| 31 | + ] |
| 32 | + |
| 33 | + private(set) var selected: InboxFilterModel = InboxFilterController.filters[0] { |
| 34 | + didSet { |
| 35 | + announcer.enumerate { $0.didUpdateSelectedFilter(for: self) } |
| 36 | + } |
| 37 | + } |
| 38 | + private var fetchedFilters = [InboxFilterModel]() |
| 39 | + |
| 40 | + init(client: InboxFilterControllerClient) { |
| 41 | + self.client = client |
| 42 | + } |
| 43 | + |
| 44 | + func update(selection: InboxFilterModel) { |
| 45 | + selected = selection |
| 46 | + } |
| 47 | + |
| 48 | + private func selected(model: InboxFilterModel) { |
| 49 | + selected = model |
| 50 | + } |
| 51 | + |
| 52 | + private func showRepos(from viewController: UIViewController?) { |
| 53 | + guard let viewController = viewController else { return } |
| 54 | + ContextMenu.shared.show( |
| 55 | + sourceViewController: viewController, |
| 56 | + viewController: InboxFilterReposViewController(inboxFilterController: self), |
| 57 | + options: ContextMenu.Options( |
| 58 | + containerStyle: ContextMenu.ContainerStyle( |
| 59 | + backgroundColor: Styles.Colors.menuBackgroundColor.color |
| 60 | + ) |
| 61 | + ) |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + func showMenu(from viewController: UIViewController) { |
| 66 | + var items: [ContrastContextMenuItem] = InboxFilterController.filters.map { model in |
| 67 | + ContrastContextMenuItem( |
| 68 | + title: model.type.title, |
| 69 | + iconName: model.type.iconName, |
| 70 | + iconColor: Styles.Colors.Blue.medium.color, |
| 71 | + separator: false, |
| 72 | + action: { [weak self] menu in |
| 73 | + menu.dismiss(animated: trueUnlessReduceMotionEnabled) |
| 74 | + self?.selected(model: model) |
| 75 | + }) |
| 76 | + } |
| 77 | + items.append(ContrastContextMenuItem( |
| 78 | + title: NSLocalizedString("Repos", comment: ""), |
| 79 | + iconName: "repo", |
| 80 | + iconColor: Styles.Colors.Blue.medium.color, |
| 81 | + separator: true, |
| 82 | + action: { [weak self, weak viewController] menu in |
| 83 | + menu.dismiss(animated: trueUnlessReduceMotionEnabled) |
| 84 | + self?.showRepos(from: viewController) |
| 85 | + })) |
| 86 | + |
| 87 | + ContextMenu.shared.show( |
| 88 | + sourceViewController: viewController, |
| 89 | + viewController: ContrastContextMenu(items: items), |
| 90 | + options: ContextMenu.Options( |
| 91 | + containerStyle: ContextMenu.ContainerStyle( |
| 92 | + backgroundColor: Styles.Colors.menuBackgroundColor.color |
| 93 | + ), |
| 94 | + menuStyle: .minimal, |
| 95 | + hapticsStyle: .medium, |
| 96 | + position: .centerX |
| 97 | + ), |
| 98 | + sourceView: viewController.navigationItem.titleView |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments