-
Notifications
You must be signed in to change notification settings - Fork 223
/
HRPGBuyItemModalViewController.swift
496 lines (444 loc) · 20 KB
/
HRPGBuyItemModalViewController.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
// HRPGBuyItemModalViewController.swift
// Habitica
//
// Created by Elliot Schrock on 8/3/17.
// Copyright © 2017 HabitRPG Inc. All rights reserved.
//
import UIKit
import Habitica_Models
import ReactiveSwift
class HRPGBuyItemModalViewController: UIViewController, Themeable {
@objc var reward: InAppRewardProtocol?
@objc var shopIdentifier: String?
private let inventoryRepository = InventoryRepository()
private let userRepository = UserRepository()
private let disposable = ScopedDisposable(CompositeDisposable())
@IBOutlet weak var topContentView: UIView!
@IBOutlet weak var bottomButtons: UIView!
@IBOutlet weak var hourglassCountView: HRPGCurrencyCountView!
@IBOutlet weak var gemCountView: HRPGCurrencyCountView!
@IBOutlet weak var goldCountView: HRPGCurrencyCountView!
@IBOutlet weak var pinButton: UIButton!
@IBOutlet weak var buyButton: UIView!
@IBOutlet weak var buyLabel: UILabel!
@IBOutlet weak var currencyCountView: HRPGCurrencyCountView!
@IBOutlet weak var closableShopModal: HRPGCloseableShopModalView!
@IBOutlet weak var balanceLabel: UILabel!
@objc public weak var shopViewController: HRPGShopViewController?
private var user: UserProtocol? {
didSet {
refreshBalances()
updateBuyButton()
}
}
private var isPinned: Bool = false {
didSet {
if isPinned {
pinButton.setTitle(L10n.unpinFromRewards, for: .normal)
} else {
pinButton.setTitle(L10n.pinToRewards, for: .normal)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
topContentView.superview?.bringSubviewToFront(topContentView)
bottomButtons.superview?.bringSubviewToFront(bottomButtons)
styleViews()
setupItem()
closableShopModal.closeButton.addTarget(self, action: #selector(closePressed), for: UIControl.Event.touchUpInside)
buyButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(buyPressed)))
let inAppReward = reward
pinButton.isHidden = inAppReward?.pinType == "armoire" || inAppReward?.pinType == "potion"
if #available(iOS 11.0, *) {
pinButton.layer.cornerRadius = 12
buyButton.layer.cornerRadius = 12
if pinButton.isHidden {
buyButton.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
} else {
pinButton.layer.maskedCorners = [ .layerMinXMaxYCorner]
buyButton.layer.maskedCorners = [ .layerMaxXMaxYCorner]
}
}
ThemeService.shared.addThemeable(themable: self)
populateText()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
disposable.inner.add(userRepository.getUser().on(value: {[weak self] user in
self?.user = user
}).start())
}
func populateText() {
pinButton.setTitle(L10n.pinToRewards, for: .normal)
buyLabel.text = L10n.buy.localizedCapitalized
balanceLabel.text = L10n.yourBalance
}
func applyTheme(theme: Theme) {
pinButton.setTitleColor(theme.tintColor, for: .normal)
pinButton.backgroundColor = theme.backgroundTintColor.withAlphaComponent(0.05)
pinButton.layer.borderColor = theme.dimmedColor.cgColor
buyButton.backgroundColor = theme.backgroundTintColor.withAlphaComponent(0.05)
buyButton.layer.borderColor = theme.dimmedColor.cgColor
closableShopModal.closeButton.setTitleColor(theme.tintColor, for: .normal)
closableShopModal.closeButton.backgroundColor = theme.contentBackgroundColor
closableShopModal.shopModalBgView.backgroundColor = theme.contentBackgroundColor
closableShopModal.shopModalBgView.contentView.backgroundColor = theme.contentBackgroundColor
if !itemIsLocked() {
buyLabel.textColor = theme.tintColor
}
view.backgroundColor = theme.backgroundTintColor.darker(by: 50).withAlphaComponent(0.6)
}
func styleViews() {
pinButton.layer.borderWidth = 0.5
buyButton.layer.borderWidth = 0.5
currencyCountView.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
hourglassCountView.currency = .hourglass
gemCountView.currency = .gem
goldCountView.currency = .gold
}
func refreshBalances() {
if let user = self.user {
gemCountView.amount = user.gemCount
goldCountView.amount = Int(user.stats?.gold ?? 0)
if let hourglasses = user.purchased?.subscriptionPlan?.consecutive?.hourglasses {
hourglassCountView.amount = hourglasses
}
}
}
func setupItem() {
if let contentView = closableShopModal.shopModalBgView.contentView {
var itemView: HRPGSimpleShopItemView?
if let reward = self.reward {
itemView = HRPGSimpleShopItemView(withReward: reward, withUser: user, for: contentView)
}
if shopIdentifier == nil {
isPinned = true
}
updateBuyButton()
let key = reward?.key ?? ""
if let itemView = itemView {
switch getPurchaseType() {
case "quests":
setupQuests(contentView, itemView: itemView, key: key)
case "gear":
setupGear(contentView, itemView: itemView, key: key, shopIdentifier: shopIdentifier)
case "mystery_set":
addItemSet(itemView: itemView, to: contentView)
default:
contentView.addSingleViewWithConstraints(itemView)
}
}
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.triggerLayout()
userRepository.getInAppRewards().take(first: 1)
.map({ (rewards, _) in
return rewards.map({ (reward) in
return reward.key
})
}).on(value: {[weak self]rewards in
self?.isPinned = rewards.contains(self?.reward?.key)
}).start()
}
}
private func setupQuests(_ contentView: UIView, itemView: UIView, key: String) {
let questView = QuestDetailView(frame: CGRect.zero)
inventoryRepository.getQuest(key: key).take(first: 1).skipNil().on(value: { quest in
questView.configure(quest: quest)
}).start()
addItemAndDetails(itemView, questView, to: contentView)
}
private func setupGear(_ contentView: UIView, itemView: UIView, key: String, shopIdentifier: String?) {
if let identifier = shopIdentifier, identifier == Constants.TimeTravelersShopKey {
addItemSet(itemView: itemView, to: contentView)
} else {
let statsView = HRPGItemStatsView(frame: CGRect.zero)
inventoryRepository.getGear(keys: [key])
.take(first: 1)
.map { (gear, _) -> GearProtocol? in
return gear.first
}
.skipNil()
.on(value: { gear in
statsView.configure(gear: gear)
}).start()
addItemAndDetails(itemView, statsView, to: contentView)
}
}
func updateBuyButton() {
var isLocked = itemIsLocked()
if let reward = self.reward {
if let currencyString = reward.currency, let currency = Currency(rawValue: currencyString) {
currencyCountView.currency = currency
if reward.key == "gem" && user?.purchased?.subscriptionPlan?.gemsRemaining == 0 {
isLocked = true
}
} else {
currencyCountView.currency = .gold
}
currencyCountView.amount = Int(reward.value)
}
if canAfford() && !isLocked {
currencyCountView.state = .normal
} else {
if currencyCountView.currency == .gold {
buyLabel.textColor = .gray400
}
if isLocked {
currencyCountView.state = .locked
} else {
currencyCountView.state = .cantAfford
}
}
buyButton.shouldGroupAccessibilityChildren = true
buyButton.isAccessibilityElement = true
currencyCountView.isAccessibilityElement = false
buyButton.accessibilityLabel = L10n.buyForX(currencyCountView.accessibilityLabel ?? "")
}
func canAfford() -> Bool {
var currency: Currency?
var price: Float = 0.0
if let inAppReward = reward {
if let currencyString = inAppReward.currency {
currency = Currency(rawValue: currencyString)
} else {
currency = Currency.gold
}
price = inAppReward.value
}
if let user = self.user, let selectedCurrency = currency {
switch selectedCurrency {
case .gold:
return price <= user.stats?.gold ?? 0
case .gem:
return price <= Float(user.gemCount)
case .hourglass:
return price <= Float(user.purchased?.subscriptionPlan?.consecutive?.hourglasses ?? 0)
}
}
return false
}
func itemIsLocked() -> Bool {
var isLocked = false
if let inAppReward = reward {
isLocked = inAppReward.locked
}
return isLocked
}
func canBuy() -> Bool {
return canAfford() && !itemIsLocked()
}
func getPurchaseType() -> String {
if let reward = self.reward {
return reward.purchaseType ?? ""
} else {
return ""
}
}
func addItemAndDetails(_ itemView: UIView, _ detailView: UIView, to contentView: UIView) {
let views = ["itemView": itemView, "detailView": detailView]
contentView.addSubview(itemView)
contentView.addSubview(detailView)
contentView.addConstraints(NSLayoutConstraint.defaultHorizontalConstraints(itemView))
contentView.addConstraints(NSLayoutConstraint.defaultVerticalConstraints("V:|-0-[itemView]-0-[detailView]-20-|", views))
contentView.addConstraints(NSLayoutConstraint.defaultHorizontalConstraints(detailView))
}
func addItemSet(itemView: UIView, to contentView: UIView) {
contentView.addSubview(itemView)
contentView.addConstraints(NSLayoutConstraint.defaultHorizontalConstraints(itemView))
contentView.addConstraints(NSLayoutConstraint.defaultVerticalConstraints("V:|-0-[itemView]-20-|", ["itemView": itemView]))
}
// MARK: actions
@IBAction func pinPressed() {
var path = ""
var pinType = ""
if let inAppReward = reward, inAppReward.isValid {
path = inAppReward.path ?? ""
pinType = inAppReward.pinType ?? ""
}
inventoryRepository.togglePinnedItem(pinType: pinType, path: path).observeValues {[weak self] (_) in
self?.isPinned = !(self?.isPinned ?? false)
}
}
//swiftlint:disable function_body_length
//swiftlint:disable cyclomatic_complexity
@objc
func buyPressed() {
if itemIsLocked() {
return
}
var key = ""
var purchaseType = ""
var currency = Currency.gold
var setIdentifier = ""
var value = 0
var successBlock = {}
var text = ""
/*if let shopItem = item {
key = shopItem.key ?? ""
purchaseType = shopItem.purchaseType ?? ""
text = shopItem.text ?? ""
imageName = shopItem.imageName ?? ""
setIdentifier = shopItem.category?.identifier ?? shopItem.key ?? ""
value = shopItem.value?.intValue ?? 0
if let currencyString = shopItem.currency, let thisCurrency = Currency(rawValue: currencyString) {
currency = thisCurrency
}
successBlock = {
if purchaseType == "gear" {
self.inventoryRepository.retrieveShopInventory(identifier: GearMarketKey).observeCompleted {}
} else if let identifier = self.shopIdentifier {
self.inventoryRepository.retrieveShopInventory(identifier: identifier).observeCompleted {}
}
}
} else*/ if let inAppReward = reward {
key = inAppReward.key ?? ""
purchaseType = inAppReward.purchaseType ?? ""
setIdentifier = inAppReward.key ?? ""
value = Int(inAppReward.value)
text = inAppReward.text ?? ""
if let currencyString = inAppReward.currency, let thisCurrency = Currency(rawValue: currencyString) {
currency = thisCurrency
}
successBlock = {
self.userRepository.retrieveInAppRewards().observeCompleted {}
}
}
if key.isEmpty == false {
self.dismiss(animated: true, completion: nil)
let topViewController = self.presentingViewController
if !canBuy() {
var viewControllerName: String?
if !canAfford() {
if currency == .hourglass {
viewControllerName = "InsufficientHourglassesViewController"
} else if currency == .gem {
viewControllerName = "InsufficientGemsViewController"
} else {
viewControllerName = "InsufficientGoldViewController"
}
} else if key == "gem" {
viewControllerName = "GemCapReachedViewController"
}
if let name = viewControllerName {
HRPGBuyItemModalViewController.displayViewController(name: name, parent: topViewController)
}
return
}
if currency == .hourglass {
if purchaseType == "gear" || purchaseType == "mystery_set" {
inventoryRepository.purchaseMysterySet(identifier: setIdentifier, text: text).observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientHourglassesViewController", parent: topViewController)
}
})
} else {
inventoryRepository.purchaseHourglassItem(purchaseType: purchaseType, key: key, text: text).observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientHourglassesViewController", parent: topViewController)
}
})
}
} else if currency == .gem || purchaseType == "gems" {
inventoryRepository.purchaseItem(purchaseType: purchaseType, key: key, value: value, text: text).observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
if key == "gem" {
HRPGBuyItemModalViewController.displayViewController(name: "GemCapReachedViewController", parent: topViewController)
} else {
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGemsViewController", parent: topViewController)
}
}
})
} else if purchaseType == "fortify" {
userRepository.reroll().observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
})
} else {
if currency == .gold && purchaseType == "quests" {
inventoryRepository.purchaseQuest(key: key, text: text).observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
})
} else if (purchaseType == "debuffPotion") {
userRepository.useDebuffItem(key: key).observeResult { (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
}
} else {
inventoryRepository.buyObject(key: key, price: value, text: text).observeResult({ (result) in
switch result {
case .success(_):
successBlock()
case .failure(_):
HRPGBuyItemModalViewController.displayViewController(name: "InsufficientGoldViewController", parent: topViewController)
}
})
}
}
}
}
private static func displayViewController(name: String, parent: UIViewController?) {
let storyboard = UIStoryboard(name: "BuyModal", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: name)
viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = .overFullScreen
parent?.present(viewController, animated: true, completion: nil)
}
@objc
func closePressed() {
dismiss(animated: true, completion: nil)
}
override func viewWillLayoutSubviews() {
closableShopModal.shopModalBgView.maxHeightConstraint.constant = view.frame.size.height - 200
}
}
extension NSLayoutConstraint {
static func defaultVerticalConstraints(_ visualFormat: String, _ views: [String: UIView]) -> [NSLayoutConstraint] {
return NSLayoutConstraint.constraints(withVisualFormat: visualFormat,
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: views)
}
static func defaultHorizontalConstraints(_ view: UIView) -> [NSLayoutConstraint] {
return NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: ["view": view])
}
}
extension UIView {
func addSingleViewWithConstraints(_ view: UIView) {
addSubview(view)
addConstraints(NSLayoutConstraint.defaultVerticalConstraints("V:|-0-[view]-0-|", ["view": view]))
addConstraints(NSLayoutConstraint.defaultHorizontalConstraints(view))
}
func triggerLayout() {
setNeedsUpdateConstraints()
updateConstraints()
setNeedsLayout()
layoutIfNeeded()
}
}