Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anushka/ps minor #630

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 67 additions & 15 deletions HackIllinois/ViewControllers/HIPointsShopSwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)")
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -477,13 +495,26 @@ 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 {
let count: Int
let item: Item
@Binding var showError: Bool
@Binding var errorMessage: [String]
@State private var image: UIImage? = nil

var body: some View {
ZStack {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Bool>, errorMessage: Binding<[String]>, itemId: String, completion: @escaping (String) -> Void) {
Expand Down