This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuOption.swift
73 lines (59 loc) · 2.04 KB
/
MenuOption.swift
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
//
// MenuItem.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 10/08/16.
//
//
import Foundation
enum MenuOptionType {
case `default`
case numberChooser
case checkbox
}
// TODO: Perhaps can use generics here. A simple implementation would default to number chooser for
// Int value type, checkbox for Bool value type, etcetera.
class MenuOption {
fileprivate(set) var title: String
fileprivate(set) var type = MenuOptionType.default
fileprivate(set) var onSelected: (() -> Void)?
fileprivate(set) var onValueChanged: ((_ newValue: AnyObject?) -> Void)?
var onValidate: ((_ newValue: AnyObject?) -> Bool)?
fileprivate(set) var value: AnyObject? {
didSet {
if let onValueChanged = self.onValueChanged {
onValueChanged(value)
}
}
}
// TODO: Figure out if we can use a cleaner solution than this assert. Static factory methods
// for checkbox and number chooser could be one approach, but seems to not be idiomatic Swift.
// Another option could be using a builder class.
init(title: String, type: MenuOptionType, value: AnyObject?, onValueChanged: ((_ newValue: AnyObject?) -> Void)? = nil) {
assert(type != .default, "Menu type of default can not have a value or value changed handler - use the other constructor.")
self.title = title
self.type = type
self.value = value
self.onValueChanged = onValueChanged
}
init(title: String, onSelected: (() -> Void)? = nil) {
self.title = title
self.onSelected = onSelected
}
// MARK: - Public
func update(_ newValue: AnyObject?) -> Bool {
var didUpdate = false
let updateBlock = {
self.value = newValue
didUpdate = true
}
if let onValidate = self.onValidate {
if onValidate(newValue) {
updateBlock()
}
} else {
updateBlock()
}
return didUpdate
}
}