You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
78 lines
2.5 KiB
//
|
|
// DataStore.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Laurent Morvillier on 02/02/2024.
|
|
//
|
|
|
|
import Foundation
|
|
import LeStorage
|
|
import SwiftUI
|
|
|
|
class DataStore: ObservableObject {
|
|
|
|
// fileprivate var _store: Store
|
|
static let shared = DataStore()
|
|
|
|
|
|
fileprivate(set) var tournaments: StoredCollection<Tournament>
|
|
fileprivate(set) var clubs: StoredCollection<Club>
|
|
|
|
fileprivate var _userStorage: OptionalStorage<User> = OptionalStorage<User>(fileName: "user.json")
|
|
|
|
var user: User? {
|
|
return self._userStorage.item
|
|
}
|
|
|
|
static let fakeTournaments: [Tournament] = [
|
|
Tournament(name: "P100", club_id: "", category: 0, playerCount: 16),
|
|
Tournament(name: "P250", club_id: "", category: 0, playerCount: 24),
|
|
Tournament(name: "P25", club_id: "", category: 0, playerCount: 4),
|
|
Tournament(name: "P1000", club_id: "", category: 0, playerCount: 8),
|
|
Tournament(name: "P500", club_id: "", category: 0, playerCount: 48),
|
|
]
|
|
|
|
|
|
func setUser(_ user: User?) {
|
|
self._userStorage.item = user
|
|
}
|
|
|
|
init() {
|
|
let store = Store.main
|
|
store.synchronizationApiURL = "http://127.0.0.1:8000/api/"
|
|
|
|
// store.addMigration(Migration<ClubV1, Club>(version: 2))
|
|
// store.addMigration(Migration<TournamentV1, TournamentV2>(version: 2))
|
|
// store.addMigration(Migration<TournamentV2, Tournament>(version: 3))
|
|
|
|
self.clubs = store.registerCollection(synchronized: false)
|
|
self.tournaments = store.registerCollection(synchronized: false)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(collectionWasUpdated), name: NSNotification.Name.CollectionDidLoad, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(collectionWasUpdated), name: NSNotification.Name.CollectionDidChange, object: nil)
|
|
}
|
|
|
|
@objc func collectionWasUpdated(notification: Notification) {
|
|
self.objectWillChange.send()
|
|
}
|
|
|
|
var globalRights: UserRight {
|
|
if let _ = Guard.main.currentPlan {
|
|
return .creation
|
|
}
|
|
if let user = self.user, user.club_id != nil {
|
|
if user.umpire_code != nil {
|
|
return .creation
|
|
} else {
|
|
return .edition
|
|
}
|
|
}
|
|
|
|
// TODO what are the rules when testing the app?
|
|
// scenario example: one cancelled tournament
|
|
|
|
|
|
return .none
|
|
}
|
|
|
|
}
|
|
|