diff --git a/Sources/AllInApp/AllIn/Components/AllcoinsCounter.swift b/Sources/AllInApp/AllIn/Components/AllcoinsCounter.swift index 6fd256f..7cce46f 100644 --- a/Sources/AllInApp/AllIn/Components/AllcoinsCounter.swift +++ b/Sources/AllInApp/AllIn/Components/AllcoinsCounter.swift @@ -15,7 +15,7 @@ struct AllcoinsCounter: View { var body: some View { HStack(alignment: .center) { - Text(String(appStateContainer.user?.nbCoins ?? 0)) + Text(String(appStateContainer.user!.nbCoins)) .contentTransition(.numericText()) .fontWeight(.black) .foregroundColor(foregroundColor) diff --git a/Sources/AllInApp/AllIn/Components/BetCard.swift b/Sources/AllInApp/AllIn/Components/BetCard.swift index c261ed5..4db923c 100644 --- a/Sources/AllInApp/AllIn/Components/BetCard.swift +++ b/Sources/AllInApp/AllIn/Components/BetCard.swift @@ -22,7 +22,6 @@ struct BetCard: View { Text("bet_proposed_by_format \(bet.author.capitalized)") .font(.system(size: 10)) .foregroundColor(AllInColors.grey800Color) - } Text(bet.theme) .font(.system(size: 15)) diff --git a/Sources/AllInApp/AllIn/Components/EmptyInfo.swift b/Sources/AllInApp/AllIn/Components/EmptyInfo.swift index c4ae925..bcccc50 100644 --- a/Sources/AllInApp/AllIn/Components/EmptyInfo.swift +++ b/Sources/AllInApp/AllIn/Components/EmptyInfo.swift @@ -20,6 +20,8 @@ struct EmptyInfo: View { } } -#Preview { - EmptyInfo(emoji:"👥", title: "Vous n’avez pas encore d’amis", explain: "Ajoutez les depuis cet écran") +struct EmptyInfo_Previews: PreviewProvider { + static var previews: some View { + EmptyInfo(emoji:"👥", title: "Vous n’avez pas encore d’amis", explain: "Ajoutez les depuis cet écran") + } } diff --git a/Sources/AllInApp/AllIn/Components/Friend.swift b/Sources/AllInApp/AllIn/Components/Friend.swift index f99d545..56ddec6 100644 --- a/Sources/AllInApp/AllIn/Components/Friend.swift +++ b/Sources/AllInApp/AllIn/Components/Friend.swift @@ -29,7 +29,7 @@ struct Friend: View { var body: some View { HStack{ - AsyncImage(url: URL(string: "https://picsum.photos/536/354")) + AsyncImage(url: URL(string: user.image ?? "")) .frame(width: 50, height: 50) .cornerRadius(180) .scaledToFit() diff --git a/Sources/AllInApp/AllIn/Components/Menu.swift b/Sources/AllInApp/AllIn/Components/Menu.swift index f6ddada..a3ad403 100644 --- a/Sources/AllInApp/AllIn/Components/Menu.swift +++ b/Sources/AllInApp/AllIn/Components/Menu.swift @@ -27,8 +27,7 @@ struct Menu: View { HStack() { Spacer() VStack(){ - Image("defaultUserImage") - .resizable() + AsyncImage(url: URL(string: AppStateContainer.shared.user?.image ?? "")) .scaledToFit() .frame(width: 100, height: 100) .cornerRadius(180) @@ -44,7 +43,7 @@ struct Menu: View { HStack(spacing: 30) { Spacer() VStack(){ - Text("114") + Text(AppStateContainer.shared.user?.bestWin.description ?? "0") .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) @@ -54,7 +53,7 @@ struct Menu: View { .foregroundColor(AllInColors.grey600Color) } VStack(){ - Text("343") + Text(AppStateContainer.shared.user?.bestWin.description ?? "0") .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) @@ -64,7 +63,7 @@ struct Menu: View { .foregroundColor(AllInColors.grey600Color) } VStack(){ - Text("5") + Text(AppStateContainer.shared.user?.nbFriends.description ?? "0") .fontWeight(.heavy) .font(.system(size: 15)) .foregroundColor(.white) diff --git a/Sources/AllInApp/AllIn/Components/ParticipateButton.swift b/Sources/AllInApp/AllIn/Components/ParticipateButton.swift index d0ad4e9..15bb890 100644 --- a/Sources/AllInApp/AllIn/Components/ParticipateButton.swift +++ b/Sources/AllInApp/AllIn/Components/ParticipateButton.swift @@ -15,6 +15,8 @@ struct ParticipateButton: View { var bet: Bet? var isDisabled: Bool { + guard bet?.author != AppStateContainer.shared.user?.username else { return true } + if let betType = bet?.status { switch betType { case .inProgress: diff --git a/Sources/AllInApp/AllIn/Components/ParticipationCell.swift b/Sources/AllInApp/AllIn/Components/ParticipationCell.swift index 60902b1..f902425 100644 --- a/Sources/AllInApp/AllIn/Components/ParticipationCell.swift +++ b/Sources/AllInApp/AllIn/Components/ParticipationCell.swift @@ -9,7 +9,7 @@ import SwiftUI import Model struct ParticipationCell: View { - @State var participation: Participation + var participation: Participation var body: some View { HStack(alignment: .center, spacing: 0){ Circle() diff --git a/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift b/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift index de512af..dd8c328 100644 --- a/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift +++ b/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift @@ -12,6 +12,7 @@ struct TrendingBetCard: View { var bet: Bet @State var showDetails: Bool = false + @State var showParticipate: Bool = false var body: some View { VStack(alignment: .leading) { @@ -61,6 +62,12 @@ struct TrendingBetCard: View { .stroke(AllInColors.primaryGradient, lineWidth: 5) ) .clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous)) + .onTapGesture { + showDetails.toggle() + } + .fullScreenCover(isPresented: $showDetails) { + DetailsView(isModalPresented: $showDetails, isModalParticipated: $showParticipate, id: bet.id) + } } } diff --git a/Sources/AllInApp/AllIn/Services/AuthService.swift b/Sources/AllInApp/AllIn/Services/AuthService.swift index 1ba7a2b..75411cb 100644 --- a/Sources/AllInApp/AllIn/Services/AuthService.swift +++ b/Sources/AllInApp/AllIn/Services/AuthService.swift @@ -148,7 +148,7 @@ class AuthService: IAuthService { } private func initManagerVM(token: String) { - DependencyInjection.shared.addSingleton(Manager.self, Manager(withBetDataManager: BetApiManager(withUserToken: token), withUserDataManager: UserApiManager(withUserToken: token))) + DependencyInjection.shared.addSingleton(Manager.self, Manager(withBetDataManager: BetApiManager(withUserToken: token, andApiUrl: Config.allInApi), withUserDataManager: UserApiManager(withUserToken: token, andApiUrl: Config.allInApi))) } public func logout() { diff --git a/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift index 69e2bb1..89ce7d6 100644 --- a/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift @@ -21,7 +21,9 @@ class CurrentBetViewModel: ObservableObject { func getItems() { manager.getCurrentBets(withIndex: 0, withCount: 20) { bets in - self.bets = bets + DispatchQueue.main.async { + self.bets = bets + } } } diff --git a/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift index 543196b..ec1472b 100644 --- a/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/DetailsViewModel.swift @@ -27,7 +27,12 @@ class DetailsViewModel: ObservableObject { func getItem(withId id: String) { manager.getBet(withId: id) { bet in - self.betDetail = bet + DispatchQueue.main.async { + for par in bet.participations { + print(par.id) + } + self.betDetail = bet + } } } diff --git a/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift index 5ad222b..f14b882 100644 --- a/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift @@ -6,7 +6,6 @@ // import Foundation -import SwiftUI import DependencyInjection import Model diff --git a/Sources/AllInApp/AllIn/ViewModels/RankingViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/RankingViewModel.swift index 7bd1879..15eda4d 100644 --- a/Sources/AllInApp/AllIn/ViewModels/RankingViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/RankingViewModel.swift @@ -23,7 +23,9 @@ class RankingViewModel: ObservableObject { manager.getFriends() { users in var friends = users friends.append(AppStateContainer.shared.user!) - self.friends = friends.sorted(by: { $0.nbCoins > $1.nbCoins }) + DispatchQueue.main.async { + self.friends = friends.sorted(by: { $0.nbCoins > $1.nbCoins }) + } } } } diff --git a/Sources/AllInApp/AllIn/ViewModels/RegisterViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/RegisterViewModel.swift index dcdc885..f7948bd 100644 --- a/Sources/AllInApp/AllIn/ViewModels/RegisterViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/RegisterViewModel.swift @@ -102,6 +102,7 @@ class RegisterViewModel: ObservableObject { registerPasswordFieldError = newRegisterPasswordFieldError registerConfirmPasswordFieldError = newRegisterConfirmPasswordFieldError } + return false } diff --git a/Sources/AllInApp/AllIn/Views/DetailsView.swift b/Sources/AllInApp/AllIn/Views/DetailsView.swift index f32cbdd..aabc20f 100644 --- a/Sources/AllInApp/AllIn/Views/DetailsView.swift +++ b/Sources/AllInApp/AllIn/Views/DetailsView.swift @@ -5,7 +5,6 @@ struct DetailsView: View { @Binding var isModalPresented: Bool @Binding var isModalParticipated: Bool - @State var progressValue: Float = 0.2 @StateObject private var viewModel: DetailsViewModel var isFinished: Bool { @@ -122,8 +121,9 @@ struct DetailsView: View { .fontWeight(.bold) .padding(.bottom, 10) ScrollView(showsIndicators: false) { - ForEach(viewModel.betDetail?.participations ?? []) { participation in - ParticipationCell(participation: participation).padding(.horizontal, 10) + ForEach(viewModel.betDetail?.participations ?? [], id: \.self) { participation in + ParticipationCell(participation: participation) + .padding(.horizontal, 10) } } .padding(.bottom, geometry.safeAreaInsets.bottom + 28) diff --git a/Sources/AllInApp/AllIn/Views/FriendsView.swift b/Sources/AllInApp/AllIn/Views/FriendsView.swift index a32a211..e1240d3 100644 --- a/Sources/AllInApp/AllIn/Views/FriendsView.swift +++ b/Sources/AllInApp/AllIn/Views/FriendsView.swift @@ -50,7 +50,6 @@ struct FriendsView: View { EmptyInfo(emoji:"👥", title: String(localized: "empty_friends_title"), explain: String(localized: "empty_friends_explain")).padding(.top, 40) } else{ - ForEach(viewModel.users, id: \.self) { friend in Friend(user: friend, isRequest: false, viewModel: viewModel) } diff --git a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj index 5cc5531..f9b6afe 100644 --- a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj +++ b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ EC30770B2B24D9160060E34D /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770A2B24D9160060E34D /* WelcomeView.swift */; }; EC30770D2B24DB7A0060E34D /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770C2B24DB7A0060E34D /* Extensions.swift */; }; EC30770F2B24FCB00060E34D /* RegisterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770E2B24FCB00060E34D /* RegisterView.swift */; }; + EC34FDCE2C0DB46C0020A371 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC34FDCD2C0DB46C0020A371 /* Config.swift */; }; EC4F0D2C2B4EBF5600853949 /* Model in Frameworks */ = {isa = PBXBuildFile; productRef = ECB357342B3E13A400045D41 /* Model */; }; EC60C5682B5A83FB00FFD6EF /* StubLib in Frameworks */ = {isa = PBXBuildFile; productRef = EC60C5672B5A83FB00FFD6EF /* StubLib */; }; EC650A422B25C817003AFCAD /* Friend.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC650A412B25C817003AFCAD /* Friend.swift */; }; @@ -56,7 +57,6 @@ EC6B96B72B24B4CC00FC1C58 /* AllInUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96B62B24B4CC00FC1C58 /* AllInUITests.swift */; }; EC6B96B92B24B4CC00FC1C58 /* AllInUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96B82B24B4CC00FC1C58 /* AllInUITestsLaunchTests.swift */; }; EC6B96CC2B24B7E500FC1C58 /* IAuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96CB2B24B7E500FC1C58 /* IAuthService.swift */; }; - EC6B96CF2B24B8D900FC1C58 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96CE2B24B8D900FC1C58 /* Config.swift */; }; EC6B96D12B24BAE800FC1C58 /* AuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96D02B24BAE800FC1C58 /* AuthService.swift */; }; EC6B96D52B24BE0E00FC1C58 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96D42B24BE0E00FC1C58 /* MainView.swift */; }; EC6B96D82B24BF2100FC1C58 /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC6B96D72B24BF2100FC1C58 /* Menu.swift */; }; @@ -172,6 +172,7 @@ EC30770A2B24D9160060E34D /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = ""; }; EC30770C2B24DB7A0060E34D /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; EC30770E2B24FCB00060E34D /* RegisterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterView.swift; sourceTree = ""; }; + EC34FDCD2C0DB46C0020A371 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = ""; }; EC650A412B25C817003AFCAD /* Friend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Friend.swift; sourceTree = ""; }; EC650A432B25CDF3003AFCAD /* ParameterMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParameterMenu.swift; sourceTree = ""; }; EC650A452B25D686003AFCAD /* RankingRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RankingRow.swift; sourceTree = ""; }; @@ -196,7 +197,6 @@ EC6B96B62B24B4CC00FC1C58 /* AllInUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllInUITests.swift; sourceTree = ""; }; EC6B96B82B24B4CC00FC1C58 /* AllInUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllInUITestsLaunchTests.swift; sourceTree = ""; }; EC6B96CB2B24B7E500FC1C58 /* IAuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAuthService.swift; sourceTree = ""; }; - EC6B96CE2B24B8D900FC1C58 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = ""; }; EC6B96D02B24BAE800FC1C58 /* AuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthService.swift; sourceTree = ""; }; EC6B96D42B24BE0E00FC1C58 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; EC6B96D72B24BF2100FC1C58 /* Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = ""; }; @@ -365,9 +365,9 @@ EC6B96CD2B24B8A300FC1C58 /* Ressources */ = { isa = PBXGroup; children = ( - EC6B96CE2B24B8D900FC1C58 /* Config.swift */, EC3077082B24CF7F0060E34D /* Colors.swift */, ECDDD48C2BF4A7920009223A /* Localizable.strings */, + EC34FDCD2C0DB46C0020A371 /* Config.swift */, ); path = Ressources; sourceTree = ""; @@ -692,9 +692,9 @@ EC7A882B2B28D1E0004F226A /* DropDownMenu.swift in Sources */, EC7A882D2B28D8A1004F226A /* CreationBetView.swift in Sources */, ECED90B52B6D9CEC00F50937 /* DailyGiftPage.swift in Sources */, - EC6B96CF2B24B8D900FC1C58 /* Config.swift in Sources */, 1244EF602B4EC31E00374ABF /* HistoricBetView.swift in Sources */, EC30770B2B24D9160060E34D /* WelcomeView.swift in Sources */, + EC34FDCE2C0DB46C0020A371 /* Config.swift in Sources */, EC30770D2B24DB7A0060E34D /* Extensions.swift in Sources */, EC6B96D82B24BF2100FC1C58 /* Menu.swift in Sources */, EC650A4E2B278EDB003AFCAD /* TrendingBetCard.swift in Sources */, diff --git a/Sources/Api/Sources/Api/BetApiManager.swift b/Sources/Api/Sources/Api/BetApiManager.swift index e764231..ff6ae9f 100644 --- a/Sources/Api/Sources/Api/BetApiManager.swift +++ b/Sources/Api/Sources/Api/BetApiManager.swift @@ -11,13 +11,15 @@ import Model public struct BetApiManager: BetDataManager { public let token: String + public let url: String - public init(withUserToken token: String) { + public init(withUserToken token: String, andApiUrl url: String) { self.token = token + self.url = url } public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter] = [], completion: @escaping ([Bet]) -> Void) { - let url = URL(string: allInApi + "bets/gets")! + let url = URL(string: url + "bets/gets")! var request = URLRequest(url: url) request.httpMethod = "POST" @@ -62,7 +64,7 @@ public struct BetApiManager: BetDataManager { } public func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) { - let url = URL(string: allInApi + "betdetail/get/" + id)! + let url = URL(string: url + "betdetail/get/" + id)! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -74,11 +76,10 @@ public struct BetApiManager: BetDataManager { print ("ALLIN : get bet with id :" + id) do { if let httpResponse = response as? HTTPURLResponse, let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { - + print(httpResponse.statusCode) if let betDetail = FactoryApiBet().toBetDetail(from: json) { completion(betDetail) } - print(httpResponse.statusCode) } } catch { print("Error parsing JSON: \(error)") @@ -88,7 +89,7 @@ public struct BetApiManager: BetDataManager { } public func getPopularBet(completion: @escaping (Bet) -> Void) { - let url = URL(string: allInApi + "bets/popular")! + let url = URL(string: url + "bets/popular")! var request = URLRequest(url: url) request.httpMethod = "GET" diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index df39982..488db88 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -8,14 +8,14 @@ import Foundation import Model -let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api/" - public struct UserApiManager: UserDataManager { public let token: String + public let url: String - public init(withUserToken token: String) { + public init(withUserToken token: String, andApiUrl url: String) { self.token = token + self.url = url } public func getBets(withIndex index: Int, withCount count: Int) -> [Bet] { @@ -23,7 +23,7 @@ public struct UserApiManager: UserDataManager { } public func getBetsOver(completion : @escaping ([BetDetail])-> ()) { - let url = URL(string: allInApi + "bets/toConfirm")! + let url = URL(string: url + "bets/toConfirm")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -54,7 +54,7 @@ public struct UserApiManager: UserDataManager { public func addBet(bet: Bet, completion : @escaping (Int)-> ()) { - let url = URL(string: allInApi + "bets/add")! + let url = URL(string: url + "bets/add")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") @@ -75,7 +75,7 @@ public struct UserApiManager: UserDataManager { public func addFriend(username: String, completion : @escaping (Int)-> ()) { - let url = URL(string: allInApi + "friends/add")! + let url = URL(string: url + "friends/add")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") @@ -96,7 +96,7 @@ public struct UserApiManager: UserDataManager { public func removeFriend(username: String, completion : @escaping (Int)-> ()) { - let url = URL(string: allInApi + "friends/delete")! + let url = URL(string: url + "friends/delete")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") @@ -116,7 +116,7 @@ public struct UserApiManager: UserDataManager { } public func getFriends(completion: @escaping ([User]) -> Void) { - let url = URL(string: allInApi + "friends/gets")! + let url = URL(string: url + "friends/gets")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -130,7 +130,6 @@ public struct UserApiManager: UserDataManager { print ("ALLIN : get friends") do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { - print(jsonArray) users = try JSONDecoder().decode([User].self, from: JSONSerialization.data(withJSONObject: jsonArray)) print(httpResponse.statusCode) completion(users) @@ -143,7 +142,7 @@ public struct UserApiManager: UserDataManager { } public func getRequests(completion: @escaping ([User]) -> Void) { - let url = URL(string: allInApi + "friends/requests")! + let url = URL(string: url + "friends/requests")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -157,7 +156,6 @@ public struct UserApiManager: UserDataManager { print ("ALLIN : get friends") do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { - print(jsonArray) users = try JSONDecoder().decode([User].self, from: JSONSerialization.data(withJSONObject: jsonArray)) print(httpResponse.statusCode) completion(users) @@ -170,7 +168,7 @@ public struct UserApiManager: UserDataManager { } public func getUsers(withName name: String, completion: @escaping ([User]) -> Void) { - let url = URL(string: allInApi + "friends/search/" + name)! + let url = URL(string: url + "friends/search/" + name)! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -184,7 +182,6 @@ public struct UserApiManager: UserDataManager { print ("ALLIN : get friends by search") do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { - print(jsonArray) users = try JSONDecoder().decode([User].self, from: JSONSerialization.data(withJSONObject: jsonArray)) print(httpResponse.statusCode) completion(users) @@ -197,7 +194,7 @@ public struct UserApiManager: UserDataManager { } public func getGifts(completion : @escaping (Int, Int)-> ()) { - let url = URL(string: allInApi + "users/gift")! + let url = URL(string: url + "users/gift")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -222,7 +219,7 @@ public struct UserApiManager: UserDataManager { } public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { - let url = URL(string: allInApi + "bets/history")! + let url = URL(string: url + "bets/history")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -237,7 +234,6 @@ public struct UserApiManager: UserDataManager { do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { for json in jsonArray { - print(json) if let bet = FactoryApiBet().toBetDetail(from: json) { bets.append(bet) } @@ -253,7 +249,7 @@ public struct UserApiManager: UserDataManager { } public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { - let url = URL(string: allInApi + "bets/current")! + let url = URL(string: url + "bets/current")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -268,7 +264,6 @@ public struct UserApiManager: UserDataManager { do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { for json in jsonArray { - print(json) if let bet = FactoryApiBet().toBetDetail(from: json) { bets.append(bet) } @@ -284,7 +279,7 @@ public struct UserApiManager: UserDataManager { } public func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ()) { - let url = URL(string: allInApi + "participations/add")! + let url = URL(string: url + "participations/add")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") @@ -308,7 +303,7 @@ public struct UserApiManager: UserDataManager { } public func addResponse(withIdBet id: String, andResponse responseBet: String) { - let url = URL(string: allInApi + "bets/confirm/" + id)! + let url = URL(string: url + "bets/confirm/" + id)! var request = URLRequest(url: url) request.httpMethod = "POST" diff --git a/Sources/Model/Sources/Model/Participation.swift b/Sources/Model/Sources/Model/Participation.swift index 60d65e4..4d5e735 100644 --- a/Sources/Model/Sources/Model/Participation.swift +++ b/Sources/Model/Sources/Model/Participation.swift @@ -8,9 +8,7 @@ import Foundation /// A class representing a user's participation in a bet. -public class Participation: ObservableObject, Codable, Identifiable { - - public let id: String +public class Participation: ObservableObject, Codable, Identifiable, Hashable { /// The amount of stake in the bet. public private(set) var stake: Int @@ -28,15 +26,29 @@ public class Participation: ObservableObject, Codable, Identifiable { /// /// - Parameters: /// - stake: The amount of stake in the bet. - /// - date: The date and time when the participation occurred. - /// - response: The response or outcome of the participation. - /// - user: The user who participated in the bet. + /// - answer: The response or outcome of the participation. + /// - username: The user who participated in the bet. /// - betId: The unique identifier of the bet. - public init(id: String, stake: Int, answer: String, username: String, betId: String) { - self.id = id + public init(stake: Int, answer: String, username: String, betId: String) { self.stake = stake self.answer = answer self.username = username self.betId = betId } + + // Conformance to Equatable + public static func == (lhs: Participation, rhs: Participation) -> Bool { + return lhs.stake == rhs.stake && + lhs.answer == rhs.answer && + lhs.username == rhs.username && + lhs.betId == rhs.betId + } + + // Conformance to Hashable + public func hash(into hasher: inout Hasher) { + hasher.combine(stake) + hasher.combine(answer) + hasher.combine(username) + hasher.combine(betId) + } } diff --git a/Sources/Model/Sources/Model/User.swift b/Sources/Model/Sources/Model/User.swift index 6dd0b5d..a07565e 100644 --- a/Sources/Model/Sources/Model/User.swift +++ b/Sources/Model/Sources/Model/User.swift @@ -22,17 +22,17 @@ public struct User: Codable, Hashable { /// The friendship status with the main user. public var friendStatus: FriendStatus? - /// Custom constructor to initialize a User instance. - /// - /// - Parameters: - /// - username: The username of the user. - /// - email: The email address of the user. - /// - nbCoins: The number of coins associated with the user. - public init(username: String, email: String, nbCoins: Int) { - self.username = username - self.email = email - self.nbCoins = nbCoins - } + /// The user's profile image. + public var image: String? + + /// The number of bets made by the user. + public var nbBets: Int + + /// The number of friends the user has. + public var nbFriends: Int + + /// The user's best win. + public var bestWin: Int /// Custom constructor to initialize a User instance. /// @@ -40,11 +40,19 @@ public struct User: Codable, Hashable { /// - username: The username of the user. /// - email: The email address of the user. /// - nbCoins: The number of coins associated with the user. - /// - status: The friendship status with the main user. - public init(username: String, email: String, nbCoins: Int, status: FriendStatus) { + /// - friendStatus: The friendship status with the main user. + /// - image: The user's profile image. + /// - nbBets: The number of bets made by the user. + /// - nbFriends: The number of friends the user has. + /// - bestWin: The user's best win. + public init(username: String, email: String, nbCoins: Int, friendStatus: FriendStatus? = nil, image: String? = nil, nbBets: Int, nbFriends: Int, bestWin: Int) { self.username = username self.email = email self.nbCoins = nbCoins - self.friendStatus = status + self.friendStatus = friendStatus + self.image = image + self.nbBets = nbBets + self.nbFriends = nbFriends + self.bestWin = bestWin } } diff --git a/Sources/StubLib/Sources/StubLib/Stub.swift b/Sources/StubLib/Sources/StubLib/Stub.swift index a6e15cd..097ad0d 100644 --- a/Sources/StubLib/Sources/StubLib/Stub.swift +++ b/Sources/StubLib/Sources/StubLib/Stub.swift @@ -21,13 +21,13 @@ struct Stub { public mutating func loadBets() { - let user1 = User(username: "Lucas", email: "lucas.delanier@etu.uca.fr", nbCoins: 100) + let user1 = User(username: "Lucas", email: "lucas.delanier@etu.uca.fr", nbCoins: 100, nbBets: 0, nbFriends: 0, bestWin: 0) users.append(user1) - let user2 = User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75) + let user2 = User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75, nbBets: 0, nbFriends: 0, bestWin: 0) users.append(user2) - let user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30) + let user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, nbBets: 0, nbFriends: 0, bestWin: 0) users.append(user3) let bet1 = BinaryBet( @@ -87,10 +87,10 @@ struct Stub { self.betsDetail.append(betDetail) } - self.betsDetail[0].addParticipation(newParticipation: Participation(id: UUID().uuidString, stake: 120, answer: "OUI", username: user1.username, betId: "1")) - self.betsDetail[0].addParticipation(newParticipation: Participation(id: UUID().uuidString, stake: 20, answer: "NON", username: user2.username, betId: "2")) - self.betsDetail[0].addParticipation(newParticipation: Participation(id: UUID().uuidString, stake: 320, answer: "OUI", username: user3.username, betId: "3")) - self.betsDetail[0].addParticipation(newParticipation: Participation(id: UUID().uuidString, stake: 320, answer: "OUI", username: user3.username, betId: "3")) + self.betsDetail[0].addParticipation(newParticipation: Participation(stake: 120, answer: "OUI", username: user1.username, betId: "1")) + self.betsDetail[0].addParticipation(newParticipation: Participation(stake: 20, answer: "NON", username: user2.username, betId: "2")) + self.betsDetail[0].addParticipation(newParticipation: Participation(stake: 320, answer: "OUI", username: user3.username, betId: "3")) + self.betsDetail[0].addParticipation(newParticipation: Participation(stake: 320, answer: "OUI", username: user3.username, betId: "3")) }