Skip to content

Commit

Permalink
Ranking updated
Browse files Browse the repository at this point in the history
  • Loading branch information
anushkasankaran committed Feb 7, 2024
1 parent a3b49e6 commit 4e64b0d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
6 changes: 6 additions & 0 deletions HIAPI/Models/Profile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public struct Profile: Codable, APIReturnable {
case discordTag
case avatarUrl
case coins
case ranking
}

public let userId: String
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions HIAPI/Services/ProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public final class ProfileService: BaseService {
public static func updateUserProfile(profileData: [String: Any]) -> APIRequest<Profile> {
return APIRequest<Profile>(service: self, endpoint: "", body: profileData, headers: headers, method: .PUT)
}

public static func getUserRanking(userToken: String) -> APIRequest<Ranking> {
var authorizationHeaders = HTTPHeaders()
authorizationHeaders["Authorization"] = userToken
return APIRequest<Ranking>(service: self, endpoint: "ranking/", headers: authorizationHeaders, method: .GET)
}

public static func getAllFavorites() -> APIRequest<ProfileFavorites> {
return APIRequest<ProfileFavorites>(service: self, endpoint: "favorite/", method: .GET)
Expand Down
55 changes: 50 additions & 5 deletions HackIllinois/ViewControllers/HIProfileCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,6 +46,7 @@ extension String {
}

struct HIProfileCardView: View {
@State private var rank: Int = 0
let displayName: String
let points: Int
let tier: String
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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)")
Expand All @@ -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 {
Expand All @@ -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"
)
}
}
31 changes: 30 additions & 1 deletion HackIllinois/ViewControllers/HIProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class HIProfileViewController: HIBaseViewController {
backgroundView.image = #imageLiteral(resourceName: "PurpleBackground")
}
private var tiers: [Tier] = []
private var ranking: Int = 0
}

// MARK: - UITabBarItem Setup
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down

0 comments on commit 4e64b0d

Please sign in to comment.