diff --git a/Sources/AllInApp/AllIn/Components/BetCard.swift b/Sources/AllInApp/AllIn/Components/BetCard.swift index 2fb87a9..c261ed5 100644 --- a/Sources/AllInApp/AllIn/Components/BetCard.swift +++ b/Sources/AllInApp/AllIn/Components/BetCard.swift @@ -13,7 +13,7 @@ struct BetCard: View { var bet: Bet @State var showDetails: Bool = false @State var showParticipate: Bool = false - + var body: some View { VStack(spacing: 0) { VStack(alignment: .leading,spacing: 2){ @@ -63,12 +63,11 @@ struct BetCard: View { .cornerRadius(20, corners: [.bottomLeft,.bottomRight]) .border(width: 1, edges: [.top], color: AllInColors.delimiterGrey) } - .onTapGesture { showDetails.toggle() } .fullScreenCover(isPresented: $showDetails) { - DetailsView(isModalPresented: $showDetails, isModalParticipated: $showParticipate,id: bet.id) + DetailsView(isModalPresented: $showDetails, isModalParticipated: $showParticipate, id: bet.id) } } } diff --git a/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift b/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift index da3c1c9..d98fbfb 100644 --- a/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift +++ b/Sources/AllInApp/AllIn/Components/ChoiceCapsule.swift @@ -51,12 +51,12 @@ struct ChoiceCapsule: View { } } .onTapGesture() { - if(!pressed) { + pressed.toggle() + if(pressed) { viewModel.filters.insert(filter) } else { viewModel.filters.remove(filter) } - pressed.toggle() } } diff --git a/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift b/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift index 82f39a7..de512af 100644 --- a/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift +++ b/Sources/AllInApp/AllIn/Components/TrendingBetCard.swift @@ -6,8 +6,13 @@ // import SwiftUI +import Model struct TrendingBetCard: View { + + var bet: Bet + @State var showDetails: Bool = false + var body: some View { VStack(alignment: .leading) { HStack { @@ -20,7 +25,7 @@ struct TrendingBetCard: View { } .padding([.leading, .top], 10) - Text("Emre va réussir son TP de CI/CD mercredi?") + Text(bet.theme) .textStyle(weight: .heavy, color: .white, size: 17) .frame(height: 47) .multilineTextAlignment(.leading) @@ -61,6 +66,14 @@ struct TrendingBetCard: View { struct TrendingBetCard_Previews: PreviewProvider { static var previews: some View { - TrendingBetCard() + TrendingBetCard(bet: BinaryBet(theme: "Football - Finale de la Ligue des Champions", + phrase: "Le gagnant de la finale sera l'équipe avec le plus de tirs au but.", + endRegisterDate: Date().addingTimeInterval(86400), + endBetDate: Date().addingTimeInterval(172800), + isPublic: true, + status: .inProgress, + invited: [], + author: "Imri", + registered: [])) } } diff --git a/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift index 5d994bd..4b73477 100644 --- a/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/BetViewModel.swift @@ -14,27 +14,20 @@ class BetViewModel: ObservableObject { @Inject var manager: Manager + @Published var popularBet: Bet? @Published private(set) var bets: [Bet] = [] @Published var betsOver: [BetDetail] = [] @Published var showingSheet: Bool = false @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) - } + didSet { + getItems() + } + } + + init() { + getItems() + getPopularBet() + } func getItems() { manager.getBets(withIndex: 0, withCount: 20, filters: Array(filters)) { bets in @@ -51,4 +44,12 @@ class BetViewModel: ObservableObject { } } } + + func getPopularBet() { + manager.getPopularBet() { bet in + DispatchQueue.main.async { + self.popularBet = bet + } + } + } } diff --git a/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift index 23b7428..69e2bb1 100644 --- a/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift @@ -12,7 +12,7 @@ import Model class CurrentBetViewModel: ObservableObject { @Inject var manager: Manager - + @Published private(set) var bets: [BetDetail] = [] init() { diff --git a/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift index 480c7a1..5ad222b 100644 --- a/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/HistoricBetViewModel.swift @@ -13,12 +13,16 @@ import Model class HistoricBetViewModel: ObservableObject { @Inject var manager: Manager - + + @Published private(set) var bets: [BetDetail] = [] + init() { getItems() } func getItems() { - + manager.getHistoricBets(withIndex: 0, withCount: 20) { bets in + self.bets = bets + } } } diff --git a/Sources/AllInApp/AllIn/Views/BetView.swift b/Sources/AllInApp/AllIn/Views/BetView.swift index e0f0dc4..6e4dea9 100644 --- a/Sources/AllInApp/AllIn/Views/BetView.swift +++ b/Sources/AllInApp/AllIn/Views/BetView.swift @@ -20,7 +20,11 @@ struct BetView: View { ScrollView(showsIndicators: false) { LazyVStack(alignment: .center, spacing: 0, pinnedViews: [.sectionHeaders]) { - TrendingBetCard().padding(.top,25).padding([.leading,.trailing],25) + if let bet = viewModel.popularBet { + TrendingBetCard(bet: bet) + .padding(.top,25) + .padding([.leading,.trailing],25) + } Section { VStack(spacing: 20){ @@ -67,14 +71,5 @@ struct BetView: View { } .edgesIgnoringSafeArea(.bottom) .background(AllInColors.backgroundColor) - - } -} - - -struct BetView_Previews: PreviewProvider { - static var previews: some View { - BetView(showMenu: .constant(false)) - .preferredColorScheme(.light) } } diff --git a/Sources/AllInApp/AllIn/Views/CurrentBetView.swift b/Sources/AllInApp/AllIn/Views/CurrentBetView.swift index f9d3817..2c5279f 100644 --- a/Sources/AllInApp/AllIn/Views/CurrentBetView.swift +++ b/Sources/AllInApp/AllIn/Views/CurrentBetView.swift @@ -7,7 +7,6 @@ import SwiftUI import Model -import StubLib struct CurrentBetView: View { diff --git a/Sources/AllInApp/AllIn/Views/HistoricBetView.swift b/Sources/AllInApp/AllIn/Views/HistoricBetView.swift index 3768a8e..0df01ed 100644 --- a/Sources/AllInApp/AllIn/Views/HistoricBetView.swift +++ b/Sources/AllInApp/AllIn/Views/HistoricBetView.swift @@ -6,9 +6,11 @@ // import SwiftUI +import Model struct HistoricBetView: View { + @StateObject private var viewModel = HistoricBetViewModel() @Binding var showMenu: Bool @State private var showingSheet = false @@ -22,8 +24,9 @@ struct HistoricBetView: View { .textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25) .padding([.top],15) VStack(spacing: 20){ -// ReviewCard(amountBetted: 110, isAWin: true) -// ReviewCard(amountBetted: 3, isAWin: false) + ForEach(viewModel.bets, id: \.bet.id) { (bet: BetDetail) in + ReviewCard(betDetail: bet, amountBetted: 110, isAWin: false) + } } .padding([.trailing, .leading, .bottom],25) } diff --git a/Sources/Api/Sources/Api/BetApiManager.swift b/Sources/Api/Sources/Api/BetApiManager.swift index ec7f5ee..e764231 100644 --- a/Sources/Api/Sources/Api/BetApiManager.swift +++ b/Sources/Api/Sources/Api/BetApiManager.swift @@ -26,6 +26,7 @@ public struct BetApiManager: BetDataManager { let filterStrings = filters.map { $0.rawValue } let jsonDictionary: [String: Any] = ["filters": filterStrings] + do { let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: []) request.httpBody = jsonData @@ -85,4 +86,30 @@ public struct BetApiManager: BetDataManager { } }.resume() } + + public func getPopularBet(completion: @escaping (Bet) -> Void) { + let url = URL(string: allInApi + "bets/popular")! + + var request = URLRequest(url: url) + request.httpMethod = "GET" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") + + URLSession.shared.dataTask(with: request) { data, response, error in + if let data = data { + print ("ALLIN : get popular bet") + do { + if let httpResponse = response as? HTTPURLResponse, let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { + + if let bet = FactoryApiBet().toBet(from: json) { + completion(bet) + } + print(httpResponse.statusCode) + } + } catch { + print("Error parsing JSON: \(error)") + } + } + }.resume() + } } diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index 1b9ea35..d5787ff 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -221,8 +221,35 @@ public struct UserApiManager: UserDataManager { }.resume() } - public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { - fatalError("Not implemented yet") + public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { + let url = URL(string: allInApi + "bets/history")! + + var request = URLRequest(url: url) + request.httpMethod = "GET" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") + + var bets: [BetDetail] = [] + + URLSession.shared.dataTask(with: request) { data, response, error in + if let data = data { + print ("ALLIN : get old bets") + 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) + } + } + print(httpResponse.statusCode) + completion(bets) + } + } catch { + print("Error parsing JSON: \(error)") + } + } + }.resume() } public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { diff --git a/Sources/Model/Sources/Model/BetDataManager.swift b/Sources/Model/Sources/Model/BetDataManager.swift index 200ccf4..d370b06 100644 --- a/Sources/Model/Sources/Model/BetDataManager.swift +++ b/Sources/Model/Sources/Model/BetDataManager.swift @@ -11,4 +11,6 @@ public protocol BetDataManager { 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) + func getPopularBet(completion: @escaping (Bet) -> Void) + } diff --git a/Sources/Model/Sources/Model/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index 0620247..1ffe1e1 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -70,12 +70,18 @@ public struct Manager { } } - public func getHistoricBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { + public func getHistoricBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { userDataManager.getOldBets(withIndex: index, withCount: count) { bets in completion(bets) } } + public func getPopularBet(completion: @escaping (Bet) -> Void) { + betDataManager.getPopularBet() { bet in + completion(bet) + } + } + public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) { userDataManager.getCurrentBets(withIndex: index, withCount: count) { bets in completion(bets) diff --git a/Sources/Model/Sources/Model/UserDataManager.swift b/Sources/Model/Sources/Model/UserDataManager.swift index 6c805a1..3eaccaa 100644 --- a/Sources/Model/Sources/Model/UserDataManager.swift +++ b/Sources/Model/Sources/Model/UserDataManager.swift @@ -17,7 +17,7 @@ public protocol UserDataManager { func getRequests(completion: @escaping ([User]) -> Void) func getUsers(withName name: String, completion: @escaping ([User]) -> Void) func getGifts(completion : @escaping (Int, Int)-> ()) - func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) + func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ()) func addResponse(withIdBet id: String, andResponse responseBet: String) diff --git a/Sources/StubLib/Sources/StubLib/BetStubManager.swift b/Sources/StubLib/Sources/StubLib/BetStubManager.swift index acd1460..f5cd6ef 100644 --- a/Sources/StubLib/Sources/StubLib/BetStubManager.swift +++ b/Sources/StubLib/Sources/StubLib/BetStubManager.swift @@ -29,6 +29,10 @@ public struct BetStubManager: BetDataManager { } } + public func getPopularBet(completion: @escaping (Bet) -> Void) { + + } + public func getABetDetail() -> BetDetail{ Stub.shared.betsDetail.first! }