diff --git a/HIAPI/Models/Profile.swift b/HIAPI/Models/Profile.swift index a7f81f57..c6d05573 100644 --- a/HIAPI/Models/Profile.swift +++ b/HIAPI/Models/Profile.swift @@ -32,6 +32,7 @@ public struct Profile: Codable, APIReturnable { case discordTag case avatarUrl case coins + case ranking } public let userId: String @@ -41,6 +42,11 @@ public struct Profile: Codable, APIReturnable { public let discordTag: String public let avatarUrl: String public let coins: Int + public let ranking: Int? +} + +public struct Ranking: Codable, APIReturnable { + public let ranking: Int } public struct ProfileFavorites: Codable, APIReturnable { diff --git a/HIAPI/Services/ProfileService.swift b/HIAPI/Services/ProfileService.swift index 9dd9f834..042abf00 100644 --- a/HIAPI/Services/ProfileService.swift +++ b/HIAPI/Services/ProfileService.swift @@ -39,6 +39,12 @@ public final class ProfileService: BaseService { public static func updateUserProfile(profileData: [String: Any]) -> APIRequest { return APIRequest(service: self, endpoint: "", body: profileData, headers: headers, method: .PUT) } + + public static func getUserRanking(userToken: String) -> APIRequest { + var authorizationHeaders = HTTPHeaders() + authorizationHeaders["Authorization"] = userToken + return APIRequest(service: self, endpoint: "ranking/", headers: authorizationHeaders, method: .GET) + } public static func getAllFavorites() -> APIRequest { return APIRequest(service: self, endpoint: "favorite/", method: .GET) diff --git a/HackIllinois/ViewControllers/HIProfileCardView.swift b/HackIllinois/ViewControllers/HIProfileCardView.swift index 7b0d1c25..a298ed70 100644 --- a/HackIllinois/ViewControllers/HIProfileCardView.swift +++ b/HackIllinois/ViewControllers/HIProfileCardView.swift @@ -15,6 +15,23 @@ import URLImage import HIAPI // Loads url data and converts into image +//extension String { +// func loadImage(completion: @escaping (UIImage?) -> Void) { +// guard let url = URL(string: self) else { +// completion(nil) +// return +// } +// +// URLSession.shared.dataTask(with: url) { data, _, error in +// if let data = data, let image = UIImage(data: data) { +// completion(image) +// } else { +// completion(nil) +// } +// }.resume() +// } +//} + extension String { func load() -> UIImage { do { @@ -29,6 +46,7 @@ extension String { } struct HIProfileCardView: View { + @State private var rank: Int = 0 let displayName: String let points: Int let tier: String @@ -60,7 +78,15 @@ struct HIProfileCardView: View { .font(Font(HIAppearance.Font.profileSubtitle ?? .systemFont(ofSize: 20))) HStack(alignment: .bottom, spacing: 5) { Image("RankSymbol") - Text("25").foregroundColor(.white).font(Font(HIAppearance.Font.profileSubtitle ?? .systemFont(ofSize: 20))) + Text("Rank: \(rank)") + .foregroundColor(.white) + .font(Font(HIAppearance.Font.profileSubtitle ?? .systemFont(ofSize: 20))) + .onAppear { + // Call getRank and update the rank when it's available + getRank { retrievedRank in + self.rank = retrievedRank + } + } } }.padding(.bottom, isIpad ? 40 : 25) }.alignmentGuide(.bottom) {dimensions in dimensions[.bottom] / 1.2 } @@ -168,8 +194,6 @@ struct HIProfileCardView: View { let (qr, _) = try result.get() DispatchQueue.main.async { self.qrInfo = qr.qrInfo - print("qrInfo is not empty:") - print(qrInfo) } } catch { print("An error has occurred \(error)") @@ -178,6 +202,28 @@ struct HIProfileCardView: View { .authorize(with: user) .launch() } + + func getRank(completion: @escaping (Int) -> Void) { + guard let user = HIApplicationStateController.shared.user else { + completion(0) + return + } + + var rank = 0 + + HIAPI.ProfileService.getUserRanking(userToken: user.token) + .onCompletion { result in + do { + let (userRanking, _) = try result.get() + rank = userRanking.ranking + completion(rank) + } catch { + print("An error has occurred in ranking \(error)") + completion(0) + } + } + } + } struct HIProfileCardView_Previews: PreviewProvider { @@ -186,8 +232,7 @@ struct HIProfileCardView_Previews: PreviewProvider { points: 100, tier: "Pro", foodWave: 1, - avatarUrl: "https://raw.githubusercontent.com/HackIllinois/adonix-metadata/main/avatars/fishercat.png", - userId: "https://www.hackillinois.org" + avatarUrl: "https://raw.githubusercontent.com/HackIllinois/adonix-metadata/main/avatars/fishercat.png", userId: "https://www.hackillinois.org" ) } } diff --git a/HackIllinois/ViewControllers/HIProfileViewController.swift b/HackIllinois/ViewControllers/HIProfileViewController.swift index ba71049f..ed00d848 100644 --- a/HackIllinois/ViewControllers/HIProfileViewController.swift +++ b/HackIllinois/ViewControllers/HIProfileViewController.swift @@ -39,6 +39,7 @@ class HIProfileViewController: HIBaseViewController { backgroundView.image = #imageLiteral(resourceName: "PurpleBackground") } private var tiers: [Tier] = [] + private var ranking: Int = 0 } // MARK: - UITabBarItem Setup @@ -73,6 +74,20 @@ extension HIProfileViewController { view.willRemoveSubview(profileCardController!.view) profileCardController?.removeFromParent() } + var rank: Int = 0 +// guard let user = HIApplicationStateController.shared.user else { return } +// HIAPI.ProfileService.getUserRanking(userToken: user.token) +// .onCompletion { [weak self] result in +// do { +// let (userRanking, _) = try result.get() +// self?.ranking = userRanking.ranking +// rank = userRanking.ranking +// } catch { +// print("An error has occurred in ranking \(error)") +// } +// } +// .launch() +// print("rank \(rank)") profileCardController = UIHostingController(rootView: HIProfileCardView(displayName: profile.displayName, points: profile.points, tier: profileTier, @@ -158,11 +173,11 @@ extension HIProfileViewController { .onCompletion { [weak self] result in do { let (apiProfile, _) = try result.get() + print(apiProfile) self?.profile.userId = apiProfile.userId self?.profile.displayName = apiProfile.discordTag self?.profile.points = apiProfile.points self?.profile.avatarUrl = apiProfile.avatarUrl - print(apiProfile.avatarUrl) //self?.profile.foodWave = apiProfile.foodWave DispatchQueue.main.async { NotificationCenter.default.post(name: .loginProfile, object: nil, userInfo: ["profile": self?.profile]) @@ -187,6 +202,20 @@ extension HIProfileViewController { } } .launch() + HIAPI.ProfileService.getUserRanking(userToken: user.token) + .onCompletion { [weak self] result in + do { + let (userRanking, _) = try result.get() + self?.ranking = userRanking.ranking + print(userRanking.ranking) + DispatchQueue.main.async { + self?.updateProfile() + } + } catch { + print("An error has occurred in ranking \(error)") + } + } + .launch() HIAPI.RegistrationService.getAttendee() .onCompletion { [weak self] result in