Caps-iOS/CapCollector/Presentation/SortController.swift

81 lines
2.3 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
}
2020-05-16 11:21:55 +02:00
protocol SortControllerDelegate: class {
var sortControllerShouldIncludeMatchOption: Bool { get }
2019-03-15 13:19:19 +01:00
func sortController(didSelect sortType: SortCriteria, ascending: Bool)
}
class SortController: UITableViewController {
var selected: SortCriteria = .count
var ascending: Bool = true
2020-05-16 11:21:55 +02:00
private var includeMatches: Bool {
delegate?.sortControllerShouldIncludeMatchOption ?? false
}
weak var delegate: SortControllerDelegate?
2019-03-15 13:19:19 +01:00
override func viewDidLoad() {
super.viewDidLoad()
2020-05-16 11:21:55 +02:00
let height = includeMatches ? 298 : 258
2019-03-15 13:19:19 +01:00
preferredContentSize = CGSize(width: 200, height: height)
}
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
2020-05-16 11:21:55 +02:00
tableView.reloadData()
2019-03-15 13:19:19 +01:00
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 }
2020-05-16 11:21:55 +02:00
return includeMatches ? indexPath : nil
2019-03-15 13:19:19 +01:00
}
2020-05-16 11:21:55 +02:00
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
cell.accessoryType = ascending ? .checkmark : .none
default:
cell.accessoryType = indexPath.row == selected.rawValue ? .checkmark : .none
break
}
2019-03-15 13:19:19 +01:00
}
}