-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatefulTextButtonVC.swift
62 lines (47 loc) · 1.39 KB
/
StatefulTextButtonVC.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
//
// StatefulTextButtonVC.swift
// ASDisplayNodes+Style
//
// Created by Adam Tait on 2/21/18.
// Copyright © 2018 Sisterical Inc. All rights reserved.
//
import UIKit
class StatefulTextButtonVC : TextButtonVC
{
struct State : Hashable, Equatable
{
let uuid = UUID()
let title : String
let style : Style
public static func == (lhs: State, rhs: State) -> Bool {
return lhs.uuid == rhs.uuid
}
public var hashValue: Int { get { return uuid.hashValue } }
}
// private properties
let states : MutableCollection<State>
// initializers
init(_ states : [State])
{
self.states = MutableCollection<State>(states)
super.init(style: states.first!.style,
title: states.first!.title)
_ = self.selected.addObserver(selectedPropertyChanged)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// observers
func selectedPropertyChanged(_: Observable)
// cycle through available states
{
let s = currentState()
self.node.styleset.set(s.style)
self.set(title: s.title)
}
// helpers
func currentState() -> State
{
return selected.get()! ? states.at(1)! : states.at(0)!
}
}