pull/25/head
Lucas DELANIER 1 year ago
parent 80b5f9ba51
commit 431c95e84c

@ -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)

@ -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()
}
}

@ -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/"
}

@ -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";

@ -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";

@ -17,13 +17,27 @@ class BetViewModel: ObservableObject {
@Published private(set) var bets: [Bet] = []
@Published var betsOver: [BetDetail] = []
@Published var showingSheet: Bool = false
@Published var filters: Set<BetFilter> = [] {
didSet {
getItems()
}
}
init() {
getItems()
}
private var cancellables = Set<AnyCancellable>()
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)
}
}
}

@ -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)

@ -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)
}
}

@ -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)
@ -46,6 +55,7 @@ public struct BetApiManager: BetDataManager {
}.resume()
}
public func getUsers(username: String) -> [User] {
return []
}

@ -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 {

@ -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)
}

@ -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"
}

@ -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)
}
}

@ -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)
}

Loading…
Cancel
Save