diff --git a/Sources/AllInApp/AllIn/Components/BetCard.swift b/Sources/AllInApp/AllIn/Components/BetCard.swift index 2b7f637..2fb87a9 100644 --- a/Sources/AllInApp/AllIn/Components/BetCard.swift +++ b/Sources/AllInApp/AllIn/Components/BetCard.swift @@ -19,7 +19,7 @@ struct BetCard: View { VStack(alignment: .leading,spacing: 2){ HStack{ Spacer() - Text("bet_proposed_by_format \(bet.author.capitalized)") + Text("bet_proposed_by_format \(bet.author.capitalized)") .font(.system(size: 10)) .foregroundColor(AllInColors.grey800Color) diff --git a/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift b/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift index f540b1a..da3c1c9 100644 --- a/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift +++ b/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift @@ -6,22 +6,39 @@ // import SwiftUI +import Model struct ChoiceCapsule: View { - + let filter: BetFilter @State var pressed = false + @ObservedObject var viewModel: BetViewModel + + var label: String { + switch filter { + case .isPublic: + return String(localized: "bet_public") + case .isInvitation: + return String(localized: "bet_invitation") + case .inProgress: + return String(localized: "bet_current") + case .isFinished: + return String(localized: "bet_finished") + default: + return "NaN" + } + } var body: some View { Group { if(pressed) { - Text("bet_current") + Text(label) .textStyle(weight: .semibold, color: .white, size: 15) .padding([.leading,.trailing],13.8) .padding([.top,.bottom], 7) .background(AllInColors.lightPurpleColor) .clipShape(Capsule()) } else { - Text("bet_current") + Text(label) .textStyle(weight: .regular, color: AllInColors.grey800Color, size: 15) .padding([.leading,.trailing], 15) .padding([.top,.bottom], 7) @@ -34,14 +51,13 @@ struct ChoiceCapsule: View { } } .onTapGesture() { + if(!pressed) { + viewModel.filters.insert(filter) + } else { + viewModel.filters.remove(filter) + } pressed.toggle() } } } - -struct ChoiceCapsule_Previews: PreviewProvider { - static var previews: some View { - ChoiceCapsule() - } -} diff --git a/Sources/AllInApp/AllIn/Ressources/Config.swift b/Sources/AllInApp/AllIn/Ressources/Config.swift index d734b64..ab35db0 100644 --- a/Sources/AllInApp/AllIn/Ressources/Config.swift +++ b/Sources/AllInApp/AllIn/Ressources/Config.swift @@ -8,5 +8,5 @@ import Foundation struct Config { - static let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api/" + static let allInApi = "http://localhost:8080/" } diff --git a/Sources/AllInApp/AllIn/Ressources/en.lproj/Localizable.strings b/Sources/AllInApp/AllIn/Ressources/en.lproj/Localizable.strings index bdfcf8f..9939b51 100644 --- a/Sources/AllInApp/AllIn/Ressources/en.lproj/Localizable.strings +++ b/Sources/AllInApp/AllIn/Ressources/en.lproj/Localizable.strings @@ -132,6 +132,7 @@ "bet_ended" = "Ended on"; "bet_participate" = "Participate"; "bet_proposed_by_format %@" = "Proposed by %@"; +"bet_proposed_by_format" = "Proposed by"; "bet_players_waiting_format %@" = "%@ joueurs en attente"; "bet_players_format" = "players"; "bet_points_at_stake_format" = "points at stake"; diff --git a/Sources/AllInApp/AllIn/Ressources/fr.lproj/Localizable.strings b/Sources/AllInApp/AllIn/Ressources/fr.lproj/Localizable.strings index f3e9b2d..4a22e95 100644 --- a/Sources/AllInApp/AllIn/Ressources/fr.lproj/Localizable.strings +++ b/Sources/AllInApp/AllIn/Ressources/fr.lproj/Localizable.strings @@ -132,6 +132,7 @@ "bet_ended" = "A pris fin le"; "bet_participate" = "Participer"; "bet_proposed_by_format %@" = "Proposé par %@"; +"bet_proposed_by_format" = "Proposé par"; "bet_players_waiting_format %@" = "%@ players waiting"; "bet_players_format" = "joueurs"; "bet_points_at_stake_format" = "points en jeu"; diff --git a/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift index 3f7e383..6b939b9 100644 --- a/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift @@ -17,13 +17,27 @@ class BetViewModel: ObservableObject { @Published private(set) var bets: [Bet] = [] @Published var betsOver: [BetDetail] = [] @Published var showingSheet: Bool = false - - init() { - getItems() - } + @Published var filters: Set = [] { + didSet { + getItems() + } + } + + private var cancellables = Set() + + init() { + getItems() + + // Observer for changes in filters + $filters + .sink { [weak self] _ in + self?.getItems() + } + .store(in: &cancellables) + } func getItems() { - manager.getBets(withIndex: 0, withCount: 20) { bets in + manager.getBets(withIndex: 0, withCount: 20, filters: Array(filters)) { bets in self.bets = bets } manager.getBetsOver() { bets in @@ -31,7 +45,6 @@ class BetViewModel: ObservableObject { if !self.betsOver.isEmpty { self.showingSheet = true } - print(bets) } } } diff --git a/Sources/AllInApp/AllIn/Views/BetView.swift b/Sources/AllInApp/AllIn/Views/BetView.swift index 2878d7a..80e8630 100644 --- a/Sources/AllInApp/AllIn/Views/BetView.swift +++ b/Sources/AllInApp/AllIn/Views/BetView.swift @@ -35,13 +35,10 @@ struct BetView: View { AllInColors.fadeInGradiantCard ScrollView(.horizontal,showsIndicators: false){ HStack{ - ChoiceCapsule() - ChoiceCapsule() - ChoiceCapsule() - ChoiceCapsule() - ChoiceCapsule() - ChoiceCapsule() - ChoiceCapsule() + ChoiceCapsule(filter: .isPublic, viewModel: viewModel) + ChoiceCapsule(filter: .isInvitation, viewModel: viewModel) + ChoiceCapsule(filter: .inProgress, viewModel: viewModel) + ChoiceCapsule(filter: .isFinished, viewModel: viewModel) } .padding(.leading,25) .padding([.top,.bottom],15) diff --git a/Sources/AllInApp/AllIn/Views/DetailsView.swift b/Sources/AllInApp/AllIn/Views/DetailsView.swift index f354f52..f32cbdd 100644 --- a/Sources/AllInApp/AllIn/Views/DetailsView.swift +++ b/Sources/AllInApp/AllIn/Views/DetailsView.swift @@ -16,16 +16,16 @@ struct DetailsView: View { if let betType = viewModel.betDetail?.bet.status { switch betType { case .inProgress: - return ("bet_status_in_progress", AllInColors.darkPurpleColor) + return (String(localized: "bet_status_in_progress"), AllInColors.darkPurpleColor) case .waiting, .closing: - return ("bet_status_waiting", AllInColors.pink100) + return (String(localized: "bet_status_waiting"), AllInColors.pink100) case .finished: - return ("bet_status_finished", AllInColors.grey100Color) + return (String(localized: "bet_status_finished"), AllInColors.grey100Color) case .cancelled: - return ("bet_status_cancelled", AllInColors.grey100Color) + return (String(localized: "bet_status_cancelled"), AllInColors.grey100Color) } } else { - return ("bet_status_unavailable", AllInColors.pink100) + return (String(localized: "bet_status_unavailable"), AllInColors.pink100) } } diff --git a/Sources/Api/Sources/Api/BetApiManager.swift b/Sources/Api/Sources/Api/BetApiManager.swift index d0e1f0e..ec7f5ee 100644 --- a/Sources/Api/Sources/Api/BetApiManager.swift +++ b/Sources/Api/Sources/Api/BetApiManager.swift @@ -16,7 +16,7 @@ public struct BetApiManager: BetDataManager { self.token = token } - public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { + public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter] = [], completion: @escaping ([Bet]) -> Void) { let url = URL(string: allInApi + "bets/gets")! var request = URLRequest(url: url) @@ -24,13 +24,22 @@ public struct BetApiManager: BetDataManager { request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") - var bets: [Bet] = [] + let filterStrings = filters.map { $0.rawValue } + let jsonDictionary: [String: Any] = ["filters": filterStrings] + do { + let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: []) + request.httpBody = jsonData + } catch { + print("Error creating JSON data: \(error)") + return + } URLSession.shared.dataTask(with: request) { data, response, error in if let data = data { print ("ALLIN : get bets") do { if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { + var bets: [Bet] = [] for json in jsonArray { if let bet = FactoryApiBet().toBet(from: json) { bets.append(bet) @@ -45,6 +54,7 @@ public struct BetApiManager: BetDataManager { } }.resume() } + public func getUsers(username: String) -> [User] { return [] diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index 267bf16..ef89393 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -8,7 +8,7 @@ import Foundation import Model -let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api/" +let allInApi = "http://localhost:8080/" public struct UserApiManager: UserDataManager { diff --git a/Sources/Model/Sources/Model/BetDataManager.swift b/Sources/Model/Sources/Model/BetDataManager.swift index 6fede4a..200ccf4 100644 --- a/Sources/Model/Sources/Model/BetDataManager.swift +++ b/Sources/Model/Sources/Model/BetDataManager.swift @@ -8,7 +8,7 @@ import Foundation public protocol BetDataManager { - func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) + func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void) func getUsers(username: String) -> [User] func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) } diff --git a/Sources/Model/Sources/Model/Enums/File.swift b/Sources/Model/Sources/Model/Enums/File.swift new file mode 100644 index 0000000..f5b22f5 --- /dev/null +++ b/Sources/Model/Sources/Model/Enums/File.swift @@ -0,0 +1,16 @@ +// +// File.swift +// +// +// Created by Lucas Delanier on 27/05/2024. +// + +import Foundation + +public enum BetFilter: String, Codable { + case inProgress = "IN_PROGRESS" + case isPublic = "PUBLIC" + case isInvitation = "INVITATION" + case isFinished = "FINISHED" +} + diff --git a/Sources/Model/Sources/Model/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index 65f59aa..afed8f9 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -22,8 +22,8 @@ public struct Manager { } } - public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { - betDataManager.getBets(withIndex: index, withCount: count) { bets in + public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void) { + betDataManager.getBets(withIndex: index, withCount: count, filters: filters) { bets in completion(bets) } } diff --git a/Sources/StubLib/Sources/StubLib/BetStubManager.swift b/Sources/StubLib/Sources/StubLib/BetStubManager.swift index fa9e5bc..acd1460 100644 --- a/Sources/StubLib/Sources/StubLib/BetStubManager.swift +++ b/Sources/StubLib/Sources/StubLib/BetStubManager.swift @@ -12,7 +12,7 @@ public struct BetStubManager: BetDataManager { public init() {} - public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { + public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter] = [], completion: @escaping ([Bet]) -> Void) { completion(Stub.shared.bets) }