Caps-iOS/CapCollector/Presentation/SortController.swift

114 lines
3.2 KiB
Swift
Raw Normal View History

2019-03-15 13:19:19 +01:00
//
// SortController.swift
// CapCollector
//
// Created by Christoph on 12.11.18.
// Copyright © 2018 CH. All rights reserved.
//
import UIKit
enum SortCriteria: Int {
case id = 0
case name = 1
case count = 2
case match = 3
var text: String {
switch self {
case .id:
return "Id"
case .name:
return "Name"
case .count:
return "Count"
case .match:
return "Match"
}
}
2019-03-15 13:19:19 +01:00
}
2020-05-16 11:21:55 +02:00
protocol SortControllerDelegate: class {
2019-03-15 13:19:19 +01:00
func sortController(didSelect sortType: SortCriteria, ascending: Bool)
}
class SortController: UITableViewController {
@IBOutlet weak var thirdRowLabel: UILabel!
2019-03-15 13:19:19 +01:00
var selected: SortCriteria = .id
2019-03-15 13:19:19 +01:00
var ascending: Bool = false
2020-05-16 11:21:55 +02:00
weak var delegate: SortControllerDelegate?
2019-03-15 13:19:19 +01:00
var options = [SortCriteria]()
2019-03-15 13:19:19 +01:00
override func viewDidLoad() {
super.viewDidLoad()
preferredContentSize = CGSize(width: 200, height: 139 + options.count * 40)
2019-03-15 13:19:19 +01:00
}
2019-03-15 13:19:19 +01:00
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
UIImpactFeedbackGenerator(style: style).impactOccurred()
}
private func sortCriteria(for index: Int) -> SortCriteria {
index < options.count ? options[index] : .match
2019-03-15 13:19:19 +01:00
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
section == 0 ? "Sort order" : "Sort by"
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
section == 0 ? 1 : options.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SortCell")!
guard indexPath.section != 0 else {
cell.accessoryType = ascending ? .checkmark : .none
cell.textLabel?.text = "Ascending"
return cell
}
let select = sortCriteria(for: indexPath.row)
cell.textLabel?.text = select.text
guard select == selected else {
cell.accessoryType = .none
return cell
}
cell.accessoryType = .checkmark
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
40
}
2019-03-15 13:19:19 +01:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard indexPath.section == 1 else {
ascending = !ascending
tableView.reloadRows(at: [indexPath], with: .automatic)
2019-03-15 13:19:19 +01:00
delegate?.sortController(didSelect: selected, ascending: ascending)
giveFeedback(.light)
return
}
giveFeedback(.medium)
selected = sortCriteria(for: indexPath.row)
tableView.reloadRows(at: [indexPath], with: .automatic)
2019-03-15 13:19:19 +01:00
delegate?.sortController(didSelect: selected, ascending: ascending)
self.dismiss(animated: true)
}
}