114 lines
3.2 KiB
Swift
114 lines
3.2 KiB
Swift
//
|
|
// 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"
|
|
}
|
|
}
|
|
}
|
|
|
|
protocol SortControllerDelegate: AnyObject {
|
|
|
|
func sortController(didSelect sortType: SortCriteria, ascending: Bool)
|
|
}
|
|
|
|
class SortController: UITableViewController {
|
|
|
|
@IBOutlet weak var thirdRowLabel: UILabel!
|
|
|
|
var selected: SortCriteria = .id
|
|
|
|
var ascending: Bool = false
|
|
|
|
weak var delegate: SortControllerDelegate?
|
|
|
|
var options = [SortCriteria]()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
preferredContentSize = CGSize(width: 200, height: 139 + options.count * 40)
|
|
}
|
|
|
|
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
|
|
UIImpactFeedbackGenerator(style: style).impactOccurred()
|
|
}
|
|
|
|
private func sortCriteria(for index: Int) -> SortCriteria {
|
|
index < options.count ? options[index] : .match
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
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)
|
|
delegate?.sortController(didSelect: selected, ascending: ascending)
|
|
giveFeedback(.light)
|
|
return
|
|
}
|
|
giveFeedback(.medium)
|
|
selected = sortCriteria(for: indexPath.row)
|
|
tableView.reloadRows(at: [indexPath], with: .automatic)
|
|
delegate?.sortController(didSelect: selected, ascending: ascending)
|
|
self.dismiss(animated: true)
|
|
}
|
|
}
|