Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/random music #13

Merged
merged 11 commits into from
Apr 27, 2022
11 changes: 8 additions & 3 deletions LiarGame/Sources/Reactor/HomeReactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ import RxCocoa

final class HomeReactor: Reactor{
enum Action{
case updateMode(String?)
case updateMode(GameMode)
}
enum Mutation{
case setMode(String?)
case setMode(GameMode)
}
struct State{
var mode: String?
var mode: GameMode?
}

let initialState = State()

enum GameMode {
case liarGame
case randomMusicQuiz
}

2jae6 marked this conversation as resolved.
Show resolved Hide resolved

func mutate(action: Action) -> Observable<Mutation> {
switch action {
Expand Down
105 changes: 63 additions & 42 deletions LiarGame/Sources/ViewController/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,99 @@ import RxCocoa
import ReactorKit

final class HomeViewController: UIViewController, View{


typealias Reactor = HomeReactor
typealias Reactor = HomeReactor

init(reactor: HomeReactor){

super.init(nibName: nil, bundle: nil)
self.reactor = reactor
self.view.addSubview(self.flexLayoutContainer)
self.flexLayoutContainer.flex.direction(.column).alignItems(.center).justifyContent(.center).padding(10).define{ flex in
flex.backgroundColor(.systemPink)
flex.addItem(self.liarGameStartButton).width(200).height(50).backgroundColor(.yellow)
}
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
setupView()

}

@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.flexLayoutContainer.pin.all()
self.flexLayoutContainer.pin.all(view.pin.safeArea)
self.flexLayoutContainer.flex.layout()
}

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.setupView()
self.view.backgroundColor = .systemPink
}

let flexLayoutContainer: UIView = UIView()
private let flexLayoutContainer: UIView = UIView()

var disposeBag: DisposeBag = DisposeBag()
let liarGameStartButton = UIButton()

private lazy var gameList = [liarGameStartButton, randomMusicQuiz]
private let liarGameStartButton = makeGameButton(str: "라이어 게임")
private let randomMusicQuiz = makeGameButton(str: "랜덤 음악 맞추기")
2jae6 marked this conversation as resolved.
Show resolved Hide resolved

}

// MARK: - Setup View
extension HomeViewController{
private func setupView(){
liarGameStartButton.do{
$0.setTitle("라이어 게임", for: .normal)
$0.setTitleColor(.black, for: .normal)
self.view.addSubview($0)
extension HomeViewController {
private func setupView() {
self.view.addSubview(self.flexLayoutContainer)
self.flexLayoutContainer.flex.direction(.column).alignItems(.center).justifyContent(.center).padding(10).define { flex in
gameList.forEach {
flex.addItem($0)
.width(200)
.height(50)
.backgroundColor(.yellow)
}
}
.verticallySpacing(15)
}
}

// MARK: - Binding
extension HomeViewController{
// MARK: - Binding
extension HomeViewController {
func bind(reactor: Reactor) {
self.liarGameStartButton.rx.tap.asDriver()
.drive(onNext: {
reactor.action.onNext(.updateMode("LIAR"))
}).disposed(by: disposeBag)

liarGameStartButton.rx.tap
.subscribe(onNext: {
reactor.action.onNext(.updateMode(.liarGame))
})
.disposed(by: disposeBag)

reactor.state.map { $0.mode }
.subscribe(onNext: {
print($0)
if $0 == "LIAR"{
let liarVC = LiarGameModeViewController(reactor: LiarGameModeReactor())
liarVC.modalPresentationStyle = .fullScreen
self.present(liarVC, animated: true, completion: nil)
}
})
.disposed(by: disposeBag)
randomMusicQuiz.rx.tap
.map { _ in Reactor.Action.updateMode(.randomMusicQuiz) }
.bind(to: reactor.action)
.disposed(by: disposeBag)

// TODO: - 해당 부분에서 `fullScreen` 으로 `present` 가 이루어지므로 메뉴로 돌아갈 기능 필요
reactor.state.map(\.mode)
.compactMap { $0 }
.withUnretained(self)
.subscribe(onNext: { `self`, mode in
switch mode {
case .liarGame:
let liarVC = LiarGameModeViewController(reactor: LiarGameModeReactor())
liarVC.modalPresentationStyle = .fullScreen
self.present(liarVC, animated: true)
case .randomMusicQuiz:
let reactor = RandomMusicQuizReactor(repository: RandomMusicRepository())
let vc = RandomMusicQuizViewController(reactor: reactor)
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true)
}
})
.disposed(by: disposeBag)
}

}

fileprivate func makeGameButton(str: String) -> UIButton {
let button = UIButton()

button.setTitle(str, for: .normal)
button.setTitleColor(.black, for: .normal)
button.setTitleColor(.systemGray, for: .normal)

return button
}