From 86b6e0c76534e7dc3df328fccb0ab67261b1c2dc Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 31 Jan 2024 16:11:27 +0100 Subject: [PATCH] Add View CurrentBet --- Sources/AllInApp/AllIn/Components/Menu.swift | 21 +++++++--- .../ViewModels/CurrentBetViewModel.swift | 28 +++++++++++++ .../AllInApp/AllIn/Views/CurrentBetView.swift | 42 +++++++++++++++++++ .../AllIn/Views/HistoricBetView.swift | 1 - Sources/AllInApp/AllIn/Views/MainView.swift | 2 + .../AllInApp.xcodeproj/project.pbxproj | 8 ++++ Sources/Api/Sources/Api/UserApiManager.swift | 30 +++++++++++++ Sources/Model/Sources/Model/Manager.swift | 6 +++ .../Model/Sources/Model/UserDataManager.swift | 1 + 9 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift create mode 100644 Sources/AllInApp/AllIn/Views/CurrentBetView.swift diff --git a/Sources/AllInApp/AllIn/Components/Menu.swift b/Sources/AllInApp/AllIn/Components/Menu.swift index 3bfd971..9d184d8 100644 --- a/Sources/AllInApp/AllIn/Components/Menu.swift +++ b/Sources/AllInApp/AllIn/Components/Menu.swift @@ -86,17 +86,26 @@ struct Menu: View { .padding([.leading,.trailing], 13) } - NavigationLink(destination: MainView(page: "Bet").navigationBarBackButtonHidden(true)) - { - ParameterMenu(image: "moneyImage", title: "BET EN COURS", description: "Gérez vos bets et récompensez les gagnants.") - .padding([.leading,.trailing], 13) + VStack { + NavigationLink(destination: MainView(page: "Bet").navigationBarBackButtonHidden(true)) + { + ParameterMenu(image: "moneyImage", title: "BET EN COURS", description: "Gérez vos bets et récompensez les gagnants.") + .padding([.leading,.trailing], 13) + } + NavigationLink(destination: MainView(page: "Ranking").navigationBarBackButtonHidden(true)) + { + ParameterMenu(image: "rankingImage", title: "CLASSEMENT", description: "Consultez votre classement parmis vos amis.") + .padding([.leading,.trailing], 13) + } } - NavigationLink(destination: MainView(page: "Ranking").navigationBarBackButtonHidden(true)) + + NavigationLink(destination: MainView(page: "Current").navigationBarBackButtonHidden(true)) { - ParameterMenu(image: "rankingImage", title: "CLASSEMENT", description: "Consultez votre classement parmis vos amis.") + ParameterMenu(image: "eyesImage", title: "BETS EN COURS", description: "Consultez vos paris en cours.") .padding([.leading,.trailing], 13) } + HStack { Spacer() Button { diff --git a/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift new file mode 100644 index 0000000..70691eb --- /dev/null +++ b/Sources/AllInApp/AllIn/ViewModels/CurrentBetViewModel.swift @@ -0,0 +1,28 @@ +// +// CurrentBetViewModel.swift +// AllIn +// +// Created by Emre on 31/01/2024. +// + +import Foundation +import DependencyInjection +import Model + +class CurrentBetViewModel: ObservableObject { + + @Inject var manager: Manager + + @Published private(set) var bets: [Bet] = [] + + init() { + getItems() + } + + func getItems() { + manager.getCurrentBets(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 new file mode 100644 index 0000000..275e93b --- /dev/null +++ b/Sources/AllInApp/AllIn/Views/CurrentBetView.swift @@ -0,0 +1,42 @@ +// +// CurrentBetView.swift +// AllIn +// +// Created by Emre on 31/01/2024. +// + +import SwiftUI +import Model + +struct CurrentBetView: View { + + @StateObject private var viewModel = CurrentBetViewModel() + @Binding var showMenu: Bool + @State private var showingSheet = false + + var body: some View { + + VStack(alignment: .center, spacing: 0) { + + TopBar(showMenu: self.$showMenu) + ScrollView(showsIndicators: false) { + Text("En cours") + .textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25) + .padding([.top],15) + VStack(spacing: 20){ + ForEach(viewModel.bets, id: \.id) { (bet: Bet) in + ReviewCard(amountBetted: 110, isAWin: false) + } + } + .padding([.trailing, .leading, .bottom],25) + } + .refreshable { + viewModel.getItems() + } + Spacer() + } + .edgesIgnoringSafeArea(.bottom) + .background(AllInColors.backgroundColor) + } +} + diff --git a/Sources/AllInApp/AllIn/Views/HistoricBetView.swift b/Sources/AllInApp/AllIn/Views/HistoricBetView.swift index beaebcb..2e4dea0 100644 --- a/Sources/AllInApp/AllIn/Views/HistoricBetView.swift +++ b/Sources/AllInApp/AllIn/Views/HistoricBetView.swift @@ -9,7 +9,6 @@ import SwiftUI struct HistoricBetView: View { - @StateObject private var viewModel = BetViewModel() @Binding var showMenu: Bool @State private var showingSheet = false diff --git a/Sources/AllInApp/AllIn/Views/MainView.swift b/Sources/AllInApp/AllIn/Views/MainView.swift index 9b7900a..5dae676 100644 --- a/Sources/AllInApp/AllIn/Views/MainView.swift +++ b/Sources/AllInApp/AllIn/Views/MainView.swift @@ -46,6 +46,8 @@ struct MainView: View { FriendsView(showMenu: self.$showMenu) case "CreationBet": CreationBetView(showMenu: self.$showMenu) + case "Current": + CurrentBetView(showMenu: self.$showMenu) default: BetView(showMenu: self.$showMenu) } diff --git a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj index 9d640e0..da2b013 100644 --- a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj +++ b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ EC01937E2B25C52E005D81E6 /* TopBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC01937D2B25C52E005D81E6 /* TopBar.swift */; }; EC01FCC32B56650400BB2390 /* DetailsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC01FCC22B56650400BB2390 /* DetailsViewModel.swift */; }; EC01FCC52B56791B00BB2390 /* HistoricBetViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC01FCC42B56791B00BB2390 /* HistoricBetViewModel.swift */; }; + EC17A15E2B6A955E008A8679 /* CurrentBetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC17A15D2B6A955E008A8679 /* CurrentBetView.swift */; }; + EC17A1602B6A9781008A8679 /* CurrentBetViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC17A15F2B6A9781008A8679 /* CurrentBetViewModel.swift */; }; EC3077072B24CB840060E34D /* SplashView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC3077062B24CB840060E34D /* SplashView.swift */; }; EC3077092B24CF7F0060E34D /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC3077082B24CF7F0060E34D /* Colors.swift */; }; EC30770B2B24D9160060E34D /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770A2B24D9160060E34D /* WelcomeView.swift */; }; @@ -123,6 +125,8 @@ EC01937D2B25C52E005D81E6 /* TopBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopBar.swift; sourceTree = ""; }; EC01FCC22B56650400BB2390 /* DetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailsViewModel.swift; sourceTree = ""; }; EC01FCC42B56791B00BB2390 /* HistoricBetViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricBetViewModel.swift; sourceTree = ""; }; + EC17A15D2B6A955E008A8679 /* CurrentBetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentBetView.swift; sourceTree = ""; }; + EC17A15F2B6A9781008A8679 /* CurrentBetViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentBetViewModel.swift; sourceTree = ""; }; EC3077062B24CB840060E34D /* SplashView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashView.swift; sourceTree = ""; }; EC3077082B24CF7F0060E34D /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; EC30770A2B24D9160060E34D /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = ""; }; @@ -309,6 +313,7 @@ 1244EF5F2B4EC31E00374ABF /* HistoricBetView.swift */, 123590B32B51792000F7AEBD /* DetailsView.swift */, 123225D82B67B46100D30BB3 /* BetEndingValidationView.swift */, + EC17A15D2B6A955E008A8679 /* CurrentBetView.swift */, ); path = Views; sourceTree = ""; @@ -367,6 +372,7 @@ ECB26A1A2B40746C00FE06B3 /* FriendsViewModel.swift */, EC01FCC22B56650400BB2390 /* DetailsViewModel.swift */, EC01FCC42B56791B00BB2390 /* HistoricBetViewModel.swift */, + EC17A15F2B6A9781008A8679 /* CurrentBetViewModel.swift */, ); path = ViewModels; sourceTree = ""; @@ -526,11 +532,13 @@ ECB26A192B40744F00FE06B3 /* RankingViewModel.swift in Sources */, EC650A502B2793D5003AFCAD /* TextCapsule.swift in Sources */, EC650A482B25DCFF003AFCAD /* UsersPreview.swift in Sources */, + EC17A15E2B6A955E008A8679 /* CurrentBetView.swift in Sources */, EC650A462B25D686003AFCAD /* RankingRow.swift in Sources */, EC01937A2B25C12B005D81E6 /* BetCard.swift in Sources */, EC650A422B25C817003AFCAD /* Friend.swift in Sources */, EC7A882F2B28E6BE004F226A /* ConfidentialityButton.swift in Sources */, ECB7BC702B336E28002A6654 /* RegisterViewModel.swift in Sources */, + EC17A1602B6A9781008A8679 /* CurrentBetViewModel.swift in Sources */, EC650A4C2B25E9C7003AFCAD /* RankingView.swift in Sources */, EC7A882B2B28D1E0004F226A /* DropDownMenu.swift in Sources */, EC7A882D2B28D8A1004F226A /* CreationBetView.swift in Sources */, diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index 59a79cc..c10fe43 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -51,6 +51,36 @@ public struct UserApiManager: UserDataManager { fatalError("Not implemented yet") } + public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { + let url = URL(string: allInApi + "bets/current")! + + var request = URLRequest(url: url) + request.httpMethod = "GET" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") + + var bets: [Bet] = [] + + 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 { + if let bet = FactoryApiBet().toBet(from: json) { + bets.append(bet) + } + } + print(httpResponse.statusCode) + completion(bets) + } + } catch { + print("Error parsing JSON: \(error)") + } + } + }.resume() + } + public func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ()) { let url = URL(string: allInApi + "participations/add")! var request = URLRequest(url: url) diff --git a/Sources/Model/Sources/Model/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index b8cbc23..5f65694 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -40,6 +40,12 @@ public struct Manager { } } + public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { + userDataManager.getCurrentBets(withIndex: index, withCount: count) { bets in + completion(bets) + } + } + public func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ()) { userDataManager.addParticipation(withId: id, withAnswer: answer, andStake: stake) { status in completion(status) diff --git a/Sources/Model/Sources/Model/UserDataManager.swift b/Sources/Model/Sources/Model/UserDataManager.swift index 2fdc461..ff132db 100644 --- a/Sources/Model/Sources/Model/UserDataManager.swift +++ b/Sources/Model/Sources/Model/UserDataManager.swift @@ -12,5 +12,6 @@ public protocol UserDataManager { func addBet(bet: Bet, completion : @escaping (Int)-> ()) func getFriends() -> [User] func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) + func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ()) }