Add layout logic and models

This commit is contained in:
Ivan Vorobei
2020-11-28 12:13:27 +03:00
parent c81153d727
commit d3a3705e05
6 changed files with 190 additions and 37 deletions
+39 -11
View File
@@ -2,9 +2,10 @@ import UIKit
import SparrowKit
import SPDiffable
// Maybe ding toolbar
class PresetsController: SPDiffableTableController {
// MARK: - Init
init() {
super.init(style: .insetGrouped)
}
@@ -13,15 +14,27 @@ class PresetsController: SPDiffableTableController {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "SPAlert Presets"
currentPreset = presets.first!
setCellProviders(SPDiffableTableCellProviders.default, sections: content)
navigationController?.isToolbarHidden = false
toolbarItems = [
.init(image: .system("chevron.down.circle.fill"), primaryAction: .init(handler: { (action) in
#warning("doing next switch")
.init(image: .system("chevron.down.circle"), primaryAction: .init(handler: { (action) in
guard let currentPreset = self.currentPreset else {
self.currentPreset = self.presets.first
return
}
guard let index = self.presets.firstIndex(where: { $0.id == currentPreset.id }) else { return }
self.currentPreset = self.presets[safe: index + 1]
if self.currentPreset == nil {
self.currentPreset = self.presets.first
}
})),
.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
.init(systemItem: .play, primaryAction: .init(handler: { (action) in
@@ -31,6 +44,8 @@ class PresetsController: SPDiffableTableController {
]
}
// MARK: - Data
fileprivate var presets: [AlertPreset] {
return [
AlertPreset(name: "Done", preset: .done),
@@ -38,10 +53,29 @@ class PresetsController: SPDiffableTableController {
]
}
// MARK: - Diffable
var currentPreset: AlertPreset? {
willSet {
guard let id = self.currentPreset?.id else { return }
let cell = diffableDataSource?.cell(UITableViewCell.self, for: id)
cell?.accessoryType = .none
}
didSet {
guard let id = self.currentPreset?.id else { return }
let cell = diffableDataSource?.cell(UITableViewCell.self, for: id)
cell?.accessoryType = .checkmark
}
}
var content: [SPDiffableSection] {
let items = presets.map { (preset) -> SPDiffableTableRow in
return SPDiffableTableRow(text: preset.name, accessoryType: .none, selectionStyle: .none) { (_) in
return
return SPDiffableTableRow(
text: preset.name,
accessoryType: (preset.id == currentPreset?.id) ? .checkmark : .none,
selectionStyle: .none) { [weak self] _ in
guard let self = self else { return }
self.currentPreset = preset
}
}
return [
@@ -49,9 +83,3 @@ class PresetsController: SPDiffableTableController {
]
}
}
struct AlertPreset {
var name: String
var preset: SPAlertPreset
}