Building Subscription Blogging App: Part 9 – Pay Wall Guards (2021, Xcode 12, Swift 5) – iOS
private func bind() {
premium
.combineLatest(availableCount)
.sink { [weak self] isPremium, count in
if isPremium {
self?.isServiceUsable = false
} else {
if count > 0 {
self?.isServiceUsable = true
} else {
self?.isServiceUsable = false
}
}
}
.store(in: &cancellables)
getSubscriptionStatus { _ in
}
}
premium
퍼블리셔 및 현재 날짜 별로 초기화될 예정인 availableCount
값을 동시에 combineLatest
를 통해 구독true
리턴true
리턴func useService() {
let currentCount = availableCount.value
availableCount.send(currentCount - 1)
}
userService
메소드가 호출, availableCount
퍼블리셔 값 1 감소private func handlePost(with post: PostModel) {
guard let uid = Auth.auth().currentUser?.uid else { return }
if post.uid == uid {
output.send(.navigateToPost(post))
} else {
if iapService.isServiceUsable {
iapService.useService()
output.send(.navigateToPost(post))
} else {
output.send(.showPayWallView)
}
}
}
func fetchPosts() {
service.fetchPosts() { [weak self] result in
switch result {
case .failure(let error): print(error.localizedDescription)
case .success(let posts): self?.posts.send(posts)
}
self?.output.send(.fetchResultReturend)
}
}
private func showPayWallView() {
let vc = PayWallViewController()
let nav = UINavigationController(rootViewController: vc)
nav.navigationItem.largeTitleDisplayMode = .always
nav.navigationBar.prefersLargeTitles = true
nav.modalPresentationStyle = .fullScreen
present(nav, animated: true)
}
어떤 사람을 보게 해줄 것인지 뷰 모델에서 모두 결정한 뒤 행동할 방향만을 컨트롤러에게 알려주자.