forked from refactoring-challenge/reversi-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
143 lines (116 loc) · 5.1 KB
/
ViewController.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import ReversiLogic
import RxCocoa
import RxSwift
import UIKit
class ViewController: UIViewController {
@IBOutlet private(set) var boardView: BoardView! {
didSet { boardView.delegate = self }
}
@IBOutlet private(set) var resetButton: UIButton!
@IBOutlet private(set) var messageDiskView: DiskView!
@IBOutlet private(set) var messageLabel: UILabel!
@IBOutlet private(set) var messageDiskSizeConstraint: NSLayoutConstraint!
@IBOutlet private(set) var playerDarkControl: UISegmentedControl!
@IBOutlet private(set) var playerLightControl: UISegmentedControl!
@IBOutlet private(set) var playerDarkCountLabel: UILabel!
@IBOutlet private(set) var playerLightCountLabel: UILabel!
@IBOutlet private(set) var playerDarkActivityIndicator: UIActivityIndicatorView!
@IBOutlet private(set) var playerLightActivityIndicator: UIActivityIndicatorView!
private let disposeBag = DisposeBag()
@Lazy() private var viewModelFactory: ReversiViewModelFactoryType
private lazy var viewModel = viewModelFactory.make(
messageDiskSize: messageDiskSizeConstraint.constant,
mainAsyncScheduler: MainScheduler.asyncInstance,
mainScheduler: MainScheduler.instance
)
override func viewDidLoad() {
super.viewDidLoad()
do {
let output = viewModel.output
let scheduler = ConcurrentMainScheduler.instance
output.showAlert
.bind(to: Binder(self, scheduler: scheduler) { me, alert in
let alertController = UIAlertController.make(alert: alert)
me.present(alertController, animated: true)
})
.disposed(by: disposeBag)
output.messageDisk
.bind(to: Binder(self, scheduler: scheduler) { me, disk in
me.messageDiskView.disk = disk
})
.disposed(by: disposeBag)
output.messageDiskSizeConstant
.bind(to: Binder(self, scheduler: scheduler) { me, constant in
me.messageDiskSizeConstraint.constant = constant
})
.disposed(by: disposeBag)
output.resetBoard
.bind(to: Binder(self, scheduler: scheduler) { me, _ in
me.boardView.reset()
})
.disposed(by: disposeBag)
output.updateBoard
.bind(to: Binder(self, scheduler: scheduler) { me, update in
me.boardView.setDisk(update.disk,
atX: update.coordinate.x,
y: update.coordinate.y,
animated: update.animated,
completion: update.completion)
})
.disposed(by: disposeBag)
output.messageText
.bind(to: messageLabel.rx.text)
.disposed(by: disposeBag)
output.isPlayerDarkAnimating
.bind(to: playerDarkActivityIndicator.rx.isAnimating)
.disposed(by: disposeBag)
output.isPlayerLightAnimating
.bind(to: playerLightActivityIndicator.rx.isAnimating)
.disposed(by: disposeBag)
output.playerDarkCount
.bind(to: playerDarkCountLabel.rx.text)
.disposed(by: disposeBag)
output.playerLightCount
.bind(to: playerLightCountLabel.rx.text)
.disposed(by: disposeBag)
output.playerDarkSelectedIndex
.bind(to: playerDarkControl.rx.selectedSegmentIndex)
.disposed(by: disposeBag)
output.playerLightSelectedIndex
.bind(to: playerLightControl.rx.selectedSegmentIndex)
.disposed(by: disposeBag)
}
do {
let input = viewModel.input
defer { input.startGame(()) }
playerDarkControl.rx.selectedSegmentIndex.changed
.map { (.dark, $0) }
.bind(to: input.setPlayerWithDiskAndIndex)
.disposed(by: disposeBag)
playerLightControl.rx.selectedSegmentIndex.changed
.map { (.light, $0) }
.bind(to: input.setPlayerWithDiskAndIndex)
.disposed(by: disposeBag)
resetButton.rx.tap
.bind(to: input.handleReset)
.disposed(by: disposeBag)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
viewModel.input.viewDidAppear(())
}
}
extension ViewController: BoardViewDelegate {
func boardView(_ boardView: BoardView, didSelectCellAtX x: Int, y: Int) {
viewModel.input.handleSelectedCoordinate(.init(x: x, y: y))
}
}
extension ViewController {
static func make(factory: ReversiViewModelFactoryType) -> ViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: ViewController = storyboard.instantiateInitialViewController()!
vc.viewModelFactory = factory
return vc
}
}