This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLanguageSelectionViewController.swift
98 lines (75 loc) · 2.8 KB
/
LanguageSelectionViewController.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
private typealias S = LanguageSelectionState
private typealias E = LanguageSelectionState.Event
private typealias Feedback = (Driver<S>) -> Signal<E>
extension LanguageSelectionState {
init() { self.init(currentLanguage: L10n.currentLanguage, hasRegion: API.Region.isSelected) }
}
class LanguageSelectionViewController: ModalTableController<LanguageOption>, IO {
typealias InputType = LanguageSelectionState
typealias OutputType = LanguageChanged
// MARK: -
override func viewDidLoad() {
super.viewDidLoad()
setupModal()
Driver.system(initialState: input,
reduce: S.reduce,
feedback: [bindUI()] + bindData() + bindFlow())
.drive()
.disposed(by: rx_disposeBag)
}
private func setupModal() {
navigationBarLayout = NavigationBarLayout(title: TextData(L10n.selectYourLanguageTitle()))
confirmationTitle = L10n.actionSave()
cancellationTitle = nil
let languages = LanguageOption.available()
let current = languages.index { $0.language == L10n.currentLanguage }
data = ModalData(style: .fullscreen)
set(cells: languages, selection: current)
}
// MARK: - Feedbacks
// MARK: UI
private func bindUI() -> Feedback {
return bind(self) { `self`, state in
let activityPopup = ActivityPopupLayout()
let showActivityPopup = state
.query { $0.showActivityPopup }
.driveNext { activityPopup.display(in: self, isVisible: $0) }
let tappedSaveSubject = PublishSubject<String>()
self.didCompleteSelection = { tappedSaveSubject.onNext($0.language) }
let tappedSave = tappedSaveSubject.map(E.tappedSave).asSignal(onErrorSignalWith: .empty())
return Bindings(subscriptions: [showActivityPopup], mutations: [tappedSave])
}
}
// MARK: Data
private func bindData() -> [Feedback] {
return [
setLanguage(),
loadConfig(),
]
}
private func setLanguage() -> Feedback {
return react(query: { $0.setLanguage }) {
L10n.set(language: $0)
return .just(E.didSetLanguage)
}
}
private func loadConfig() -> Feedback {
return react(query: { $0.loadConfig }) {
API.V1AppConfigGet().request()
.map { _ in E.loadedConfig }
.asSignal(onErrorJustReturn: E.failedLoadingConfig)
}
}
// MARK: Flow
private func bindFlow() -> [Feedback] {
return [
produceOutput(),
]
}
private func produceOutput() -> Feedback {
return react(query: { $0.produceOutput }) { [unowned self] in
self.produce($0)
return .just(E.producedOutput)
}
}
}