Correct AuthService

pull/16/head
Emre KARTAL 1 year ago
parent 816d83c4a1
commit c381fdf0e7

@ -7,9 +7,6 @@
<FileRef
location = "group:AllInApp/AllInApp.xcodeproj">
</FileRef>
<FileRef
location = "group:StubLib">
</FileRef>
<FileRef
location = "group:ViewModel">
</FileRef>

@ -88,7 +88,7 @@ struct BetCard: View {
}.onTapGesture {
showDetails.toggle()
}.fullScreenCover(isPresented: $showDetails) {
DetailsView(isModalPresented: $showDetails)
DetailsView(isModalPresented: $showDetails, id: bet.id)
}
}

@ -107,7 +107,7 @@ struct RecapBetCard: View {
.onTapGesture {
showDetails.toggle()
}.fullScreenCover(isPresented: $showDetails) {
DetailsView(isModalPresented: $showDetails)
DetailsView(isModalPresented: $showDetails, id: "1")
}
.gesture(
LongPressGesture(minimumDuration: 0.5)

@ -70,7 +70,7 @@ struct ReviewCard: View {
.onTapGesture {
showDetails.toggle()
}.fullScreenCover(isPresented: $showDetails) {
DetailsView(isModalPresented: $showDetails)
DetailsView(isModalPresented: $showDetails, id: "1")
}
}
}

@ -8,5 +8,5 @@
import Foundation
struct Config {
static let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api"
static let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api/"
}

@ -10,46 +10,49 @@ import Model
import ViewModel
import DependencyInjection
import Api
import StubLib
class AuthService: IAuthService {
public func login(login: String, password: String, completion : @escaping (Int)-> ()) {
public func login(login: String, password: String, completion: @escaping (Int) -> ()) {
let url = URL(string: Config.allInApi + "users/login")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let json = [
"login": login.lowercased(),
"password": password,
]
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []) {
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
print ("ALLIN : Process LOGIN")
print("ALLIN: Process LOGIN")
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
if let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let token = json["token"] as? String {
AppStateContainer.shared.authenticationRefresh = token;
AppStateContainer.shared.authenticationRefresh = token
self.initializeUser(withToken: token) { status in
print(status)
if status != 200 {
completion(status)
AppStateContainer.shared.authenticationRefresh = nil;
AppStateContainer.shared.authenticationRefresh = nil
} else {
self.initManagerVM(token: token)
completion(httpResponse.statusCode)
}
}
}
} else {
completion(httpResponse.statusCode)
}
completion(httpResponse.statusCode)
}
}.resume()
}
}
func register(username: String, email: String, password: String, completion : @escaping (Int)-> ()) {
let url = URL(string: Config.allInApi + "/users/register")!
@ -78,11 +81,13 @@ class AuthService: IAuthService {
AppStateContainer.shared.authenticationRefresh = nil;
} else {
self.initManagerVM(token: token)
completion(httpResponse.statusCode)
}
}
}
} else {
completion(httpResponse.statusCode)
}
completion(httpResponse.statusCode)
}
}.resume()
}
@ -118,12 +123,14 @@ class AuthService: IAuthService {
}
private func initializeUser(withToken token: String, completion: @escaping (Int) -> ()) {
let url = URL(string: Config.allInApi + "users/token")!
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,
let httpResponse = response as? HTTPURLResponse {

@ -0,0 +1,26 @@
//
// DetailsViewModel.swift
// AllIn
//
// Created by Emre on 16/01/2024.
//
import Foundation
import SwiftUI
import ViewModel
import DependencyInjection
class DetailsViewModel: ObservableObject {
@Inject var manager: ManagerVM
var id: String
init(id: String) {
self.id = id
getItem()
}
func getItem() {
}
}

@ -0,0 +1,24 @@
//
// HistoricBetViewModel.swift
// AllIn
//
// Created by Emre on 16/01/2024.
//
import Foundation
import SwiftUI
import ViewModel
import DependencyInjection
class HistoricBetViewModel: ObservableObject {
@Inject var manager: ManagerVM
init() {
getItems()
}
func getItems() {
}
}

@ -54,6 +54,9 @@ struct BetView: View {
}
}
}
.refreshable {
viewModel.getItems()
}
.sheet(isPresented: $showingSheet) {
WinModal()
}

@ -1,7 +1,17 @@
import SwiftUI
struct DetailsView: View {
@Binding var isModalPresented: Bool
var id: String
@StateObject private var viewModel: DetailsViewModel
init(isModalPresented: Binding<Bool>, id: String) {
self._isModalPresented = isModalPresented
self.id = id
self._viewModel = StateObject(wrappedValue: DetailsViewModel(id: id))
}
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .bottom) {

@ -16,6 +16,8 @@
EC01937A2B25C12B005D81E6 /* BetCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC0193792B25C12B005D81E6 /* BetCard.swift */; };
EC01937C2B25C2A8005D81E6 /* AllcoinsCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC01937B2B25C2A8005D81E6 /* AllcoinsCounter.swift */; };
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 */; };
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 */; };
@ -23,7 +25,6 @@
EC30770F2B24FCB00060E34D /* RegisterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770E2B24FCB00060E34D /* RegisterView.swift */; };
EC4F0D2C2B4EBF5600853949 /* Model in Frameworks */ = {isa = PBXBuildFile; productRef = ECB357342B3E13A400045D41 /* Model */; };
EC4F0D2E2B4EC04B00853949 /* ViewModel in Frameworks */ = {isa = PBXBuildFile; productRef = EC4F0D2D2B4EC04B00853949 /* ViewModel */; };
EC4F0D302B4EC05D00853949 /* StubLib in Frameworks */ = {isa = PBXBuildFile; productRef = EC4F0D2F2B4EC05D00853949 /* StubLib */; };
EC650A422B25C817003AFCAD /* Friend.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC650A412B25C817003AFCAD /* Friend.swift */; };
EC650A442B25CDF3003AFCAD /* ParameterMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC650A432B25CDF3003AFCAD /* ParameterMenu.swift */; };
EC650A462B25D686003AFCAD /* RankingRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC650A452B25D686003AFCAD /* RankingRow.swift */; };
@ -111,6 +112,8 @@
EC0193792B25C12B005D81E6 /* BetCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BetCard.swift; sourceTree = "<group>"; };
EC01937B2B25C2A8005D81E6 /* AllcoinsCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllcoinsCounter.swift; sourceTree = "<group>"; };
EC01937D2B25C52E005D81E6 /* TopBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopBar.swift; sourceTree = "<group>"; };
EC01FCC22B56650400BB2390 /* DetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailsViewModel.swift; sourceTree = "<group>"; };
EC01FCC42B56791B00BB2390 /* HistoricBetViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricBetViewModel.swift; sourceTree = "<group>"; };
EC3077062B24CB840060E34D /* SplashView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashView.swift; sourceTree = "<group>"; };
EC3077082B24CF7F0060E34D /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
EC30770A2B24D9160060E34D /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = "<group>"; };
@ -167,7 +170,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
EC4F0D302B4EC05D00853949 /* StubLib in Frameworks */,
EC4F0D2C2B4EBF5600853949 /* Model in Frameworks */,
ECCD244A2B4DE8010071FA9E /* Api in Frameworks */,
EC4F0D2E2B4EC04B00853949 /* ViewModel in Frameworks */,
@ -350,6 +352,8 @@
ECB26A162B4073F100FE06B3 /* CreationBetViewModel.swift */,
ECB26A182B40744F00FE06B3 /* RankingViewModel.swift */,
ECB26A1A2B40746C00FE06B3 /* FriendsViewModel.swift */,
EC01FCC22B56650400BB2390 /* DetailsViewModel.swift */,
EC01FCC42B56791B00BB2390 /* HistoricBetViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
@ -375,7 +379,6 @@
ECB357342B3E13A400045D41 /* Model */,
ECCD24492B4DE8010071FA9E /* Api */,
EC4F0D2D2B4EC04B00853949 /* ViewModel */,
EC4F0D2F2B4EC05D00853949 /* StubLib */,
);
productName = AllIn;
productReference = EC6B96982B24B4CC00FC1C58 /* AllIn.app */;
@ -529,6 +532,7 @@
EC01937E2B25C52E005D81E6 /* TopBar.swift in Sources */,
ECA9D1CB2B2DA2320076E0EC /* DropDownFriends.swift in Sources */,
123590B82B5541BA00F7AEBD /* ParticipateButton.swift in Sources */,
EC01FCC32B56650400BB2390 /* DetailsViewModel.swift in Sources */,
ECB26A1B2B40746C00FE06B3 /* FriendsViewModel.swift in Sources */,
ECB7BC682B2F1ADF002A6654 /* LoginViewModel.swift in Sources */,
1244EF622B4EC67000374ABF /* ReviewCard.swift in Sources */,
@ -536,6 +540,7 @@
EC650A562B279D68003AFCAD /* WinModal.swift in Sources */,
EC6B96D12B24BAE800FC1C58 /* AuthService.swift in Sources */,
123590B62B5537E200F7AEBD /* ResultBanner.swift in Sources */,
EC01FCC52B56791B00BB2390 /* HistoricBetViewModel.swift in Sources */,
EC01937C2B25C2A8005D81E6 /* AllcoinsCounter.swift in Sources */,
EC650A542B279545003AFCAD /* ChoiceCapsule.swift in Sources */,
ECB7BC6C2B2F43EE002A6654 /* AppState.swift in Sources */,
@ -873,10 +878,6 @@
isa = XCSwiftPackageProductDependency;
productName = ViewModel;
};
EC4F0D2F2B4EC05D00853949 /* StubLib */ = {
isa = XCSwiftPackageProductDependency;
productName = StubLib;
};
ECB357342B3E13A400045D41 /* Model */ = {
isa = XCSwiftPackageProductDependency;
productName = Model;

@ -46,4 +46,8 @@ public struct BetApiManager: BetDataManager {
return []
}
public func getBet(withId id: String, completion: @escaping (Bet) -> Void) {
}
}

@ -11,13 +11,13 @@ import Model
public class FactoryApiBet: FactoryBet {
public func toResponse(bet: Bet) -> [String: Any] {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let json: [String: Any] = [
"id": "1",
"theme": bet.theme,
"sentenceBet": bet.phrase,
"endRegistration": dateFormatter.string(from: bet.endRegisterDate),
"endBet": dateFormatter.string(from: bet.endBetDate),
"endRegistration": bet.endRegisterDate.timeIntervalSince1970,
"endBet": bet.endBetDate.timeIntervalSince1970,
"isPrivate": String(bet.isPublic),
"response": [],
]
@ -26,7 +26,8 @@ public class FactoryApiBet: FactoryBet {
}
public func toModel(from json: [String: Any]) -> Bet? {
guard let theme = json["theme"] as? String,
guard let id = json["id"] as? String,
let theme = json["theme"] as? String,
let phrase = json["sentenceBet"] as? String,
let endRegisterDateString = json["endRegistration"] as? String,
let endBetDateString = json["endBet"] as? String,
@ -43,19 +44,19 @@ public class FactoryApiBet: FactoryBet {
return nil
}
return toModel(theme: theme, description: phrase, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: User(username: createdBy, email: createdBy, nbCoins: 0, friends: []), type: 0)
return toModel(id: id, theme: theme, description: phrase, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: User(username: createdBy, email: createdBy, nbCoins: 0, friends: []), type: 0)
}
public func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet {
public func toModel(id: String, theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet {
switch type {
case 0:
return BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
case 1:
return MatchBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [], nameTeam1: "", nameTeam2: "")
return MatchBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [], nameTeam1: "", nameTeam2: "")
case 2:
return CustomBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return CustomBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
default:
return BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
}
}

@ -8,7 +8,7 @@
import Foundation
import Model
let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api"
let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api/"
public struct UserApiManager: UserDataManager {
@ -37,9 +37,6 @@ public struct UserApiManager: UserDataManager {
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
print ("ALLIN : Add BET")
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 201 {
}
print(httpResponse.statusCode)
}
}.resume()
@ -49,4 +46,8 @@ public struct UserApiManager: UserDataManager {
public func getFriends() -> [User] {
fatalError("Not implemented yet")
}
public func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
fatalError("Not implemented yet")
}
}

@ -11,7 +11,7 @@ import Foundation
public class Bet: ObservableObject, Identifiable {
/// The id for the bet.
public var id = UUID()
public private(set) var id: String
/// The theme or topic of the bet.
public private(set) var theme: String
@ -43,6 +43,32 @@ public class Bet: ObservableObject, Identifiable {
/// Custom Constructor
///
/// - Parameters:
/// - id: The id for the bet.
/// - theme: The theme or topic of the bet.
/// - phrase: The specific phrase or question related to the bet.
/// - endRegisterDate: The deadline for users to register for the bet.
/// - endBetDate: The deadline for the actual betting to take place.
/// - totalStakes: The total stakes or amount involved in the bet.
/// - isPublic: Indicates whether the bet is public or private.
/// - 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.
public init(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) {
self.id = id
self.theme = theme
self.phrase = phrase
self.endRegisterDate = endRegisterDate
self.endBetDate = endBetDate
self.totalStakes = totalStakes
self.isPublic = isPublic
self.invited = invited
self.author = author
self.registered = registered
}
/// Custom Constructor without Id
///
/// - Parameters:
/// - theme: The theme or topic of the bet.
/// - phrase: The specific phrase or question related to the bet.
/// - endRegisterDate: The deadline for users to register for the bet.
@ -53,6 +79,7 @@ public class Bet: ObservableObject, Identifiable {
/// - author: The user who created the bet.
/// - registered: List of users who have registered for the bet.
public init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) {
self.id = UUID().uuidString
self.theme = theme
self.phrase = phrase
self.endRegisterDate = endRegisterDate

@ -10,4 +10,5 @@ import Foundation
public protocol BetDataManager {
func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
func getUsers(username: String) -> [User]
func getBet(withId id: String, completion: @escaping (Bet) -> Void)
}

@ -13,6 +13,23 @@ public class BinaryBet: Bet {
// Custom Constructor
///
/// - Parameters:
/// - id: The id for the bet.
/// - theme: The theme or topic of the binary bet.
/// - phrase: The specific phrase or question related to the binary bet.
/// - endRegisterDate: The deadline for users to register for the binary bet.
/// - endBetDate: The deadline for the actual betting to take place for the binary bet.
/// - totalStakes: The total stakes or amount involved in the binary bet.
/// - isPublic: Indicates whether the binary bet is public or private.
/// - invited: List of users who are invited to participate in the binary bet.
/// - author: The user who created the binary bet.
/// - registered: List of users who have registered for the binary bet.
public override init(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) {
super.init(id: id, theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered)
}
// Custom Constructor without Id
///
/// - Parameters:
/// - theme: The theme or topic of the binary bet.
/// - phrase: The specific phrase or question related to the binary bet.
/// - endRegisterDate: The deadline for users to register for the binary bet.

@ -10,5 +10,5 @@ import Foundation
public protocol FactoryBet {
func toResponse(bet: Bet) -> [String: Any]
func toModel(from json: [String: Any]) -> Bet?
func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet
func toModel(id: String, theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet
}

@ -25,4 +25,16 @@ public struct Manager {
completion(bets)
}
}
public func getBet(withId id: String, completion: @escaping (Bet) -> Void) {
betDataManager.getBet(withId: id) { bet in
completion(bet)
}
}
public func getHistoricBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
userDataManager.getOldBets(withIndex: index, withCount: count) { bets in
completion(bets)
}
}
}

@ -18,6 +18,27 @@ public class MatchBet: Bet {
/// Custom Constructor
///
/// - Parameters:
/// - id: The id for the bet.
/// - theme: The theme or topic of the match 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.
/// - 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.
/// - nameTeam1: The name of the first team involved in the match.
/// - nameTeam2: The name of the second team involved in the match.
public init(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User], nameTeam1: String, nameTeam2: String) {
self.nameTeam1 = nameTeam1
self.nameTeam2 = nameTeam2
super.init(id: id, theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered)
}
/// Custom Constructor without Id
///
/// - Parameters:
/// - theme: The theme or topic of the match bet.
/// - phrase: The specific phrase or question related to the match bet.
/// - endRegisterDate: The deadline for users to register for the match bet.

@ -11,4 +11,5 @@ public protocol UserDataManager {
func getBets(withIndex index: Int, withCount count: Int) -> [Bet]
func addBet(bet: Bet)
func getFriends() -> [User]
func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
}

@ -1,29 +0,0 @@
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "StubLib",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "StubLib",
targets: ["StubLib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(name: "Model", path: "../Model")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "StubLib",
dependencies: ["Model"]),
]
)

@ -1,3 +0,0 @@
# StubLib
A description of this package.

@ -1,26 +0,0 @@
//
// BetStubManager.swift
//
//
// Created by Emre on 31/12/2023.
//
import Foundation
import Model
public struct BetStubManager: BetDataManager {
public init() {}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
completion(Stub.shared.bets)
}
public func getUsers(username: String) -> [User] {
return Stub.shared.users
.filter { user in
user.username.contains(username)
}
}
}

@ -1,91 +0,0 @@
//
// Stub.swift
//
//
// Created by Emre on 01/01/2024.
//
import Foundation
import Model
struct Stub {
static var shared = Stub()
public var bets: [Bet] = []
public var users: [User] = []
public init() {
loadBets()
}
public mutating func loadBets() {
var user1 = User(username: "Lucas", email: "lucas.delanier@etu.uca.fr", nbCoins: 100, friends: [])
users.append(user1)
var user2 = User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75, friends: [user1])
users.append(user2)
user1.addFriend(user: user2)
let user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, friends: [user2])
users.append(user3)
user2.addFriend(user: user3)
let bet1 = 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),
totalStakes: 100,
isPublic: true,
invited: [],
author: user1,
registered: [user2]
)
self.bets.append(bet1)
let bet2 = BinaryBet(
theme: "Cuisine - Concours de cuisine en direct",
phrase: "Le plat préféré du jury sera une recette végétarienne.",
endRegisterDate: Date().addingTimeInterval(172800),
endBetDate: Date().addingTimeInterval(259200),
totalStakes: 150,
isPublic: false,
invited: [user3],
author: user1,
registered: [user2]
)
self.bets.append(bet2)
let bet3 = BinaryBet(
theme: "Technologie - Lancement d'un nouveau smartphone",
phrase: "Le nombre total de précommandes dépassera-t-il 1 million dans la première semaine ?",
endRegisterDate: Date().addingTimeInterval(259200),
endBetDate: Date().addingTimeInterval(345600),
totalStakes: 75,
isPublic: true,
invited: [],
author: user1,
registered: [user2, user1, user3]
)
self.bets.append(bet3)
let bet4 = BinaryBet(
theme: "Cinéma - Oscars 2024",
phrase: "Le film favori des critiques remportera-t-il le prix du meilleur film ?",
endRegisterDate: Date().addingTimeInterval(345600),
endBetDate: Date().addingTimeInterval(432000),
totalStakes: 120,
isPublic: false,
invited: [user1],
author: user2,
registered: [user3]
)
self.bets.append(bet4)
}
public mutating func add(bet: Bet) {
self.bets.append(bet)
}
}

@ -1,38 +0,0 @@
//
// UserStubManager.swift
//
//
// Created by Emre on 31/12/2023.
//
import Foundation
import Model
public struct UserStubManager: UserDataManager {
private var username: String
public init(username: String) {
self.username = username
}
public func getBets(withIndex index: Int, withCount count: Int) -> [Bet] {
return Stub.shared.bets.filter { bet in
bet.registered.contains { user in
user.username == self.username
}
}
}
public func addBet(bet: Bet) {
Stub.shared.add(bet: bet)
}
public func getFriends() -> [User] {
return Stub.shared.users.filter { user in
user.friends.contains { friend in
friend.username == self.username
}
}
}
}

@ -9,9 +9,10 @@ import Foundation
import Model
public class ManagerVM: ObservableObject {
@Published var model: Manager
@Published var model: Manager
@Published public var bets: [Bet] = []
@Published public var bet: Bet?
public init(withModel model: Manager) {
self.model = model
@ -26,4 +27,17 @@ public class ManagerVM: ObservableObject {
public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User) {
model.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: []))
}
public func getBet(withId id: String) {
model.getBet(withId: id) { bet in
self.bet = bet
}
}
public func getHistoricBets() {
model.getHistoricBets(withIndex: 0, withCount: 20) { bets in
self.bets = bets
}
}
}

Loading…
Cancel
Save