-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathThanksViewController.swift
278 lines (228 loc) · 9.56 KB
/
ThanksViewController.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import KsApi
import Library
import Prelude
import ReactiveSwift
import ReactiveExtensions
import Social
import UIKit
internal final class ThanksViewController: UIViewController, UICollectionViewDelegate {
@IBOutlet fileprivate weak var facebookButton: UIButton!
@IBOutlet fileprivate weak var twitterButton: UIButton!
@IBOutlet fileprivate weak var shareMoreButton: UIButton!
@IBOutlet fileprivate weak var doneButton: UIBarButtonItem!
@IBOutlet fileprivate weak var projectsCollectionView: UICollectionView!
@IBOutlet fileprivate weak var backedLabel: UILabel!
@IBOutlet fileprivate weak var recommendationsLabel: UILabel!
@IBOutlet fileprivate weak var woohooLabel: UILabel!
fileprivate let viewModel: ThanksViewModelType = ThanksViewModel()
fileprivate let shareViewModel: ShareViewModelType = ShareViewModel()
fileprivate let dataSource = ThanksProjectsDataSource()
internal static func configuredWith(project: Project) -> ThanksViewController {
let vc = Storyboard.Thanks.instantiate(ThanksViewController.self)
vc.viewModel.inputs.project(project)
vc.shareViewModel.inputs.configureWith(shareContext: .thanks(project))
return vc
}
override func viewDidLoad() {
super.viewDidLoad()
self.projectsCollectionView.dataSource = self.dataSource
self.projectsCollectionView.delegate = self
self.viewModel.inputs.facebookIsAvailable(
SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook)
)
self.viewModel.inputs.twitterIsAvailable(
SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter)
)
self.viewModel.inputs.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: animated)
}
override func bindStyles() {
super.bindStyles()
_ = self |> baseControllerStyle()
_ = self.woohooLabel
|> UILabel.lens.textColor .~ .white
|> UILabel.lens.font .~ UIFont.ksr_title2().bolded
|> UILabel.lens.text %~ { _ in Strings.project_checkout_share_exclamation() }
|> UILabel.lens.isAccessibilityElement .~ false
_ = self.backedLabel |> UILabel.lens.textColor .~ .ksr_text_navy_900
_ = self.recommendationsLabel
|> UILabel.lens.textColor .~ .ksr_text_navy_900
|> UILabel.lens.font .~ .ksr_subhead()
|> UILabel.lens.text %~ { _ in Strings.project_checkout_looking_for_more_projects_check_these_out() }
_ = self.facebookButton
|> facebookThanksButtonStyle
|> UIButton.lens.targets .~ [(self, #selector(facebookButtonTapped), .touchUpInside)]
|> UIButton.lens.accessibilityLabel %~ { _ in Strings.Share_this_project_on_Facebook() }
_ = self.twitterButton
|> twitterButtonStyle
|> UIButton.lens.targets .~ [(self, #selector(twitterButtonTapped), .touchUpInside)]
|> UIButton.lens.accessibilityLabel %~ { _ in Strings.Share_this_project_on_Twitter() }
_ = self.shareMoreButton
|> borderButtonStyle
|> UIButton.lens.targets .~ [(self, #selector(shareMoreButtonTapped), .touchUpInside)]
|> UIButton.lens.title(forState: .normal) %~ { _ in
Strings.project_checkout_share_buttons_more_share_options()
}
_ = self.doneButton
|> doneBarButtonItemStyle
|> UIBarButtonItem.lens.targetAction .~ (self, #selector(doneButtonTapped))
}
// swiftlint:disable function_body_length
override func bindViewModel() {
super.bindViewModel()
self.facebookButton.rac.hidden = self.viewModel.outputs.facebookButtonIsHidden
self.twitterButton.rac.hidden = self.viewModel.outputs.twitterButtonIsHidden
self.backedLabel.rac.attributedText = self.viewModel.outputs.backedProjectText
self.viewModel.outputs.dismissToRootViewController
.observeForControllerAction()
.observeValues { [weak self] in
self?.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
self.viewModel.outputs.goToDiscovery
.observeForControllerAction()
.observeValues { [weak self] params in
self?.goToDiscovery(params: params)
}
self.viewModel.outputs.goToAppStoreRating
.observeForControllerAction()
.observeValues { [weak self] link in
self?.goToAppStore(link: link)
}
self.viewModel.outputs.goToProject
.observeForControllerAction()
.observeValues { [weak self] project, projects, refTag in
self?.goToProject(project, projects: projects, refTag: refTag)
}
self.viewModel.outputs.showRatingAlert
.observeForControllerAction()
.observeValues { [weak self] in
self?.showRatingAlert()
}
self.viewModel.outputs.showGamesNewsletterAlert
.observeForControllerAction()
.observeValues { [weak self] in
self?.showGamesNewsletterAlert()
}
self.viewModel.outputs.showGamesNewsletterOptInAlert
.observeForControllerAction()
.observeValues { [weak self] title in
self?.showGamesNewsletterOptInAlert(title: title)
}
self.viewModel.outputs.updateUserInEnvironment
.observeValues { [weak self] user in
AppEnvironment.updateCurrentUser(user)
self?.viewModel.inputs.userUpdated()
}
self.viewModel.outputs.postUserUpdatedNotification
.observeValues(NotificationCenter.default.post)
self.viewModel.outputs.showRecommendations
.observeForControllerAction()
.observeValues { [weak self] projects, category in
self?.dataSource.loadData(projects: projects, category: category)
self?.projectsCollectionView.reloadData()
}
self.shareViewModel.outputs.showShareSheet
.observeForControllerAction()
.observeValues { [weak self] in self?.showShareSheet($0) }
self.shareViewModel.outputs.showShareCompose
.observeForControllerAction()
.observeValues { [weak self] in self?.showShareCompose($0) }
}
// swiftlint:enable function_body_length
fileprivate func goToDiscovery(params: DiscoveryParams) {
self.view.window?.rootViewController
.flatMap { $0 as? RootTabBarViewController }
.doIfSome { $0.switchToDiscovery(params: params) }
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
fileprivate func goToAppStore(link: String) {
guard let url = URL(string: link) else { return }
UIApplication.shared.openURL(url)
}
fileprivate func goToProject(_ project: Project, projects: [Project], refTag: RefTag) {
let vc = ProjectNavigatorViewController.configuredWith(project: project,
refTag: refTag,
initialPlaylist: projects,
navigatorDelegate: self)
self.present(vc, animated: true, completion: nil)
}
fileprivate func showRatingAlert() {
self.present(
UIAlertController.rating(
yesHandler: { [weak self] action in
self?.viewModel.inputs.rateNowButtonTapped()
}, remindHandler: { [weak self] action in
self?.viewModel.inputs.rateRemindLaterButtonTapped()
}, noHandler: { [weak self] action in
self?.viewModel.inputs.rateNoThanksButtonTapped()
}),
animated: true,
completion: nil
)
}
fileprivate func showGamesNewsletterAlert() {
self.present(
UIAlertController.games(
subscribeHandler: { [weak self] _ in
self?.viewModel.inputs.gamesNewsletterSignupButtonTapped()
}),
animated: true,
completion: nil
)
}
fileprivate func showGamesNewsletterOptInAlert(title: String) {
self.present(
UIAlertController.newsletterOptIn(title),
animated: true,
completion: nil
)
}
fileprivate func showShareSheet(_ controller: UIActivityViewController) {
controller.completionWithItemsHandler = { [weak self] activityType, completed, returnedItems, error in
self?.shareViewModel.inputs.shareActivityCompletion(
with: .init(activityType: activityType,
completed: completed,
returnedItems: returnedItems,
activityError: error)
)
}
if UIDevice.current.userInterfaceIdiom == .pad {
controller.modalPresentationStyle = .popover
let popover = controller.popoverPresentationController
popover?.sourceView = self.shareMoreButton
}
self.present(controller, animated: true, completion: nil)
}
fileprivate func showShareCompose(_ controller: SLComposeViewController) {
controller.completionHandler = { [weak self] in
self?.shareViewModel.inputs.shareComposeCompletion(result: $0)
}
self.present(controller, animated: true, completion: nil)
}
internal func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
if let project = self.dataSource.projectAtIndexPath(indexPath) {
self.viewModel.inputs.projectTapped(project)
} else if let category = self.dataSource.categoryAtIndexPath(indexPath) {
self.viewModel.inputs.categoryCellTapped(category)
}
}
@objc fileprivate func facebookButtonTapped() {
self.shareViewModel.inputs.facebookButtonTapped()
}
@objc fileprivate func twitterButtonTapped() {
self.shareViewModel.inputs.twitterButtonTapped()
}
@objc fileprivate func shareMoreButtonTapped() {
self.shareViewModel.inputs.shareButtonTapped()
}
@objc fileprivate func doneButtonTapped() {
self.viewModel.inputs.closeButtonTapped()
}
}
extension ThanksViewController: ProjectNavigatorDelegate {
func transitionedToProject(at index: Int) {}
}