update to use appSettings stuff in user

multistore
Razmig Sarkissian 2 years ago
parent c4a6d37446
commit 8462be0de7
  1. 19
      PadelClub/Data/DataStore.swift
  2. 29
      PadelClub/Data/User.swift
  3. 14
      PadelClub/Utils/ContactManager.swift
  4. 8
      PadelClub/Utils/PadelRule.swift
  5. 38
      PadelClub/Views/Calling/CallMessageCustomizationView.swift
  6. 6
      PadelClub/Views/Navigation/MainView.swift
  7. 21
      PadelClub/Views/Navigation/Toolbox/GlobalSettingsView.swift
  8. 9
      PadelClub/Views/Navigation/Toolbox/MatchFormatStorageView.swift
  9. 8
      PadelClub/Views/Navigation/Toolbox/PadelClubView.swift
  10. 2
      PadelClub/Views/Tournament/Screen/Components/TournamentMatchFormatsSettingsView.swift

@ -46,25 +46,10 @@ class DataStore: ObservableObject {
fileprivate(set) var userStorage: StoredSingleton<User> fileprivate(set) var userStorage: StoredSingleton<User>
// fileprivate var _userStorage: OptionalStorage<User> = OptionalStorage<User>(fileName: "user.json") // fileprivate var _userStorage: OptionalStorage<User> = OptionalStorage<User>(fileName: "user.json")
fileprivate var _appSettingsStorage: MicroStorage<AppSettings> = MicroStorage() fileprivate(set) var appSettingsStorage: MicroStorage<AppSettings> = MicroStorage()
var appSettings: AppSettings { var appSettings: AppSettings {
_appSettingsStorage.item appSettingsStorage.item
}
func updateSettings() {
_appSettingsStorage.update { settings in
settings.lastDataSource = appSettings.lastDataSource
settings.callMessageBody = appSettings.callMessageBody
settings.callDisplayFormat = appSettings.callDisplayFormat
settings.callMessageSignature = appSettings.callMessageSignature
settings.callDisplayEntryFee = appSettings.callDisplayEntryFee
settings.callUseFullCustomMessage = appSettings.callUseFullCustomMessage
settings.matchFormatsDefaultDuration = appSettings.matchFormatsDefaultDuration
settings.bracketMatchFormatPreference = appSettings.bracketMatchFormatPreference
settings.groupStageMatchFormatPreference = appSettings.groupStageMatchFormatPreference
settings.loserBracketMatchFormatPreference = appSettings.loserBracketMatchFormatPreference
}
} }
init() { init() {

@ -32,6 +32,16 @@ class User: UserBase, Storable {
var phone: String? var phone: String?
var country: String? var country: String?
var callMessageBody : String? = nil
var callMessageSignature: String? = nil
var callDisplayFormat: Bool = false
var callDisplayEntryFee: Bool = false
var callUseFullCustomMessage: Bool = false
var matchFormatsDefaultDuration: [MatchFormat: Int]? = nil
var bracketMatchFormatPreference: MatchFormat?
var groupStageMatchFormatPreference: MatchFormat?
var loserBracketMatchFormatPreference: MatchFormat?
init(username: String, email: String, firstName: String, lastName: String, phone: String?, country: String?) { init(username: String, email: String, firstName: String, lastName: String, phone: String?, country: String?) {
self.username = username self.username = username
self.firstName = firstName self.firstName = firstName
@ -73,6 +83,15 @@ class User: UserBase, Storable {
return Store.main.filter(isIncluded: { (includeCreated && $0.creator == id) || clubs.contains($0.id) }) return Store.main.filter(isIncluded: { (includeCreated && $0.creator == id) || clubs.contains($0.id) })
} }
func saveMatchFormatsDefaultDuration(_ matchFormat: MatchFormat, estimatedDuration: Int) {
if estimatedDuration == matchFormat.defaultEstimatedDuration {
matchFormatsDefaultDuration?.removeValue(forKey: matchFormat)
} else {
matchFormatsDefaultDuration = matchFormatsDefaultDuration ?? [MatchFormat: Int]()
matchFormatsDefaultDuration?[matchFormat] = estimatedDuration
}
}
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case _id = "id" case _id = "id"
case _username = "username" case _username = "username"
@ -84,6 +103,16 @@ class User: UserBase, Storable {
case _lastName = "lastName" case _lastName = "lastName"
case _phone = "phone" case _phone = "phone"
case _country = "country" case _country = "country"
case _callMessageBody = "callMessageBody"
case _callMessageSignature = "callMessageSignature"
case _callDisplayFormat = "callDisplayFormat"
case _callDisplayEntryFee = "callDisplayEntryFee"
case _callUseFullCustomMessage = "callUseFullCustomMessage"
case _matchFormatsDefaultDuration = "matchFormatsDefaultDuration"
case _bracketMatchFormatPreference = "bracketMatchFormatPreference"
case _groupStageMatchFormatPreference = "groupStageMatchFormatPreference"
case _loserBracketMatchFormatPreference = "loserBracketMatchFormatPreference"
} }
static func placeHolder() -> User { static func placeHolder() -> User {

@ -34,7 +34,7 @@ extension ContactType {
static let defaultSignature = "" static let defaultSignature = ""
static func callingGroupStageCustomMessage(tournament: Tournament?, startDate: Date?, roundLabel: String) -> String { static func callingGroupStageCustomMessage(tournament: Tournament?, startDate: Date?, roundLabel: String) -> String {
let tournamentCustomMessage = DataStore.shared.appSettings.callMessageBody ?? defaultCustomMessage let tournamentCustomMessage = DataStore.shared.user.callMessageBody ?? defaultCustomMessage
let clubName = tournament?.clubName ?? "" let clubName = tournament?.clubName ?? ""
var text = tournamentCustomMessage var text = tournamentCustomMessage
@ -49,7 +49,7 @@ extension ContactType {
text = text.replacingOccurrences(of: "#jour", with: "\(date.formatted(Date.FormatStyle().weekday(.wide).day().month(.wide)))") text = text.replacingOccurrences(of: "#jour", with: "\(date.formatted(Date.FormatStyle().weekday(.wide).day().month(.wide)))")
text = text.replacingOccurrences(of: "#horaire", with: "\(date.formatted(Date.FormatStyle().hour().minute()))") text = text.replacingOccurrences(of: "#horaire", with: "\(date.formatted(Date.FormatStyle().hour().minute()))")
let signature = DataStore.shared.appSettings.callMessageSignature ?? defaultSignature let signature = DataStore.shared.user.callMessageSignature ?? defaultSignature
text = text.replacingOccurrences(of: "#signature", with: signature) text = text.replacingOccurrences(of: "#signature", with: signature)
return text return text
@ -57,7 +57,7 @@ extension ContactType {
static func callingGroupStageMessage(tournament: Tournament?, startDate: Date?, roundLabel: String, matchFormat: MatchFormat?) -> String { static func callingGroupStageMessage(tournament: Tournament?, startDate: Date?, roundLabel: String, matchFormat: MatchFormat?) -> String {
let useFullCustomMessage = DataStore.shared.appSettings.callUseFullCustomMessage let useFullCustomMessage = DataStore.shared.user.callUseFullCustomMessage
if useFullCustomMessage { if useFullCustomMessage {
return callingGroupStageCustomMessage(tournament: tournament, startDate: startDate, roundLabel: roundLabel) return callingGroupStageCustomMessage(tournament: tournament, startDate: startDate, roundLabel: roundLabel)
@ -66,17 +66,17 @@ extension ContactType {
let date = startDate ?? tournament?.startDate ?? Date() let date = startDate ?? tournament?.startDate ?? Date()
let clubName = tournament?.clubName ?? "" let clubName = tournament?.clubName ?? ""
let message = DataStore.shared.appSettings.callMessageBody ?? defaultCustomMessage let message = DataStore.shared.user.callMessageBody ?? defaultCustomMessage
let signature = DataStore.shared.appSettings.callMessageSignature ?? defaultSignature let signature = DataStore.shared.user.callMessageSignature ?? defaultSignature
let localizedCalled = "convoqué" + (tournament?.tournamentCategory == .women ? "e" : "") + "s" let localizedCalled = "convoqué" + (tournament?.tournamentCategory == .women ? "e" : "") + "s"
var formatMessage: String? { var formatMessage: String? {
(DataStore.shared.appSettings.callDisplayFormat) ? matchFormat?.computedLongLabel.appending(".") : nil (DataStore.shared.user.callDisplayFormat) ? matchFormat?.computedLongLabel.appending(".") : nil
} }
var entryFeeMessage: String? { var entryFeeMessage: String? {
(DataStore.shared.appSettings.callDisplayEntryFee) ? tournament?.entryFeeMessage : nil (DataStore.shared.user.callDisplayEntryFee) ? tournament?.entryFeeMessage : nil
} }
var computedMessage: String { var computedMessage: String {

@ -1075,11 +1075,11 @@ enum MatchFormat: Int, Hashable, Codable, CaseIterable {
static func defaultFormatForMatchType(_ matchType: MatchType) -> MatchFormat { static func defaultFormatForMatchType(_ matchType: MatchType) -> MatchFormat {
switch matchType { switch matchType {
case .bracket: case .bracket:
DataStore.shared.appSettings.bracketMatchFormatPreference ?? .nineGamesDecisivePoint DataStore.shared.user.bracketMatchFormatPreference ?? .nineGamesDecisivePoint
case .groupStage: case .groupStage:
DataStore.shared.appSettings.groupStageMatchFormatPreference ?? .nineGamesDecisivePoint DataStore.shared.user.groupStageMatchFormatPreference ?? .nineGamesDecisivePoint
case .loserBracket: case .loserBracket:
DataStore.shared.appSettings.loserBracketMatchFormatPreference ?? .nineGamesDecisivePoint DataStore.shared.user.loserBracketMatchFormatPreference ?? .nineGamesDecisivePoint
} }
} }
@ -1109,7 +1109,7 @@ enum MatchFormat: Int, Hashable, Codable, CaseIterable {
} }
private var estimatedDuration: Int { private var estimatedDuration: Int {
DataStore.shared.appSettings.matchFormatsDefaultDuration?[self] ?? defaultEstimatedDuration DataStore.shared.user.matchFormatsDefaultDuration?[self] ?? defaultEstimatedDuration
} }
func formattedEstimatedDuration(_ additionalDuration: Int = 0) -> String { func formattedEstimatedDuration(_ additionalDuration: Int = 0) -> String {

@ -19,8 +19,8 @@ struct CallMessageCustomizationView: View {
init(tournament: Tournament) { init(tournament: Tournament) {
self.tournament = tournament self.tournament = tournament
_customCallMessageBody = State(wrappedValue: DataStore.shared.appSettings.callMessageBody ?? "") _customCallMessageBody = State(wrappedValue: DataStore.shared.user.callMessageBody ?? "")
_customCallMessageSignature = State(wrappedValue: DataStore.shared.appSettings.callMessageSignature ?? "") _customCallMessageSignature = State(wrappedValue: DataStore.shared.user.callMessageSignature ?? "")
_customClubName = State(wrappedValue: tournament.clubName ?? "") _customClubName = State(wrappedValue: tournament.clubName ?? "")
} }
@ -29,11 +29,11 @@ struct CallMessageCustomizationView: View {
} }
var formatMessage: String? { var formatMessage: String? {
dataStore.appSettings.callDisplayFormat ? tournament.matchFormat.computedLongLabel + "." : nil dataStore.user.callDisplayFormat ? tournament.matchFormat.computedLongLabel + "." : nil
} }
var entryFeeMessage: String? { var entryFeeMessage: String? {
dataStore.appSettings.callDisplayEntryFee ? tournament.entryFeeMessage : nil dataStore.user.callDisplayEntryFee ? tournament.entryFeeMessage : nil
} }
var computedMessage: String { var computedMessage: String {
@ -46,7 +46,7 @@ struct CallMessageCustomizationView: View {
} }
var body: some View { var body: some View {
@Bindable var appSettings = dataStore.appSettings @Bindable var user = dataStore.user
List { List {
Section { Section {
ZStack { ZStack {
@ -74,7 +74,7 @@ struct CallMessageCustomizationView: View {
_clubNameView() _clubNameView()
Section { Section {
if appSettings.callUseFullCustomMessage { if user.callUseFullCustomMessage {
Text(self.computedFullCustomMessage()) Text(self.computedFullCustomMessage())
.contextMenu { .contextMenu {
Button("Coller dans le presse-papier") { Button("Coller dans le presse-papier") {
@ -96,7 +96,7 @@ struct CallMessageCustomizationView: View {
Section { Section {
LabeledContent { LabeledContent {
Toggle(isOn: $appSettings.callUseFullCustomMessage) { Toggle(isOn: $user.callUseFullCustomMessage) {
} }
} label: { } label: {
@ -112,13 +112,13 @@ struct CallMessageCustomizationView: View {
.toolbar { .toolbar {
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
Menu { Menu {
Picker(selection: $appSettings.callDisplayFormat) { Picker(selection: $user.callDisplayFormat) {
Text("Afficher le format").tag(true) Text("Afficher le format").tag(true)
Text("Masquer le format").tag(false) Text("Masquer le format").tag(false)
} label: { } label: {
} }
Picker(selection: $appSettings.callDisplayEntryFee) { Picker(selection: $user.callDisplayEntryFee) {
Text("Afficher le prix d'inscription").tag(true) Text("Afficher le prix d'inscription").tag(true)
Text("Masquer le prix d'inscription").tag(false) Text("Masquer le prix d'inscription").tag(false)
} label: { } label: {
@ -139,30 +139,34 @@ struct CallMessageCustomizationView: View {
} }
} }
} }
.onChange(of: appSettings.callUseFullCustomMessage) { .onChange(of: user.callUseFullCustomMessage) {
if appSettings.callUseFullCustomMessage == false { if user.callUseFullCustomMessage == false {
appSettings.callMessageBody = ContactType.defaultCustomMessage user.callMessageBody = ContactType.defaultCustomMessage
} }
_save() _save()
} }
.onChange(of: customCallMessageBody) { .onChange(of: customCallMessageBody) {
appSettings.callMessageBody = customCallMessageBody user.callMessageBody = customCallMessageBody
_save() _save()
} }
.onChange(of: customCallMessageSignature) { .onChange(of: customCallMessageSignature) {
appSettings.callMessageSignature = customCallMessageSignature user.callMessageSignature = customCallMessageSignature
_save() _save()
} }
.onChange(of: appSettings.callDisplayEntryFee) { .onChange(of: user.callDisplayEntryFee) {
_save() _save()
} }
.onChange(of: appSettings.callDisplayFormat) { .onChange(of: user.callDisplayFormat) {
_save() _save()
} }
} }
private func _save() { private func _save() {
dataStore.updateSettings() do {
try dataStore.userStorage.update()
} catch {
Logger.error(error)
}
} }
@ViewBuilder @ViewBuilder

@ -126,7 +126,11 @@ struct MainView: View {
Task { Task {
let lastDataSource = await FileImportManager.shared.importDataFromFFT() let lastDataSource = await FileImportManager.shared.importDataFromFFT()
dataStore.appSettings.lastDataSource = lastDataSource dataStore.appSettings.lastDataSource = lastDataSource
dataStore.updateSettings() do {
try dataStore.appSettingsStorage.write()
} catch {
Logger.error(error)
}
if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) { if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) {
await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate) await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate)
} }

@ -6,15 +6,16 @@
// //
import SwiftUI import SwiftUI
import LeStorage
struct GlobalSettingsView: View { struct GlobalSettingsView: View {
@EnvironmentObject var dataStore : DataStore @EnvironmentObject var dataStore : DataStore
var body: some View { var body: some View {
@Bindable var appSettings = dataStore.appSettings @Bindable var user = dataStore.user
List { List {
Section { Section {
Picker(selection: $appSettings.groupStageMatchFormatPreference) { Picker(selection: $user.groupStageMatchFormatPreference) {
Text("Automatique").tag(nil as MatchFormat?) Text("Automatique").tag(nil as MatchFormat?)
ForEach(MatchFormat.allCases, id: \.self) { format in ForEach(MatchFormat.allCases, id: \.self) { format in
Text(format.format).tag(format as MatchFormat?) Text(format.format).tag(format as MatchFormat?)
@ -25,7 +26,7 @@ struct GlobalSettingsView: View {
Spacer() Spacer()
} }
} }
Picker(selection: $appSettings.bracketMatchFormatPreference) { Picker(selection: $user.bracketMatchFormatPreference) {
Text("Automatique").tag(nil as MatchFormat?) Text("Automatique").tag(nil as MatchFormat?)
ForEach(MatchFormat.allCases, id: \.self) { format in ForEach(MatchFormat.allCases, id: \.self) { format in
Text(format.format).tag(format as MatchFormat?) Text(format.format).tag(format as MatchFormat?)
@ -36,7 +37,7 @@ struct GlobalSettingsView: View {
Spacer() Spacer()
} }
} }
Picker(selection: $appSettings.loserBracketMatchFormatPreference) { Picker(selection: $user.loserBracketMatchFormatPreference) {
Text("Automatique").tag(nil as MatchFormat?) Text("Automatique").tag(nil as MatchFormat?)
ForEach(MatchFormat.allCases, id: \.self) { format in ForEach(MatchFormat.allCases, id: \.self) { format in
Text(format.format).tag(format as MatchFormat?) Text(format.format).tag(format as MatchFormat?)
@ -54,11 +55,15 @@ struct GlobalSettingsView: View {
} }
} }
.onChange(of: [ .onChange(of: [
appSettings.bracketMatchFormatPreference, user.bracketMatchFormatPreference,
appSettings.groupStageMatchFormatPreference, user.groupStageMatchFormatPreference,
appSettings.loserBracketMatchFormatPreference user.loserBracketMatchFormatPreference
]) { ]) {
dataStore.updateSettings() do {
try dataStore.userStorage.update()
} catch {
Logger.error(error)
}
} }
.navigationTitle("Formats par défaut") .navigationTitle("Formats par défaut")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)

@ -6,6 +6,7 @@
// //
import SwiftUI import SwiftUI
import LeStorage
struct MatchFormatStorageView: View { struct MatchFormatStorageView: View {
@State private var estimatedDuration: Int @State private var estimatedDuration: Int
@ -42,8 +43,12 @@ struct MatchFormatStorageView: View {
} }
} }
.onChange(of: estimatedDuration) { .onChange(of: estimatedDuration) {
dataStore.appSettings.saveMatchFormatsDefaultDuration(matchFormat, estimatedDuration: estimatedDuration) dataStore.user.saveMatchFormatsDefaultDuration(matchFormat, estimatedDuration: estimatedDuration)
dataStore.updateSettings() do {
try dataStore.userStorage.update()
} catch {
Logger.error(error)
}
} }
} }
} }

@ -6,7 +6,7 @@
// //
import SwiftUI import SwiftUI
import SwiftData import LeStorage
struct PadelClubView: View { struct PadelClubView: View {
@State private var checkingFilesAttempt: Int = 0 @State private var checkingFilesAttempt: Int = 0
@ -130,7 +130,11 @@ struct PadelClubView: View {
Task { Task {
let lastDataSource = await FileImportManager.shared.importDataFromFFT() let lastDataSource = await FileImportManager.shared.importDataFromFFT()
dataStore.appSettings.lastDataSource = lastDataSource dataStore.appSettings.lastDataSource = lastDataSource
dataStore.updateSettings() do {
try dataStore.appSettingsStorage.write()
} catch {
Logger.error(error)
}
if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) { if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) {
await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate) await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate)
} }

@ -55,7 +55,7 @@ struct TournamentMatchFormatsSettingsView: View {
.onChange(of: tournament.additionalEstimationDuration) { .onChange(of: tournament.additionalEstimationDuration) {
_save() _save()
} }
.onChange(of: dataStore.appSettings.matchFormatsDefaultDuration) { .onChange(of: dataStore.user.matchFormatsDefaultDuration) {
_confirmOrSave() _confirmOrSave()
} }
.overlay(alignment: .bottom) { .overlay(alignment: .bottom) {

Loading…
Cancel
Save