-
Notifications
You must be signed in to change notification settings - Fork 225
/
AvatarDetailViewController.swift
204 lines (187 loc) · 9.65 KB
/
AvatarDetailViewController.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
//
// AvatarDetailViewController.swift
// Habitica
//
// Created by Phillip Thelen on 20.04.18.
// Copyright © 2018 HabitRPG Inc. All rights reserved.
//
import UIKit
import Habitica_Models
import SwiftUI
class AvatarDetailViewController: BaseCollectionViewController, UICollectionViewDelegateFlowLayout {
private var customizationDataSource: AvatarDetailViewDataSource?
private var gearDataSource: AvatarGearDetailViewDataSource?
private let userRepository = UserRepository()
private let inventoryRepository = InventoryRepository()
private let customizationRepository = CustomizationRepository()
private let configRepository = ConfigRepository.shared
var customizationGroup: String?
var customizationType: String?
private var newCustomizationLayout = false
override func viewDidLoad() {
super.viewDidLoad()
topHeaderCoordinator?.followScrollView = false
newCustomizationLayout = configRepository.bool(variable: .enableCustomizationShop) || configRepository.testingLevel.isDeveloper
if let type = customizationType {
if type == "eyewear" || type == "headAccessory" || type == "back" || type == "animalTails" {
gearDataSource = AvatarGearDetailViewDataSource(type: type)
gearDataSource?.collectionView = collectionView
} else {
customizationDataSource = AvatarDetailViewDataSource(type: type, group: customizationGroup, newCustomizationLayout: newCustomizationLayout)
customizationDataSource?.collectionView = collectionView
customizationDataSource?.purchaseSet = {[weak self] set in
self?.showPurchaseDialog(customizationSet: set, withSource: nil)
}
}
}
HabiticaAnalytics.shared.logNavigationEvent("navigated \(customizationType ?? "") screen")
}
override func applyTheme(theme: Theme) {
super.applyTheme(theme: theme)
collectionView.backgroundColor = theme.contentBackgroundColor
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let datasource = customizationDataSource {
return datasource.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
} else if let datasource = gearDataSource {
return datasource.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
}
return CGSize.zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let width = 80
let viewWidth = Int(collectionView.frame.size.width)
var count = 3
if let dataSource = gearDataSource {
let inSection = dataSource.collectionView(collectionView, numberOfItemsInSection: section)
if inSection < count {
count = inSection
}
} else if let dataSource = customizationDataSource {
let inSection = dataSource.collectionView(collectionView, numberOfItemsInSection: section)
if inSection < count {
count = inSection
}
}
let totalWidth = width * count + (10 * (count-1))
let spacing = CGFloat(viewWidth - totalWidth) / 2
return UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: spacing)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
if let datasource = customizationDataSource, let customization = datasource.item(at: indexPath) {
if !customization.isPurchasable || datasource.owns(customization: customization) == true {
userRepository.updateUser(key: customization.userPath, value: customization.key ?? "").observeCompleted {}
} else {
if customization.set?.key?.contains("timeTravel") == true {
showTimeTravelDialog()
} else {
showPurchaseDialog(customization: customization, withSource: cell)
}
}
} else if let datasource = gearDataSource, let gear = datasource.item(at: indexPath) {
if datasource.owns(gear: gear) {
inventoryRepository.equip(type: datasource.preferences?.useCostume == true ? "costume" : "equipped", key: gear.key ?? "").observeCompleted {}
} else {
showPurchaseDialog(gear: gear, withSource: cell)
}
}
}
private func showPurchaseDialog(customization: CustomizationProtocol, withSource sourceView: UIView?) {
let sheet = HostingBottomSheetController(rootView: BottomSheetMenu(Text(customization.titleText), iconName: customization.iconName(forUserPreferences: nil) ?? "", menuItems: {
BottomSheetMenuitem(title: HStack {
Text(L10n.purchaseForWithoutCurrency(Int(customization.price)))
Image(uiImage: HabiticaIcons.imageOfGem)
}) {[weak self] in
if self?.customizationDataSource?.canAfford(price: customization.price) != true {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: {
HRPGBuyItemModalViewController.displayInsufficientGemsModal(reason: "customization", delayDisplay: false)
})
return
}
self?.customizationRepository.unlock(customization: customization, value: customization.price).observeCompleted {}
}
})
)
present(sheet, animated: true)
}
private func showPurchaseDialog(gear: GearProtocol, withSource sourceView: UIView?) {
var value = Int(gear.value)
if gear.gearSet == "animal" {
value = 2
}
let sheet = HostingBottomSheetController(rootView: BottomSheetMenu(menuItems: {
BottomSheetMenuitem(title: HStack {
Text(L10n.purchaseForWithoutCurrency(value))
Image(uiImage: HabiticaIcons.imageOfGem)
}) {[weak self] in
self?.customizationRepository.unlock(gear: gear, value: value).observeCompleted {}
}
})
)
present(sheet, animated: true)
}
private func showPurchaseDialog(customizationSet: CustomizationSetProtocol, withSource sourceView: UIView?) {
var text = customizationSet.text ?? ""
if customizationType == "background", let key = customizationSet.key?.replacingOccurrences(of: "backgrounds", with: "") {
let index = key.index(key.startIndex, offsetBy: 2)
let month = Int(key[..<index]) ?? 0
let year = Int(key[index...]) ?? 0
let dateFormatter = DateFormatter()
let monthName = month > 0 ? dateFormatter.monthSymbols[month-1] : ""
text = "\(monthName) \(year) Backgrounds"
}
let sheet = HostingBottomSheetController(rootView: BottomSheetMenu(Text(text), menuItems: {
BottomSheetMenuitem(title: HStack {
Text(L10n.purchaseForWithoutCurrency(Int(customizationSet.setPrice)))
Image(uiImage: HabiticaIcons.imageOfGem)
}) {[weak self] in
if self?.customizationDataSource?.canAfford(price: customizationSet.setPrice) != true {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: {
HRPGBuyItemModalViewController.displayInsufficientGemsModal(reason: "customization", delayDisplay: false)
})
return
}
self?.customizationRepository.unlock(customizationSet: customizationSet, value: customizationSet.setPrice).observeCompleted {}
}
})
)
present(sheet, animated: true)
}
private func showTimeTravelDialog() {
let alertController = HabiticaAlertController(title: L10n.purchaseCustomization, message: L10n.purchaseFromTimeTravelersShop)
alertController.addAction(title: L10n.goShopping, isMainAction: true) { _ in
let storyboard = UIStoryboard(name: "Shop", bundle: nil)
if let viewController = storyboard.instantiateInitialViewController() as? ShopViewController {
viewController.shopIdentifier = Constants.TimeTravelersShopKey
self.navigationController?.pushViewController(viewController, animated: true)
}
}
alertController.addCancelAction()
alertController.show()
}
}
extension CustomizationProtocol {
var titleText: String {
if type == "background" {
return text ?? ""
} else {
if path.contains("skin") {
return L10n.Inventory.avatarSkinCustomization
} else if path.contains("shirt") {
return L10n.Inventory.avatarShirtCustomization
} else if path.contains("color") {
return L10n.Inventory.avatarHairColorCustomization
} else if path.contains("base") {
return L10n.Inventory.avatarHairStyleCustomization
} else if path.contains("bangs") {
return L10n.Inventory.avatarBangsCustomization
} else if path.contains("beard") {
return L10n.Inventory.avatarBeardCustomization
} else if path.contains("mustache") {
return L10n.Inventory.avatarMustacheCustomization
}
}
return ""
}
}