Delete ManagerVM, re add the StubLib and improvement of the model 🎨

pull/16/head
Emre KARTAL 1 year ago
parent c381fdf0e7
commit 6eb3b2e8cd

@ -2,13 +2,13 @@
<Workspace
version = "1.0">
<FileRef
location = "group:Api">
location = "group:StubLib">
</FileRef>
<FileRef
location = "group:AllInApp/AllInApp.xcodeproj">
location = "group:Api">
</FileRef>
<FileRef
location = "group:ViewModel">
location = "group:AllInApp/AllInApp.xcodeproj">
</FileRef>
<FileRef
location = "group:DependencyInjection/DependencyInjection.xcodeproj">

@ -8,7 +8,6 @@
import SwiftUI
import DependencyInjection
import Model
import ViewModel
@main
struct AllInApp: App {

@ -67,44 +67,33 @@ struct BetCard: View {
RoundedRectangle(cornerRadius: 12).stroke(AllInColors.delimiterGrey, lineWidth: 1)
).padding([.top],5)
VStack(alignment: .leading,spacing: 2){
HStack{
Spacer()
UsersPreview()
Text(" 4 joueurs en attente").font(.system(size: 15)).foregroundColor(AllInColors.grey800Color).fontWeight(.medium)
Spacer()
}.padding(0)
ParticipateButton().padding(.top, 5)
}
.frame(width: .infinity)
.padding(.all,8)
.background(AllInColors.underComponentBackgroundColor)
.cornerRadius(20, corners: [.bottomLeft,.bottomRight])
.border(width: 1, edges: [.top], color: AllInColors.delimiterGrey)
}
}.onTapGesture {
.frame(width: .infinity)
.padding(.all,8)
.background(AllInColors.underComponentBackgroundColor)
.cornerRadius(20, corners: [.bottomLeft,.bottomRight])
.border(width: 1, edges: [.top], color: AllInColors.delimiterGrey)
}
.onTapGesture {
showDetails.toggle()
}.fullScreenCover(isPresented: $showDetails) {
}
.fullScreenCover(isPresented: $showDetails) {
DetailsView(isModalPresented: $showDetails, id: bet.id)
}
}
}
struct BetCard_Previews: PreviewProvider {
static var previews: some View {
BetCard(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),
totalStakes: 100,
isPublic: true,
invited: [],
author: User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75, friends: []),
registered: []))
.preferredColorScheme(.dark)
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: User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75, friends: []),
registered: []))
.preferredColorScheme(.dark)
}
}

@ -7,9 +7,9 @@
import Foundation
import Model
import ViewModel
import DependencyInjection
import Api
import StubLib
class AuthService: IAuthService {
@ -150,7 +150,7 @@ class AuthService: IAuthService {
}
private func initManagerVM(token: String) {
DependencyInjection.shared.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetApiManager(), withUserDataManager: UserApiManager(withUserToken: token))))
DependencyInjection.shared.addSingleton(Manager.self, Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserApiManager(withUserToken: token)))
}
}

@ -7,30 +7,23 @@
import Foundation
import DependencyInjection
import ViewModel
import Model
import Combine
class BetViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
@Published private var internalBets: [Bet] = []
var bets: [Bet] {
return internalBets
}
@Published private(set) var bets: [Bet] = []
init() {
getItems()
}
func getItems() {
for bet in manager.bets {
print(bet.theme)
manager.getBets(withIndex: 0, withCount: 20) { bets in
self.bets = bets
}
manager.$bets.assign(to: \.internalBets, on: self).store(in: &cancellables)
manager.getPublicBets()
}
private var cancellables: Set<AnyCancellable> = []

@ -8,11 +8,11 @@
import Foundation
import SwiftUI
import DependencyInjection
import ViewModel
import Model
class CreationBetViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
@Published var theme: String = ""
@Published var description: String = ""
@Published var isPublic = true

@ -7,20 +7,24 @@
import Foundation
import SwiftUI
import ViewModel
import DependencyInjection
import Model
class DetailsViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
var id: String
@Published var bet: BetDetail?
init(id: String) {
self.id = id
getItem()
getItem(withId: id)
}
func getItem() {
func getItem(withId id: String) {
manager.getBet(withId: id) { bet in
self.bet = bet
}
}
}

@ -7,11 +7,11 @@
import Foundation
import DependencyInjection
import ViewModel
import Model
class FriendsViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
init() {
getItems()

@ -7,12 +7,12 @@
import Foundation
import SwiftUI
import ViewModel
import DependencyInjection
import Model
class HistoricBetViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
init() {
getItems()

@ -7,11 +7,11 @@
import Foundation
import DependencyInjection
import ViewModel
import Model
class RankingViewModel: ObservableObject {
@Inject var manager: ManagerVM
@Inject var manager: Manager
init() {
getItems()

@ -43,14 +43,12 @@ struct DetailsView: View {
HStack{
Text("Commence le").frame(maxWidth: 100).font(.system(size: 15)).foregroundColor(AllInColors.grey800Color)
TextCapsule(date: Date())
TextCapsule(date: Date())
Spacer()
}.padding(.bottom, 10)
HStack{
Text("Fini le").frame(maxWidth: 100).font(.system(size: 15)).foregroundColor(AllInColors.grey800Color)
TextCapsule(date: Date())
TextCapsule(date: Date())
Spacer()
}

@ -24,7 +24,7 @@
EC30770D2B24DB7A0060E34D /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC30770C2B24DB7A0060E34D /* Extensions.swift */; };
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 */; };
EC60C5682B5A83FB00FFD6EF /* StubLib in Frameworks */ = {isa = PBXBuildFile; productRef = EC60C5672B5A83FB00FFD6EF /* 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 */; };
@ -102,7 +102,6 @@
/* Begin PBXFileReference section */
122278B72B4BDE1100E632AA /* DependencyInjection.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = DependencyInjection.framework; sourceTree = BUILT_PRODUCTS_DIR; };
122278B92B4BDE9500E632AA /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Package.swift; path = ../Model/Package.swift; sourceTree = "<group>"; };
122278BB2B4BDEC300E632AA /* Sources */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Sources; path = ../StubLib/Sources; sourceTree = "<group>"; };
123590B32B51792000F7AEBD /* DetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailsView.swift; sourceTree = "<group>"; };
123590B52B5537E200F7AEBD /* ResultBanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultBanner.swift; sourceTree = "<group>"; };
123590B72B5541BA00F7AEBD /* ParticipateButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParticipateButton.swift; sourceTree = "<group>"; };
@ -170,9 +169,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
EC60C5682B5A83FB00FFD6EF /* StubLib in Frameworks */,
EC4F0D2C2B4EBF5600853949 /* Model in Frameworks */,
ECCD244A2B4DE8010071FA9E /* Api in Frameworks */,
EC4F0D2E2B4EC04B00853949 /* ViewModel in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -334,7 +333,6 @@
ECB3572D2B3CA3BD00045D41 /* Frameworks */ = {
isa = PBXGroup;
children = (
122278BB2B4BDEC300E632AA /* Sources */,
122278B92B4BDE9500E632AA /* Package.swift */,
122278B72B4BDE1100E632AA /* DependencyInjection.framework */,
ECB357302B3CA69300045D41 /* DependencyInjection.framework */,
@ -378,7 +376,7 @@
packageProductDependencies = (
ECB357342B3E13A400045D41 /* Model */,
ECCD24492B4DE8010071FA9E /* Api */,
EC4F0D2D2B4EC04B00853949 /* ViewModel */,
EC60C5672B5A83FB00FFD6EF /* StubLib */,
);
productName = AllIn;
productReference = EC6B96982B24B4CC00FC1C58 /* AllIn.app */;
@ -874,9 +872,9 @@
/* End XCConfigurationList section */
/* Begin XCSwiftPackageProductDependency section */
EC4F0D2D2B4EC04B00853949 /* ViewModel */ = {
EC60C5672B5A83FB00FFD6EF /* StubLib */ = {
isa = XCSwiftPackageProductDependency;
productName = ViewModel;
productName = StubLib;
};
ECB357342B3E13A400045D41 /* Model */ = {
isa = XCSwiftPackageProductDependency;

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

@ -0,0 +1,43 @@
//
// BetAnswerDetail.swift
//
//
// Created by Emre on 19/01/2024.
//
import Foundation
/// A class representing detailed information about a specific answer option for a bet.
public class BetAnswerDetail: ObservableObject {
/// The response or outcome associated with this answer.
public private(set) var response: String
/// The total amount of stakes placed on this answer.
public private(set) var totalStakes: Int
/// The total number of participants who selected this answer.
public private(set) var totalParticipants: Int
/// The highest stake placed on this answer.
public private(set) var highestStake: Int
/// The odds associated with this answer.
public private(set) var odds: Float
/// Custom Constructor
///
/// - Parameters:
/// - response: The response or outcome associated with this answer.
/// - totalStakes: The total amount of stakes placed on this answer.
/// - totalParticipants: The total number of participants who selected this answer.
/// - highestStake: The highest stake placed on this answer.
/// - odds: The odds associated with this answer.
public init(response: String, totalStakes: Int, totalParticipants: Int, highestStake: Int, odds: Float) {
self.response = response
self.totalStakes = totalStakes
self.totalParticipants = totalParticipants
self.highestStake = highestStake
self.odds = odds
}
}

@ -10,5 +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)
func getBet(withId id: String, completion: @escaping (BetDetail) -> Void)
}

@ -0,0 +1,38 @@
//
// BetDetail.swift
//
//
// Created by Emre on 19/01/2024.
//
import Foundation
/// A class representing detailed information about a specific bet, including answers and user participations.
public class BetDetail: ObservableObject {
/// The main bet information.
public private(set) var bet: Bet
/// Details about the answers available for the bet.
public private(set) var answers: [BetAnswerDetail]
/// List of user participations in the bet.
public private(set) var participations: [Participation]
/// The user's own participation in the bet.
public private(set) var userParticipation: Participation
/// Custom Constructor
///
/// - Parameters:
/// - 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.
public init(bet: Bet, answers: [BetAnswerDetail], participations: [Participation], userParticipation: Participation) {
self.bet = bet
self.answers = answers
self.participations = participations
self.userParticipation = userParticipation
}
}

@ -1,33 +0,0 @@
//
// BinaryParticipation.swift
//
//
// Created by Emre on 28/12/2023.
//
import Foundation
/// Enum to represent the possible answers for a binary participation.
public enum YesNo {
case yes
case no
}
/// A subclass of Participation that represents a binary participation (yes/no) in a bet.
public class BinaryParticipation: Participation {
/// The answer for the binary participation (yes or no).
public var answer: YesNo
/// Custom Constructor
///
/// - Parameters:
/// - coinAmount: The amount of coins involved in the binary participation.
/// - date: The date and time when the binary participation occurred.
/// - user: The user who participated in the binary bet.
/// - bet: The binary bet in which the user participated.
/// - answer: The answer for the binary participation (yes or no).
public init(coinAmount: Int, date: Date, user: User, bet: Bet, answer: YesNo) {
self.answer = answer
super.init(coinAmount: coinAmount, date: date, user: user, bet: bet)
}
}

@ -1,27 +0,0 @@
//
// CustomParticipation.swift
//
//
// Created by Emre on 28/12/2023.
//
import Foundation
/// A subclass of Participation that represents a custom participation in a bet.
public class CustomParticipation: Participation {
/// The user's response to the custom bet.
public var answer: CustomBetResponse
/// Custom Constructor
///
/// - Parameters:
/// - coinAmount: The amount of coins involved in the custom participation.
/// - date: The date and time when the custom participation occurred.
/// - user: The user who participated in the custom bet.
/// - bet: The custom bet in which the user participated.
/// - answer: The user's response to the custom bet.
public init(coinAmount: Int, date: Date, user: User, bet: Bet, answer: CustomBetResponse) {
self.answer = answer
super.init(coinAmount: coinAmount, date: date, user: user, bet: bet)
}
}

@ -16,8 +16,8 @@ public struct Manager {
self.userDataManager = userDataManager
}
public func addBet(bet: Bet) {
userDataManager.addBet(bet: bet)
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, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: []))
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
@ -26,7 +26,7 @@ public struct Manager {
}
}
public func getBet(withId id: String, completion: @escaping (Bet) -> Void) {
public func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) {
betDataManager.getBet(withId: id) { bet in
completion(bet)
}

@ -1,32 +0,0 @@
//
// MatchParticipation.swift
//
//
// Created by Emre on 28/12/2023.
//
import Foundation
/// A subclass of Participation that represents a user's participation in a match bet.
public class MatchParticipation: Participation {
/// The points earned by the user for the first team in the match.
public var pointsTeam1: Int
/// The points earned by the user for the second team in the match.
public var pointsTeam2: Int
/// Custom Constructor
///
/// - Parameters:
/// - coinAmount: The amount of coins involved in the match participation.
/// - date: The date and time when the match participation occurred.
/// - user: The user who participated in the match bet.
/// - bet: The match bet in which the user participated.
/// - pointsTeam1: The points earned by the user for the first team in the match.
/// - pointsTeam2: The points earned by the user for the second team in the match.
public init(coinAmount: Int, date: Date, user: User, bet: Bet, pointsTeam1: Int, pointsTeam2: Int) {
self.pointsTeam1 = pointsTeam1
self.pointsTeam2 = pointsTeam2
super.init(coinAmount: coinAmount, date: date, user: user, bet: bet)
}
}

@ -7,31 +7,37 @@
import Foundation
/// A class representing a user's participation in a bet, including the amount of coins, date, user, and the associated bet.
/// A class representing a user's participation in a bet.
public class Participation: ObservableObject {
/// The amount of coins involved in the participation.
var coinAmount: Int
/// The amount of stake in the bet.
public private(set) var stake: Int
/// The date and time when the participation occurred.
var date: Date
public private(set) var date: Date
/// The response or outcome of the participation.
public private(set) var response: String
/// The user who participated in the bet.
var user: User
public private(set) var user: User
/// The bet in which the user participated.
var bet: Bet
/// The unique identifier of the bet.
let betId: String
/// Custom Constructor
///
/// - Parameters:
/// - coinAmount: The amount of coins involved in the participation.
/// - stake: The amount of stake in the bet.
/// - date: The date and time when the participation occurred.
/// - response: The response or outcome of the participation.
/// - user: The user who participated in the bet.
/// - bet: The bet in which the user participated.
init(coinAmount: Int, date: Date, user: User, bet: Bet) {
self.coinAmount = coinAmount
/// - betId: The unique identifier of the bet.
public init(stake: Int, date: Date, response: String, user: User, betId: String) {
self.stake = stake
self.date = date
self.response = response
self.user = user
self.bet = bet
self.betId = betId
}
}

@ -4,15 +4,15 @@
import PackageDescription
let package = Package(
name: "ViewModel",
name: "StubLib",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "ViewModel",
targets: ["ViewModel"]),
name: "StubLib",
targets: ["StubLib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
@ -23,7 +23,7 @@ let package = Package(
// 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: "ViewModel",
name: "StubLib",
dependencies: ["Model"]),
]
)

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

@ -0,0 +1,32 @@
//
// 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 []
}
public func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) {
if let betDetail = Stub.shared.betsDetail.first(where: { $0.bet.id == id }) {
completion(betDetail)
} else {
print("BetDetail with ID \(id) not found.")
}
}
}

@ -0,0 +1,97 @@
//
// 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 betsDetail: [BetDetail] = []
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)
for bet in bets {
let betDetail = BetDetail(bet: bet, answers: [], participations: [], userParticipation: Participation(stake: 0, date: Date(), response: "", user: user1, betId: ""))
self.betsDetail.append(betDetail)
}
}
public mutating func add(bet: Bet) {
let newBetDetail = BetDetail(bet: bet, answers: [], participations: [], userParticipation: Participation(stake: 0, date: Date(), response: "", user: users[1], betId: ""))
self.betsDetail.append(newBetDetail)
}
}

@ -1,43 +0,0 @@
//
// ManagerVM.swift
//
//
// Created by Emre on 30/12/2023.
//
import Foundation
import Model
public class ManagerVM: ObservableObject {
@Published var model: Manager
@Published public var bets: [Bet] = []
@Published public var bet: Bet?
public init(withModel model: Manager) {
self.model = model
}
public func getPublicBets() {
model.getBets(withIndex: 0, withCount: 20) { bets in
self.bets = bets
}
}
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