Skip to content

Commit

Permalink
Replace all view updates with assign calls in BalanceViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
cicerocamargo committed May 20, 2021
1 parent d48d864 commit 59194fa
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 deletions CombineIntro/BalanceFeature/BalanceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,64 @@ class BalanceViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let isRefreshingPublisher = viewModel.$state
.map(\.isRefreshing)
.removeDuplicates()
let formatDate = self.formatDate

isRefreshingPublisher
.assign(to: \.isHidden, on: rootView.refreshButton)
.store(in: &cancellables)
cancellables = [
viewModel.$state
.map(\.isRefreshing)
.removeDuplicates()
.assign(to: \.isHidden, on: rootView.refreshButton),

isRefreshingPublisher
.assign(to: \.writableIsAnimating, on: rootView.activityIndicator)
.store(in: &cancellables)
viewModel.$state
.map(\.isRefreshing)
.removeDuplicates()
.assign(
to: \.writableIsAnimating,
on: rootView.activityIndicator
),

viewModel.$state
.sink { [weak self] in self?.updateView(state: $0) }
.store(in: &cancellables)
viewModel.$state
.map(\.formattedBalance)
.removeDuplicates()
.map(Optional.some)
.assign(to: \.text, on: rootView.valueLabel),

rootView.refreshButton.touchUpInsidePublisher
.map { _ in BalanceViewEvent.refreshButtonWasTapped }
.subscribe(viewModel.eventSubject)
.store(in: &cancellables)
viewModel.$state
.map { $0.infoText(formatDate: formatDate) }
.removeDuplicates()
.map(Optional.some)
.assign(to: \.text, on: rootView.infoLabel),

viewModel.$state
.map(\.infoColor)
.removeDuplicates()
.map(Optional.some)
.assign(to: \.textColor, on: rootView.infoLabel),

viewModel.$state
.map(\.isRedacted)
.removeDuplicates()
.map { isRedacted in
isRedacted ? BalanceView.alphaForRedactedValueLabel : 1
}
.assign(to: \.alpha, on: rootView.valueLabel),

viewModel.$state
.map(\.isRedacted)
.removeDuplicates()
.map { !$0 }
.assign(to: \.isHidden, on: rootView.redactedOverlay),

rootView.refreshButton.touchUpInsidePublisher
.map { _ in BalanceViewEvent.refreshButtonWasTapped }
.subscribe(viewModel.eventSubject)
]
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
viewModel.eventSubject.send(.viewDidAppear)
}

private func updateView(state: BalanceViewState) {
rootView.valueLabel.text = state.formattedBalance
rootView.valueLabel.alpha = state.isRedacted
? BalanceView.alphaForRedactedValueLabel
: 1
rootView.infoLabel.text = state.infoText(formatDate: formatDate)
rootView.infoLabel.textColor = state.infoColor
rootView.redactedOverlay.isHidden = !state.isRedacted

view.setNeedsLayout()
}
}

#if DEBUG
Expand Down

0 comments on commit 59194fa

Please sign in to comment.