diff --git a/HackIllinois/ViewControllers/HIPointsShopSwiftUIView.swift b/HackIllinois/ViewControllers/HIPointsShopSwiftUIView.swift index 109f987c..09559865 100644 --- a/HackIllinois/ViewControllers/HIPointsShopSwiftUIView.swift +++ b/HackIllinois/ViewControllers/HIPointsShopSwiftUIView.swift @@ -32,6 +32,7 @@ class PointShopManager: ObservableObject { do { let (containedItem, _) = try result.get() self.items = containedItem.items + print("Loaded point shop items") } catch { print("Failed to preload point shop items with error: \(error)") } @@ -264,7 +265,7 @@ struct HIPointShopSwiftUIView: View { } Spacer() } - if startFetchingQR { + if startFetchingQR && !cartManager.items.isEmpty { VStack { Text("SCAN HERE TO COMPLETE PURCHASE") .padding(.bottom, 50) @@ -276,6 +277,14 @@ struct HIPointShopSwiftUIView: View { .scaledToFit() .frame(width: 250 * resizeFactor[0], height: 250 * resizeFactor[0]) } + } else if cartManager.items.isEmpty { + Text("CART IS EMPTY") + .font(Font.custom("Montserrat", size: 24).weight(.bold)) + .padding() + .background( + RoundedRectangle(cornerRadius: 12) + .fill(Color(red: 255 / 255, green: 247 / 255, blue: 240 / 255)) + ) } } .overlay(showError ? ErrorPopup(title: errorMessage[0], description: errorMessage[1], show: $showError) : nil) @@ -290,7 +299,6 @@ struct HIPointShopSwiftUIView: View { DispatchQueue.main.async { do { let (containedItem, _) = try result.get() - // ✅ Update the manager’s items shopManager.items = containedItem.items } catch { print("Failed to reload points shop: \(error)") @@ -411,6 +419,7 @@ struct PointShopItemCell: View { let item: Item @Binding var showError: Bool @Binding var errorMessage: [String] + @State private var image: UIImage? = nil var body: some View { ZStack { @@ -424,15 +433,24 @@ struct PointShopItemCell: View { .font(.caption) .foregroundColor(.white) .multilineTextAlignment(.center) - .frame(width: 75 * (UIScreen.main.bounds.width/428)) + .frame(width: 100 * (UIScreen.main.bounds.width / 428)) + .lineLimit(2) // Allows up to two lines + .fixedSize(horizontal: false, vertical: true) // Item image - Image(systemName: "Profile0") - .data(url: URL(string: item.imageURL)!) - .resizable() - .scaledToFit() - .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) - + if let image = image { + Image(uiImage: image) + .resizable() + .scaledToFit() + .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) + } else { + ProgressView() // Placeholder while loading + .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) + .onAppear { + loadImage() + } + } + // Price + quantity HStack(spacing: 4) { Image("Coin") @@ -477,6 +495,18 @@ struct PointShopItemCell: View { alignment: .topTrailing ) } + + private func loadImage() { + guard let url = URL(string: item.imageURL) else { return } + + URLSession.shared.dataTask(with: url) { data, response, error in + if let data = data, let loadedImage = UIImage(data: data) { + DispatchQueue.main.async { + self.image = loadedImage + } + } + }.resume() + } } struct CartItemCell: View { @@ -484,6 +514,7 @@ struct CartItemCell: View { let item: Item @Binding var showError: Bool @Binding var errorMessage: [String] + @State private var image: UIImage? = nil var body: some View { ZStack { @@ -497,14 +528,23 @@ struct CartItemCell: View { .font(.caption) .foregroundColor(.white) .multilineTextAlignment(.center) - .frame(width: 75 * (UIScreen.main.bounds.width/428)) + .frame(width: 100 * (UIScreen.main.bounds.width / 428)) + .lineLimit(2) // Allows up to two lines + .fixedSize(horizontal: false, vertical: true) // Item image - Image(systemName: "Profile0") - .data(url: URL(string: item.imageURL)!) - .resizable() - .scaledToFit() - .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) + if let image = image { + Image(uiImage: image) + .resizable() + .scaledToFit() + .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) + } else { + ProgressView() // Placeholder while loading + .frame(width: 60 * (UIScreen.main.bounds.width/428), height: 60 * (UIScreen.main.bounds.width/428)) + .onAppear { + loadImage() + } + } // Price + quantity HStack(spacing: 4) { @@ -553,6 +593,18 @@ struct CartItemCell: View { .frame(width: 140 * (UIScreen.main.bounds.width/428), height: 140 * (UIScreen.main.bounds.width/428)) .cornerRadius(12) } + + private func loadImage() { + guard let url = URL(string: item.imageURL) else { return } + + URLSession.shared.dataTask(with: url) { data, response, error in + if let data = data, let loadedImage = UIImage(data: data) { + DispatchQueue.main.async { + self.image = loadedImage + } + } + }.resume() + } } func addItemToCart(view: Int, showError: Binding, errorMessage: Binding<[String]>, itemId: String, completion: @escaping (String) -> Void) {