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.
 
 
PadelClub/PadelClub/Data/Club.swift

96 lines
2.5 KiB

//
// Club.swift
// PadelClub
//
// Created by Laurent Morvillier on 02/02/2024.
//
import Foundation
import SwiftUI
import LeStorage
@Observable
class Club : ModelObject, Storable, Hashable {
static func resourceName() -> String { return "clubs" }
static func == (lhs: Club, rhs: Club) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
return hasher.combine(id)
}
var id: String = Store.randomId()
var name: String
var acronym: String
var phone: String?
var code: String?
var address: String?
var city: String?
var zipCode: String?
var latitude: Double?
var longitude: Double?
internal init(name: String, acronym: String? = nil, phone: String? = nil, code: String? = nil, address: String? = nil, city: String? = nil, zipCode: String? = nil, latitude: Double? = nil, longitude: Double? = nil) {
self.name = name
self.acronym = acronym ?? name.canonicalVersion.replaceCharactersFromSet(characterSet: .whitespacesAndNewlines)
self.phone = phone
self.code = code
self.address = address
self.city = city
self.zipCode = zipCode
self.latitude = latitude
self.longitude = longitude
}
var tournaments: [Tournament] {
return Store.main.filter { $0.club_id == self.id }
}
override func deleteDependencies() throws {
try Store.main.deleteDependencies(items: self.tournaments)
}
enum CodingKeys: String, CodingKey {
case _id = "id"
case _name = "name"
case _acronym = "acronym"
case _phone = "phone"
case _code = "code"
case _address = "address"
case _city = "city"
case _zipCode = "zipCode"
case _latitude = "latitude"
case _longitude = "longitude"
}
}
extension Club {
var isValid: Bool {
name.isEmpty == false && acronym.isEmpty == false
}
func automaticShortName() -> String {
name.canonicalVersion.replaceCharactersFromSet(characterSet: .whitespacesAndNewlines)
}
enum AcronymMode: String, CaseIterable {
case automatic = "Automatique"
case custom = "Personalisée"
}
func shortNameMode() -> AcronymMode {
(acronym.isEmpty || acronym == automaticShortName()) ? .automatic : .custom
}
func hasTenupId() -> Bool {
code != nil
}
func federalLink() -> URL? {
guard let code else { return nil }
return URL(string: "https://tenup.fft.fr/club/\(code)")
}
}