-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSimpleTextFieldViewController.swift
57 lines (46 loc) · 1.42 KB
/
SimpleTextFieldViewController.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
// Copyright © 2017 Stefan van den Oord. All rights reserved.
import UIKit
import ReSwift
import RxSwift
import ReRxSwift
class SimpleTextFieldViewController: UIViewController {
let connection = Connection(
store: store,
mapStateToProps: mapStateToProps,
mapDispatchToActions: mapDispatchToActions
)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
connection.connect()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
connection.disconnect()
}
override func viewDidLoad() {
super.viewDidLoad()
connection.bind(\Props.text, to: textField.rx.text)
}
@IBAction func editingChanged(_ sender: UITextField) {
actions.updatedText(sender.text ?? "")
}
@IBOutlet weak var textField: UITextField!
}
extension SimpleTextFieldViewController: Connectable {
struct Props {
let text: String
}
struct Actions {
let updatedText: (String) -> Void
}
}
private let mapStateToProps = { (appState: AppState) in
return SimpleTextFieldViewController.Props(
text: appState.simpleTextField.content
)
}
private let mapDispatchToActions = { (dispatch: @escaping DispatchFunction) in
return SimpleTextFieldViewController.Actions(
updatedText: { newText in dispatch(SetContent(newContent: newText)) }
)
}