Caps-iOS/CapCollector/Presentation/SortController.swift
Christoph Hagen 2806733b71 Version 1
2019-03-15 13:19:19 +01:00

93 lines
2.6 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
}
protocol SortControllerDelegate {
func sortController(didSelect sortType: SortCriteria, ascending: Bool)
}
class SortController: UITableViewController {
var selected: SortCriteria = .count
var ascending: Bool = true
var delegate: SortControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
let height = Cap.hasMatches ? 310 : 270
preferredContentSize = CGSize(width: 200, height: height)
}
private func setCell() {
for i in 0..<4 {
let index = IndexPath(row: i, section: 1)
let cell = tableView.cellForRow(at: index)
cell?.accessoryType = i == selected.rawValue ? .checkmark : .none
}
let index = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: index)
cell?.accessoryType = ascending ? .checkmark : .none
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setCell()
}
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
let generator = UIImpactFeedbackGenerator(style: style)
generator.impactOccurred()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard indexPath.section == 1 else {
ascending = !ascending
setCell()
delegate?.sortController(didSelect: selected, ascending: ascending)
giveFeedback(.light)
return
}
giveFeedback(.medium)
selected = SortCriteria(rawValue: indexPath.row)!
delegate?.sortController(didSelect: selected, ascending: ascending)
self.dismiss(animated: true)
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard indexPath.row == 3 else { return indexPath }
return Cap.hasMatches ? indexPath : nil
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}