Skip to content

Commit 7d58c77

Browse files
committed
Save item variants
1 parent a3af1a1 commit 7d58c77

File tree

6 files changed

+76
-18
lines changed

6 files changed

+76
-18
lines changed

ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@
11611161
CODE_SIGN_ENTITLEMENTS = ACHNBrowserUI/ACHNBrowserUI.entitlements;
11621162
CODE_SIGN_IDENTITY = "Apple Development: Thomas Ricouard (7MB55D6BJ5)";
11631163
CODE_SIGN_STYLE = Manual;
1164-
CURRENT_PROJECT_VERSION = 052572020;
1164+
CURRENT_PROJECT_VERSION = 052582020;
11651165
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
11661166
DEVELOPMENT_TEAM = Z6P74P6T99;
11671167
ENABLE_PREVIEWS = YES;
@@ -1192,7 +1192,7 @@
11921192
CODE_SIGN_IDENTITY = "iPhone Developer";
11931193
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
11941194
CODE_SIGN_STYLE = Manual;
1195-
CURRENT_PROJECT_VERSION = 052572020;
1195+
CURRENT_PROJECT_VERSION = 052582020;
11961196
DEVELOPMENT_ASSET_PATHS = "\"ACHNBrowserUI/Preview Content\"";
11971197
DEVELOPMENT_TEAM = Z6P74P6T99;
11981198
ENABLE_PREVIEWS = YES;

ACHNBrowserUI/ACHNBrowserUI/packages/Backend/Sources/Backend/environments/UserCollection.swift

+35-1
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ public class UserCollection: ObservableObject {
1515

1616
// MARK: - Published properties
1717
@Published public var items: [Item] = []
18+
@Published public var variants: [String: [Variant]] = [:]
1819
@Published public var villagers: [Villager] = []
1920
@Published public var critters: [Item] = []
2021
@Published public var lists: [UserList] = []
2122
@Published public var dailyTasks = DailyTasks()
23+
2224
@Published public var isCloudEnabled = true
2325
@Published public var isSynched = false
2426

2527
// MARK: - Private properties
2628
private struct SavedData: Codable {
2729
let items: [Item]
30+
let variants: [String: [Variant]]?
2831
let villagers: [Villager]
2932
let critters: [Item]
3033
let lists: [UserList]?
@@ -80,6 +83,31 @@ public class UserCollection: ObservableObject {
8083
return added
8184
}
8285

86+
public func containVariant(item: Item, variant: Variant) -> Bool {
87+
guard let filename = item.filename else {
88+
return false
89+
}
90+
return variants[filename]?.contains(variant) == true
91+
}
92+
93+
public func toggleVariant(item: Item, variant: Variant) -> Bool {
94+
guard let filename = item.filename else {
95+
return false
96+
}
97+
if variants[filename]?.contains(variant) == true {
98+
variants[filename]?.removeAll(where: { $0 == variant })
99+
save()
100+
return false
101+
} else {
102+
if variants[filename] == nil {
103+
variants[filename] = []
104+
}
105+
variants[filename]?.append(variant)
106+
save()
107+
return true
108+
}
109+
}
110+
83111
public func toggleCritters(critter: Item) -> Bool {
84112
let added = critters.toggle(item: critter)
85113
save()
@@ -230,7 +258,12 @@ public class UserCollection: ObservableObject {
230258
// MARK: - Import / Export
231259
private func save() {
232260
do {
233-
let savedData = SavedData(items: items, villagers: villagers, critters: critters, lists: lists, dailyTasks: dailyTasks)
261+
let savedData = SavedData(items: items,
262+
variants: variants,
263+
villagers: villagers,
264+
critters: critters,
265+
lists: lists,
266+
dailyTasks: dailyTasks)
234267
let data = try encoder.encode(savedData)
235268
try data.write(to: filePath, options: .atomicWrite)
236269

@@ -250,6 +283,7 @@ public class UserCollection: ObservableObject {
250283
do {
251284
let savedData = try decoder.decode(SavedData.self, from: data)
252285
self.items = savedData.items
286+
self.variants = savedData.variants ?? [:]
253287
self.villagers = savedData.villagers
254288
self.critters = savedData.critters
255289
self.lists = savedData.lists ?? []

ACHNBrowserUI/ACHNBrowserUI/views/items/ItemRowView.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ struct ItemRowView: View {
110110
var body: some View {
111111
HStack(spacing: 8) {
112112
if displayMode != .largeNoButton {
113-
LikeButtonView(item: item).environmentObject(collection)
113+
LikeButtonView(item: item,
114+
variant: displayedVariant).environmentObject(collection)
114115
}
115116
if item.finalImage == nil && displayedVariant == nil {
116117
Image(item.appCategory.iconName())

ACHNBrowserUI/ACHNBrowserUI/views/items/detail/ItemDetailView.swift

+22-11
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ extension ItemDetailView {
119119

120120
private var navButtons: some View {
121121
HStack {
122-
LikeButtonView(item: self.itemViewModel.item).imageScale(.large)
122+
LikeButtonView(item: itemViewModel.item,
123+
variant: displayedVariant).imageScale(.large)
123124
.environmentObject(collection)
124125
.safeHoverEffectBarItem(position: .trailing)
125126
Spacer(minLength: 12)
@@ -132,23 +133,33 @@ extension ItemDetailView {
132133
ScrollView(.horizontal, showsIndicators: false) {
133134
HStack {
134135
itemViewModel.item.variations.map { variants in
135-
ForEach(variants) { variant in
136-
ItemImage(path: variant.content.image,
137-
size: 75)
138-
.onTapGesture {
139-
withAnimation {
140-
FeedbackGenerator.shared.triggerSelection()
141-
self.displayedVariant = variant
142-
}
143-
}
144-
}
136+
ForEach(variants, content: makeVariantRow(variant:))
145137
}
146138
}.padding()
147139
}
148140
.listRowInsets(EdgeInsets())
149141
}
150142
}
151143

144+
private func makeVariantRow(variant: Variant) -> some View {
145+
ZStack(alignment: .topLeading) {
146+
ItemImage(path: variant.content.image,
147+
size: 75)
148+
.onTapGesture {
149+
withAnimation {
150+
if self.itemViewModel.item.variations?.firstIndex(of: variant) == 0 {
151+
self.displayedVariant = nil
152+
} else {
153+
self.displayedVariant = variant
154+
}
155+
FeedbackGenerator.shared.triggerSelection()
156+
}
157+
}
158+
LikeButtonView(item: item,
159+
variant: self.itemViewModel.item.variations?.firstIndex(of: variant) == 0 ? nil : variant)
160+
}
161+
}
162+
152163
private var materialsSection: some View {
153164
Section(header: SectionHeaderView(text: "Materials", icon: "leaf.arrow.circlepath")) {
154165
ScrollView(.horizontal, showsIndicators: false) {

ACHNBrowserUI/ACHNBrowserUI/views/shared/LikeButtonView.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@ import Backend
1111

1212
struct LikeButtonView: View {
1313
let item: Item?
14+
let variant: Variant?
1415
let villager: Villager?
1516
@EnvironmentObject private var collection: UserCollection
1617

17-
init(item: Item) {
18+
init(item: Item, variant: Variant?) {
1819
self.item = item
20+
self.variant = variant
1921
self.villager = nil
2022
}
2123

2224
init(villager: Villager) {
2325
self.villager = villager
26+
self.variant = nil
2427
self.item = nil
2528
}
2629

2730
private var isInCollection: Bool {
2831
if let item = item {
32+
if let variant = variant {
33+
return collection.containVariant(item: item, variant: variant)
34+
}
2935
return collection.items.contains(item) || collection.critters.contains(item)
3036
} else if let villager = villager {
3137
return collection.villagers.contains(villager)
@@ -63,6 +69,11 @@ struct LikeButtonView: View {
6369
let added = self.collection.toggleCritters(critter: item)
6470
FeedbackGenerator.shared.triggerNotification(type: added ? .success : .warning)
6571
default:
72+
if let variant = self.variant {
73+
let added = self.collection.toggleVariant(item: item, variant: variant)
74+
FeedbackGenerator.shared.triggerNotification(type: added ? .success : .warning)
75+
return
76+
}
6677
let added = self.collection.toggleItem(item: item)
6778
FeedbackGenerator.shared.triggerNotification(type: added ? .success : .warning)
6879
}
@@ -81,7 +92,7 @@ struct LikeButtonView: View {
8192

8293
struct StarButtonView_Previews: PreviewProvider {
8394
static var previews: some View {
84-
LikeButtonView(item: static_item)
95+
LikeButtonView(item: static_item, variant: nil)
8596
.environmentObject(UserCollection.shared)
8697
}
8798
}

ACHNBrowserUI/ACHNBrowserUI/views/villagers/VillagerRowView.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ struct VillagerRowView: View {
1717

1818
var body: some View {
1919
HStack {
20-
LikeButtonView(villager: villager).environmentObject(collection)
20+
LikeButtonView(villager: villager)
21+
.environmentObject(collection)
2122
ItemImage(path: ACNHApiService.BASE_URL.absoluteString +
2223
ACNHApiService.Endpoint.villagerIcon(id: villager.id).path(),
2324
size: 50)

0 commit comments

Comments
 (0)