Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

Commit

Permalink
Fix show indicator and error
Browse files Browse the repository at this point in the history
  • Loading branch information
suho committed Apr 7, 2019
1 parent 0429430 commit c9312f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class TabBarCoordinator: Coordinate {
let router = Router<InfoTarget>()
let useCase = InfoNetwork(router: router)
let coordinator = SettingsCoordinator()
coordinator.viewController = controller
let viewModel = SettingsViewModel(useCase: useCase, coordinator: coordinator)
controller.viewModel = viewModel
let navigationController = NavigationController(rootViewController: controller)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class SettingsViewController: ViewController, View {
output.viewModels.drive(tableView.rx.items(cellIdentifier: DetailCell.identify, cellType: DetailCell.self)) { (_, viewModel, cell) in
cell.viewModel = viewModel
}.disposed(by: bag)
output.indicator.drive().disposed(by: bag)
output.error.drive().disposed(by: bag)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ final class SettingsViewModel: ViewModel {
}

func transform(input: Input) -> Output {
let indicator: RxIndicator = RxIndicator()
let rxError: RxError = RxError()
let viewModels: Driver<[DetailCellViewModel]> = input.loadTrigger
.flatMapLatest { _ -> Driver<Info> in
return self.useCase.about().emptyDriverIfError()
return self.useCase.about().indicate(indicator).trackError(into: rxError).emptyDriverIfError()
}
.map { info -> [DetailCellViewModel] in
return DetailCellViewModel.Kind.allCases.map { DetailCellViewModel(kind: $0, info: info) }
}
let output = Output(viewModels: viewModels)
let error = rxError.asObservable().do(onNext: { (error) in
self.coordinator?.showError(error)
}).emptyDriverIfError()
let output = Output(viewModels: viewModels, indicator: indicator.asObservable().emptyDriverIfError(), error: error)
return output
}
}
Expand All @@ -41,6 +46,7 @@ extension SettingsViewModel {

struct Output {
let viewModels: Driver<[DetailCellViewModel]>

let indicator: Driver<Bool>
let error: Driver<Error>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ final class AddTaskViewController: ViewController {
setupBackground()
setupNavi()
setupTimePicker()
setupTextField()
}

override func bindViewModel() {
super.bindViewModel()
let save = saveButton.rx.tap.asDriver()
let save = saveButton.rx.tap
.do(onNext: { _ in
self.view.endEditing(true)
})
.emptyDriverIfError()
let cancel = cancelButton.rx.tap.asDriver()
let time = timePicker.rx.date.asDriver()
let name = nameTextField.rx.text.orEmpty.asDriver()
let input = AddTaskViewModel.Input(cancelTrigger: cancel, saveTrigger: save, time: time, name: name)
let output = viewModel.transform(input: input)
output.cancel.drive().disposed(by: bag)
output.save.drive().disposed(by: bag)
nameTextField.rx.controlEvent(.editingDidEndOnExit)
.do(onNext: { _ in
self.view.endEditing(true)
})
.emptyDriverIfError()
.drive()
.disposed(by: bag)
}
}

Expand All @@ -62,16 +72,4 @@ extension AddTaskViewController {
timePicker.minimumDate = Date()
timePicker.setValue(UIColor.white, forKey: "textColor")
}

private func setupTextField() {
nameTextField.delegate = self
}
}

// MARK: - UITextFieldDelegate
extension AddTaskViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
view.endEditing(true)
return true
}
}

0 comments on commit c9312f5

Please sign in to comment.