parent
9e2882391b
commit
9758ecfcbc
@ -0,0 +1,64 @@ |
|||||||
|
// |
||||||
|
// Court.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 23/04/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import Foundation |
||||||
|
import SwiftUI |
||||||
|
import LeStorage |
||||||
|
|
||||||
|
@Observable |
||||||
|
class Court : ModelObject, Storable, Hashable { |
||||||
|
static func resourceName() -> String { return "courts" } |
||||||
|
|
||||||
|
static func == (lhs: Court, rhs: Court) -> Bool { |
||||||
|
lhs.id == rhs.id |
||||||
|
} |
||||||
|
|
||||||
|
func hash(into hasher: inout Hasher) { |
||||||
|
return hasher.combine(id) |
||||||
|
} |
||||||
|
|
||||||
|
var id: String = Store.randomId() |
||||||
|
var index: Int |
||||||
|
var club: String |
||||||
|
var name: String? |
||||||
|
var exitAllowed: Bool = false |
||||||
|
var indoor: Bool = false |
||||||
|
|
||||||
|
internal init(club: String, name: String? = nil, index: Int) { |
||||||
|
self.club = club |
||||||
|
self.name = name |
||||||
|
self.index = index |
||||||
|
} |
||||||
|
|
||||||
|
func courtTitle() -> String { |
||||||
|
self.name ?? courtIndexTitle() |
||||||
|
} |
||||||
|
|
||||||
|
func courtIndexTitle() -> String { |
||||||
|
Self.courtIndexedTitle(atIndex: index) |
||||||
|
} |
||||||
|
|
||||||
|
static func courtIndexedTitle(atIndex index: Int) -> String { |
||||||
|
("Terrain #" + (index + 1).formatted()) |
||||||
|
} |
||||||
|
|
||||||
|
func clubObject() -> Club? { |
||||||
|
Store.main.findById(club) |
||||||
|
} |
||||||
|
|
||||||
|
override func deleteDependencies() throws { |
||||||
|
} |
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey { |
||||||
|
case _id = "id" |
||||||
|
case _index = "index" |
||||||
|
case _club = "club" |
||||||
|
case _name = "name" |
||||||
|
case _exitAllowed = "exitAllowed" |
||||||
|
case _indoor = "indoor" |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
// |
||||||
|
// CourtView.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 23/04/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import SwiftUI |
||||||
|
|
||||||
|
struct CourtView: View { |
||||||
|
@EnvironmentObject var dataStore: DataStore |
||||||
|
@Bindable var court: Court |
||||||
|
@State private var name: String = "" |
||||||
|
|
||||||
|
init(court: Court) { |
||||||
|
self.court = court |
||||||
|
_name = State(wrappedValue: court.name ?? "") |
||||||
|
} |
||||||
|
|
||||||
|
var body: some View { |
||||||
|
Form { |
||||||
|
Section { |
||||||
|
LabeledContent { |
||||||
|
TextField("Nom", text: $name) |
||||||
|
.keyboardType(.alphabet) |
||||||
|
.multilineTextAlignment(.trailing) |
||||||
|
.frame(maxWidth: .infinity) |
||||||
|
} label: { |
||||||
|
Text("Nom du terrain") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
Toggle(isOn: $court.exitAllowed) { |
||||||
|
Text("Sortie authorisée") |
||||||
|
} |
||||||
|
Toggle(isOn: $court.indoor) { |
||||||
|
Text("Terrain intérieur") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
.onChange(of: name) { |
||||||
|
court.name = name |
||||||
|
try? dataStore.courts.addOrUpdate(instance: court) |
||||||
|
} |
||||||
|
.onChange(of: [court.indoor, court.exitAllowed]) { |
||||||
|
try? dataStore.courts.addOrUpdate(instance: court) |
||||||
|
} |
||||||
|
.navigationTitle(court.courtTitle()) |
||||||
|
.navigationBarTitleDisplayMode(.inline) |
||||||
|
.toolbarBackground(.visible, for: .navigationBar) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#Preview { |
||||||
|
CourtView(court: Court.mock()) |
||||||
|
} |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
// |
||||||
|
// BarButtonView.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 24/04/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import SwiftUI |
||||||
|
|
||||||
|
struct BarButtonView: View { |
||||||
|
let accessibilityLabel: String |
||||||
|
let icon: String |
||||||
|
let action: () -> () |
||||||
|
|
||||||
|
init(_ accessibilityLabel: String, icon: String, action: @escaping () -> Void) { |
||||||
|
self.accessibilityLabel = accessibilityLabel |
||||||
|
self.icon = icon |
||||||
|
self.action = action |
||||||
|
} |
||||||
|
|
||||||
|
var body: some View { |
||||||
|
Button(action: { |
||||||
|
action() |
||||||
|
}) { |
||||||
|
Label { |
||||||
|
Text(accessibilityLabel) |
||||||
|
} icon: { |
||||||
|
Image(systemName: icon) |
||||||
|
.resizable() |
||||||
|
.scaledToFit() |
||||||
|
.frame(minHeight: 28) |
||||||
|
} |
||||||
|
.labelStyle(.iconOnly) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue