fix/api-calls #28

Merged
emre.kartal merged 3 commits from fix/api-calls into master 11 months ago

@ -63,7 +63,6 @@ struct BetCard: View {
.cornerRadius(20, corners: [.bottomLeft,.bottomRight]) .cornerRadius(20, corners: [.bottomLeft,.bottomRight])
.border(width: 1, edges: [.top], color: AllInColors.delimiterGrey) .border(width: 1, edges: [.top], color: AllInColors.delimiterGrey)
} }
.onTapGesture { .onTapGesture {
showDetails.toggle() showDetails.toggle()
} }

@ -51,12 +51,12 @@ struct ChoiceCapsule: View {
} }
} }
.onTapGesture() { .onTapGesture() {
if(!pressed) { pressed.toggle()
if(pressed) {
viewModel.filters.insert(filter) viewModel.filters.insert(filter)
} else { } else {
viewModel.filters.remove(filter) viewModel.filters.remove(filter)
} }
pressed.toggle()
} }
} }

@ -6,8 +6,13 @@
// //
import SwiftUI import SwiftUI
import Model
struct TrendingBetCard: View { struct TrendingBetCard: View {
var bet: Bet
@State var showDetails: Bool = false
var body: some View { var body: some View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
HStack { HStack {
@ -20,7 +25,7 @@ struct TrendingBetCard: View {
} }
.padding([.leading, .top], 10) .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) .textStyle(weight: .heavy, color: .white, size: 17)
.frame(height: 47) .frame(height: 47)
.multilineTextAlignment(.leading) .multilineTextAlignment(.leading)
@ -61,6 +66,14 @@ struct TrendingBetCard: View {
struct TrendingBetCard_Previews: PreviewProvider { struct TrendingBetCard_Previews: PreviewProvider {
static var previews: some View { 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: []))
} }
} }

@ -14,6 +14,7 @@ class BetViewModel: ObservableObject {
@Inject var manager: Manager @Inject var manager: Manager
@Published var popularBet: Bet?
@Published private(set) var bets: [Bet] = [] @Published private(set) var bets: [Bet] = []
@Published var betsOver: [BetDetail] = [] @Published var betsOver: [BetDetail] = []
@Published var showingSheet: Bool = false @Published var showingSheet: Bool = false
@ -23,17 +24,9 @@ class BetViewModel: ObservableObject {
} }
} }
private var cancellables = Set<AnyCancellable>()
init() { init() {
getItems() getItems()
getPopularBet()
// Observer for changes in filters
$filters
.sink { [weak self] _ in
self?.getItems()
}
.store(in: &cancellables)
} }
func getItems() { func getItems() {
@ -51,4 +44,12 @@ class BetViewModel: ObservableObject {
} }
} }
} }
func getPopularBet() {
manager.getPopularBet() { bet in
DispatchQueue.main.async {
self.popularBet = bet
}
}
}
} }

@ -14,11 +14,15 @@ class HistoricBetViewModel: ObservableObject {
@Inject var manager: Manager @Inject var manager: Manager
@Published private(set) var bets: [BetDetail] = []
init() { init() {
getItems() getItems()
} }
func getItems() { func getItems() {
manager.getHistoricBets(withIndex: 0, withCount: 20) { bets in
self.bets = bets
}
} }
} }

@ -20,7 +20,11 @@ struct BetView: View {
ScrollView(showsIndicators: false) { ScrollView(showsIndicators: false) {
LazyVStack(alignment: .leading, spacing: 0, pinnedViews: [.sectionHeaders]) { LazyVStack(alignment: .leading, 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 { Section {
VStack(spacing: 20){ VStack(spacing: 20){
@ -62,14 +66,5 @@ struct BetView: View {
} }
.edgesIgnoringSafeArea(.bottom) .edgesIgnoringSafeArea(.bottom)
.background(AllInColors.backgroundColor) .background(AllInColors.backgroundColor)
}
}
struct BetView_Previews: PreviewProvider {
static var previews: some View {
BetView(showMenu: .constant(false))
.preferredColorScheme(.light)
} }
} }

@ -7,7 +7,6 @@
import SwiftUI import SwiftUI
import Model import Model
import StubLib
struct CurrentBetView: View { struct CurrentBetView: View {

@ -6,9 +6,11 @@
// //
import SwiftUI import SwiftUI
import Model
struct HistoricBetView: View { struct HistoricBetView: View {
@StateObject private var viewModel = HistoricBetViewModel()
@Binding var showMenu: Bool @Binding var showMenu: Bool
@State private var showingSheet = false @State private var showingSheet = false
@ -22,8 +24,9 @@ struct HistoricBetView: View {
.textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25) .textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25)
.padding([.top],15) .padding([.top],15)
VStack(spacing: 20){ VStack(spacing: 20){
// ReviewCard(amountBetted: 110, isAWin: true) ForEach(viewModel.bets, id: \.bet.id) { (bet: BetDetail) in
// ReviewCard(amountBetted: 3, isAWin: false) ReviewCard(betDetail: bet, amountBetted: 110, isAWin: false)
}
} }
.padding([.trailing, .leading, .bottom],25) .padding([.trailing, .leading, .bottom],25)
} }

@ -26,6 +26,7 @@ public struct BetApiManager: BetDataManager {
let filterStrings = filters.map { $0.rawValue } let filterStrings = filters.map { $0.rawValue }
let jsonDictionary: [String: Any] = ["filters": filterStrings] let jsonDictionary: [String: Any] = ["filters": filterStrings]
do { do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: []) let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
request.httpBody = jsonData request.httpBody = jsonData
@ -85,4 +86,30 @@ public struct BetApiManager: BetDataManager {
} }
}.resume() }.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()
}
} }

@ -194,8 +194,35 @@ public struct UserApiManager: UserDataManager {
}.resume() }.resume()
} }
public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) { public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) {
fatalError("Not implemented yet") 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) { public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) {

@ -11,4 +11,6 @@ public protocol BetDataManager {
func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void) func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void)
func getUsers(username: String) -> [User] func getUsers(username: String) -> [User]
func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) func getBet(withId id: String, completion: @escaping (BetDetail) -> Void)
func getPopularBet(completion: @escaping (Bet) -> Void)
} }

@ -64,12 +64,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 userDataManager.getOldBets(withIndex: index, withCount: count) { bets in
completion(bets) 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) { public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) {
userDataManager.getCurrentBets(withIndex: index, withCount: count) { bets in userDataManager.getCurrentBets(withIndex: index, withCount: count) { bets in
completion(bets) completion(bets)

@ -16,7 +16,7 @@ public protocol UserDataManager {
func getFriends(completion: @escaping ([User]) -> Void) func getFriends(completion: @escaping ([User]) -> Void)
func getUsers(withName name: String, completion: @escaping ([User]) -> Void) func getUsers(withName name: String, completion: @escaping ([User]) -> Void)
func getGifts(completion : @escaping (Int, Int)-> ()) 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 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 addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ())
func addResponse(withIdBet id: String, andResponse responseBet: String) func addResponse(withIdBet id: String, andResponse responseBet: String)

@ -29,6 +29,10 @@ public struct BetStubManager: BetDataManager {
} }
} }
public func getPopularBet(completion: @escaping (Bet) -> Void) {
}
public func getABetDetail() -> BetDetail{ public func getABetDetail() -> BetDetail{
Stub.shared.betsDetail.first! Stub.shared.betsDetail.first!
} }

Loading…
Cancel
Save