Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Add clear button to action menus #2288

Merged
merged 6 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/Labels/LabelSectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

import IGListKit

protocol LabelSectionControllerDelegate: class {
func didSelect(controller: LabelSectionController)
}

final class LabelSectionController: ListSwiftSectionController<RepositoryLabel> {

public weak var delegate: LabelSectionControllerDelegate?

public private(set) var selected: Bool

init(selected: Bool) {
Expand Down Expand Up @@ -41,6 +47,7 @@ final class LabelSectionController: ListSwiftSectionController<RepositoryLabel>
strongSelf.selected = !strongSelf.selected
context.deselect()
context.cell?.setSelected(strongSelf.selected)
strongSelf.delegate?.didSelect(controller: strongSelf)
})
]
}
Expand Down
41 changes: 39 additions & 2 deletions Classes/Labels/LabelsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import UIKit
import IGListKit
import Squawk

final class LabelsViewController: BaseListViewController2<String>, BaseListViewController2DataSource {
final class LabelsViewController: BaseListViewController2<String>,
BaseListViewController2DataSource,
LabelSectionControllerDelegate {

private let selectedLabels: Set<RepositoryLabel>
private var labels = [RepositoryLabel]()
Expand Down Expand Up @@ -42,6 +44,7 @@ final class LabelsViewController: BaseListViewController2<String>, BaseListViewC
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
addMenuDoneButton()
addMenuClearButton()
}

// MARK: Public API
Expand All @@ -55,6 +58,31 @@ final class LabelsViewController: BaseListViewController2<String>, BaseListViewC
}
}

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?) {
Expand All @@ -78,8 +106,17 @@ final class LabelsViewController: BaseListViewController2<String>, BaseListViewC

func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
return labels.map { [selectedLabels] label in
ListSwiftPair.pair(label) { LabelSectionController(selected: selectedLabels.contains(label)) }
ListSwiftPair.pair(label) {
let controller = LabelSectionController(selected: selectedLabels.contains(label))
controller.delegate = self
return controller
}
}
}

// MARK: LabelSectionControllerDelegate

func didSelect(controller: LabelSectionController) {
updateClearButtonEnabled()
}
}
27 changes: 26 additions & 1 deletion Classes/Milestones/MilestonesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ final class MilestonesViewController: BaseListViewController2<String>,
BaseListViewController2DataSource,
MilestoneSectionControllerDelegate {

public private(set) var selected: Milestone?
public private(set) var selected: Milestone? = nil {
didSet {
self.updateClearButtonEnabled()
}
}

private var owner: String!
private var repo: String!
Expand Down Expand Up @@ -55,6 +59,27 @@ MilestoneSectionControllerDelegate {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
addMenuDoneButton()
addMenuClearButton()
}

func addMenuClearButton() {
navigationItem.leftBarButtonItem = UIBarButtonItem(
title: Constants.Strings.clear,
style: .plain,
target: self,
action: #selector(onMenuClear)
)
navigationItem.leftBarButtonItem?.tintColor = Styles.Colors.Gray.light.color
updateClearButtonEnabled()
}

func updateClearButtonEnabled() {
navigationItem.leftBarButtonItem?.isEnabled = selected != nil
}

@objc func onMenuClear() {
selected = nil
update(animated: true)
}

// MARK: Overrides
Expand Down
52 changes: 35 additions & 17 deletions Classes/People/PeopleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,10 @@ PeopleSectionControllerDelegate {

self.dataSource = self

switch type {
case .assignee: title = NSLocalizedString("Assignees", comment: "")
case .reviewer: title = NSLocalizedString("Reviewers", comment: "")
}

feed.collectionView.backgroundColor = Styles.Colors.menuBackgroundColor.color
feed.setLoadingSpinnerColor(to: .white)
preferredContentSize = Styles.Sizes.contextMenuSize
updateSelectionCount()
updateTitle()
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -68,6 +63,7 @@ PeopleSectionControllerDelegate {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
addMenuDoneButton()
addMenuClearButton()
}

// MARK: Public API
Expand All @@ -80,6 +76,29 @@ PeopleSectionControllerDelegate {
return false
}
}

func updateClearButtonEnabled() {
navigationItem.leftBarButtonItem?.isEnabled = selected.count > 0
}

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 = self.selections.count > 0
}

@objc func onMenuClear() {
self.selected.forEach {
if let sectionController: PeopleSectionController = feed.swiftAdapter.sectionController(for: $0) {
sectionController.didSelectItem(at: 0)
}
}
}

static func sortUsers(users: [V3User], currentUser: String?) -> [V3User] {
return users.sorted {
Expand All @@ -90,17 +109,17 @@ PeopleSectionControllerDelegate {
}
}
}

// MARK: Private API

func updateSelectionCount() {
let label = UILabel()
label.font = Styles.Text.body.preferredFont
label.backgroundColor = .clear
label.textColor = Styles.Colors.Gray.light.color
label.text = "\(selected.count)/\(selectionLimit)"
label.sizeToFit()
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: label)
private func updateTitle() {
let selectedCount = users.count > 0 ? selected.count : selections.count
let counter = "\(selectedCount)/\(selectionLimit)"
switch type {
case .assignee: title = "\(Constants.Strings.assignees) \(counter)"
case .reviewer: title = "\(Constants.Strings.reviewers) \(counter)"
}
updateClearButtonEnabled()
}

// MARK: Overrides
Expand Down Expand Up @@ -158,7 +177,6 @@ PeopleSectionControllerDelegate {
// MARK: PeopleSectionControllerDelegate

func didSelect(controller: PeopleSectionController) {
updateSelectionCount()
updateTitle()
}

}
1 change: 1 addition & 0 deletions Classes/Views/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ enum Constants {
static let reviewers = NSLocalizedString("Reviewers", comment: "")
static let reviewGitHubAccess = NSLocalizedString("Review GitHub Access", comment: "")
static let tryAgain = NSLocalizedString("Try Again", comment: "")
static let clear = NSLocalizedString("Clear", comment: "")
}
}