Repository navigation
Expand file tree
/
Copy pathExamplesSettingsViewController.swift
More file actions
126 lines (86 loc) · 4.4 KB
/
Copy pathExamplesSettingsViewController.swift
File metadata and controls
126 lines (86 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import UIKit
import ContainerControllerSwift
class ExamplesSettingsViewController: StoryboardController {
// MARK: - Properties
var containers: [ContainerController] = []
var items: [TableAdapterItem] = []
@IBOutlet weak var tableView: TableAdapterView?
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
title = "Example Add/Settings Container"
let barButtonAddItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(navigationBarAddAction))
let barButtonCloseItem = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(navigationBarCloseAction))
self.navigationItem.rightBarButtonItems = [ barButtonAddItem, barButtonCloseItem ]
loadTableView()
containers(addStyle: .tableAdapterView)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
for container in containers {
container.remove()
}
}
// MARK: - Load TableView
func loadTableView() {
tableView?.deleteIndexCallback = { [weak self] (index) in
guard let _self = self else { return }
_self.containers(remove: index)
}
tableView?.selectIndexCallback = { [weak self] (index) in
guard let _self = self else { return }
_self.containers[index].move(type: .top)
}
tableView?.didScrollCallback = { [weak self] in
guard let _self = self else { return }
_self.view.endEditing(true)
}
}
// MARK: - Navigation Bar Actions
@objc func navigationBarCloseAction(_ sender: UIBarButtonItem) {
containers(remove: containers.count - 1)
}
@objc func navigationBarAddAction(_ sender: UIBarButtonItem? = nil) {
let alert = UIAlertController(title: "Add Container",
message: "together with ScrollView",
preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ExamplesContainerController.ScrollType.allCases.forEach { style in
alert.addAction(UIAlertAction(title: style.rawValue,
style: style == .tableAdapterView ? .destructive : .default,
handler: { [weak self] _ in
guard let _self = self else { return }
_self.containers(addStyle: style)
}))
}
self.present(alert, animated: true)
}
// MARK: - Add/Remove Container
func containers(addStyle containerStyle: ExamplesContainerController.ScrollType) {
switch containerStyle {
case .mapsContainer: containers(add: MapsContainerController(addTo: self, darkStyle: false), style: containerStyle)
case .locationContainer: containers(add: LocationContainerController(addTo: self, darkStyle: false), style: containerStyle)
case .routeContainer: containers(add: RouteContainerController(addTo: self, darkStyle: false), style: containerStyle)
case .menuContainer: containers(add: MenuContainerController(addTo: self, darkStyle: false, selectedIndex: 0), style: containerStyle)
default: containers(add: ExamplesContainerController(addTo: self, style: containerStyle), style: containerStyle)
}
}
func containers(add container: ContainerController, style: ExamplesContainerController.ScrollType) {
container.move(type: .bottom, completion: {
container.move(type: .top)
})
containers.append(container)
let subTitle = String(describing: type(of: container))
let title = style.rawValue
items.append( TitleTextItem(title: title, subtitle: subTitle, editing: true) )
tableView?.set(items: items, animated: true)
}
func containers(remove index: Int) {
if containers.isEmpty, items.isEmpty { return }
let container = containers[index]
container.remove()
containers.remove(at: index)
items.remove(at: index)
tableView?.set(items: items, animated: true)
}
}