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/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/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index a956ead..34a77c8 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -194,8 +194,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 current 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/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index 33dc10b..4eb319e 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -64,7 +64,7 @@ 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) } diff --git a/Sources/Model/Sources/Model/UserDataManager.swift b/Sources/Model/Sources/Model/UserDataManager.swift index 4b76f5f..fd6f4fb 100644 --- a/Sources/Model/Sources/Model/UserDataManager.swift +++ b/Sources/Model/Sources/Model/UserDataManager.swift @@ -16,7 +16,7 @@ public protocol UserDataManager { func getFriends(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)