Update comments and fix refresh betPage after i add a Bet

fix/Model_Details
Emre KARTAL 1 year ago
parent 1a581825e5
commit 542c8a6b3a

@ -25,6 +25,9 @@ class CreationBetViewModel: ObservableObject {
@Published var endRegisterDateFieldError: String?
@Published var endBetDateFieldError: String?
@Published var errorMessage: String?
@Published var showErrorMessage = false
func create() {
guard checkAndSetError(forTheme: true, forDescription: true, forEndRegisterDate: true, forEndBetDate: true) else {
@ -34,10 +37,15 @@ class CreationBetViewModel: ObservableObject {
resetAllFieldErrors()
if let user = AppStateContainer.shared.user {
manager.addBet(theme: theme, description: description, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: user)
manager.addBet(theme: theme, description: description, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: user) { statusCode in
switch statusCode {
case 201:
self.betAdded = true
default:
self.setErrorMessage(errorMessage: "Problème de connexion. Veuillez réessayer ultérieurement.")
}
}
}
betAdded = true
}
func checkAndSetError(forTheme checkTheme: Bool, forDescription checkDescription: Bool, forEndRegisterDate checkEndRegisterDate: Bool, forEndBetDate checkEndBetDate: Bool) -> Bool {
@ -95,4 +103,9 @@ class CreationBetViewModel: ObservableObject {
endBetDateFieldError = nil
}
}
func setErrorMessage(errorMessage: String) {
self.showErrorMessage = true
self.errorMessage = errorMessage
}
}

@ -477,6 +477,9 @@ struct CreationBetView: View {
.onTapGesture {
hideKeyboard()
}
.alert(isPresented: $viewModel.showErrorMessage) {
Alert(title: Text("Erreur lors de la création d'un Bet"), message: Text(viewModel.errorMessage ?? ""), dismissButton: .default(Text("OK")))
}
.edgesIgnoringSafeArea(.bottom)
.background(AllInColors.backgroundColor)
}

@ -22,7 +22,7 @@ public struct UserApiManager: UserDataManager {
fatalError("Not implemented yet")
}
public func addBet(bet: Bet) {
public func addBet(bet: Bet, completion : @escaping (Int)-> ()) {
let url = URL(string: allInApi + "bets/add")!
var request = URLRequest(url: url)
@ -37,6 +37,7 @@ public struct UserApiManager: UserDataManager {
print ("ALLIN : Add BET")
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
completion(httpResponse.statusCode)
}
}.resume()
}

@ -1,6 +1,6 @@
//
// Bet.swift
//
//
//
// Created by Emre on 28/12/2023.
//
@ -28,6 +28,7 @@ public class Bet: ObservableObject, Identifiable {
/// Indicates whether the bet is public or private.
public private(set) var isPublic: Bool
/// The current status of the bet.
public private(set) var status: BetStatus
/// List of users who are invited to participate in the bet.
@ -49,6 +50,7 @@ public class Bet: ObservableObject, Identifiable {
/// - endRegisterDate: The deadline for users to register for the bet.
/// - endBetDate: The deadline for the actual betting to take place.
/// - isPublic: Indicates whether the bet is public or private.
/// - status: The current status of the bet.
/// - invited: List of users who are invited to participate in the bet.
/// - author: The user who created the bet.
/// - registered: List of users who have registered for the bet.
@ -73,6 +75,7 @@ public class Bet: ObservableObject, Identifiable {
/// - endRegisterDate: The deadline for users to register for the bet.
/// - endBetDate: The deadline for the actual betting to take place.
/// - isPublic: Indicates whether the bet is public or private.
/// - status: The current status of the bet.
/// - invited: List of users who are invited to participate in the bet.
/// - author: The user who created the bet.
/// - registered: List of users who have registered for the bet.
@ -89,6 +92,9 @@ public class Bet: ObservableObject, Identifiable {
self.registered = registered
}
/// Adds a new user to the list of registered participants for the bet.
///
/// - Parameter newUser: The user to be added to the list of registered participants.
public func addRegistered(newUser: User){
self.registered.append(newUser)
}

@ -19,6 +19,7 @@ public class BetDetail: ObservableObject {
/// List of user participations in the bet.
public private(set) var participations: [Participation]
/// The final answer selected for the bet, if available.
public private(set) var finalAnswer: String?
/// Custom Constructor
@ -27,7 +28,7 @@ public class BetDetail: ObservableObject {
/// - bet: The main bet information.
/// - answers: Details about the answers available for the bet.
/// - participations: List of user participations in the bet.
/// - userParticipation: The user's own participation in the bet.
/// - finalAnswer: The final answer selected for the bet, if available.
public init(bet: Bet, answers: [AnswerDetail], participations: [Participation], finalAnswer: String? = nil) {
self.bet = bet
self.answers = answers
@ -35,17 +36,23 @@ public class BetDetail: ObservableObject {
self.finalAnswer = finalAnswer
}
/// Updates the final answer selected for the bet.
///
/// - Parameter newFinalAnswer: The new final answer for the bet.
public func updateFinalAnswer(newFinalAnswer: String) {
self.finalAnswer = newFinalAnswer
}
self.finalAnswer = newFinalAnswer
}
/// Adds a new user participation to the list of participations for the bet.
///
/// - Parameter newParticipation: The new user participation to be added.
public func addParticipation(newParticipation: Participation){
if !self.bet.registered.contains(where: { existingUser in
return existingUser.email == newParticipation.user.email
}) {
self.bet.addRegistered(newUser: newParticipation.user)
}
self.participations.append(newParticipation)
self.bet.addRegistered(newUser: newParticipation.user)
}
self.participations.append(newParticipation)
}
}

@ -1,8 +1,8 @@
//
// File.swift
// BetStatus.swift
//
//
// Created by étudiant on 23/01/2024.
// Created by Emre on 23/01/2024.
//
import Foundation

@ -12,9 +12,11 @@ public class CustomBetResponse {
/// The name or description of the custom bet response.
public var name: String
/// Custom Constructor
///
/// - Parameter name: The name or description of the custom bet response.
///
/// - Parameters:
/// - name: The name or description of the custom bet response.
public init(name: String) {
self.name = name
}

@ -16,8 +16,10 @@ public struct Manager {
self.userDataManager = userDataManager
}
public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User) {
userDataManager.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: .IN_PROGRESS, invited: [], author: creator, registered: []))
public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, completion : @escaping (Int)-> ()) {
userDataManager.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: .IN_PROGRESS, invited: [], author: creator, registered: [])) { status in
completion(status)
}
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {

@ -1,6 +1,6 @@
//
// MatchBet.swift
//
//
//
// Created by Emre on 28/12/2023.
//
@ -23,8 +23,8 @@ public class MatchBet: Bet {
/// - phrase: The specific phrase or question related to the match bet.
/// - endRegisterDate: The deadline for users to register for the match bet.
/// - endBetDate: The deadline for the actual betting to take place for the match bet.
/// - totalStakes: The total stakes or amount involved in the match bet.
/// - isPublic: Indicates whether the match bet is public or private.
/// - status: The current status of the match bet.
/// - invited: List of users who are invited to participate in the match bet.
/// - author: The user who created the match bet.
/// - registered: List of users who have registered for the match bet.
@ -43,8 +43,8 @@ public class MatchBet: Bet {
/// - phrase: The specific phrase or question related to the match bet.
/// - endRegisterDate: The deadline for users to register for the match bet.
/// - endBetDate: The deadline for the actual betting to take place for the match bet.
/// - totalStakes: The total stakes or amount involved in the match bet.
/// - isPublic: Indicates whether the match bet is public or private.
/// - status: The current status of the match bet.
/// - invited: List of users who are invited to participate in the match bet.
/// - author: The user who created the match bet.
/// - registered: List of users who have registered for the match bet.

@ -7,12 +7,28 @@
import Foundation
/// A struct representing a user with details such as username, email, number of coins, and friends.
public struct User {
/// The username of the user.
public private(set) var username: String
/// The email address of the user.
public private(set) var email: String
/// The number of coins associated with the user.
public private(set) var nbCoins: Int
/// List of friends associated with the user.
public private(set) var friends: [User]
/// Custom constructor to initialize a User instance.
///
/// - Parameters:
/// - username: The username of the user.
/// - email: The email address of the user.
/// - nbCoins: The number of coins associated with the user.
/// - friends: List of friends associated with the user.
public init(username: String, email: String, nbCoins: Int, friends: [User]) {
self.username = username
self.email = email
@ -20,10 +36,17 @@ public struct User {
self.friends = friends
}
/// Adds a new friend to the list of friends for the user.
///
/// - Parameter user: The user to be added as a friend.
public mutating func addFriend(user: User) {
self.friends.append(user)
}
/// Maps user data from a JSON dictionary to create a User instance.
///
/// - Parameter json: The JSON dictionary containing user data.
/// - Returns: An optional User instance if mapping is successful, otherwise nil.
public static func mapUser(from json: [String: Any]) -> User? {
guard let username = json["username"] as? String,
let email = json["email"] as? String,

@ -9,7 +9,7 @@ import Foundation
public protocol UserDataManager {
func getBets(withIndex index: Int, withCount count: Int) -> [Bet]
func addBet(bet: Bet)
func addBet(bet: Bet, completion : @escaping (Int)-> ())
func getFriends() -> [User]
func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int)

Loading…
Cancel
Save