struct ServiceFormView: View {
@Environment(\.modelContext) private var context
@Environment(\.dismiss) var dismiss
@FocusState private var focus: Field?
@AppStorage("date") var date = Date()
@State private var service = ""
@State private var prop = ""
@State private var count = ""
@State private var place = ""
@State private var presider = ""
@State private var preacher = ""
@State private var server = ""
@State private var memo = ""
@State private var communions = ""
@State private var sundayAdded = false
@State private var includeInASA = false
@State private var addEuch = false
@State private var showError = false
var serviceType = ["Select Service", "Sunday Eucharist", "Weekday Eucharist", "Private Eucharist", "Sunday Office", "Weekday Office", "Burial", "Wedding", "Other"]
private enum Field: Int, CaseIterable {
case prop, count, place, presider, preacher,server, memo, communion
}
var body: some View {
NavigationStack {
ZStack {
Color(.purple)
.opacity(0.3)
.ignoresSafeArea()
VStack {
DismissView()
.padding(.top, 15)
Group {
Text("Enter Service Record")
.font(.title)
DatePicker("", selection: $date)
.frame(width: 90, alignment: .center)
.padding(.bottom, 30)
VStack(spacing: 12) {
Picker("Select Service", selection: $service) {
ForEach(serviceType, id: \.self) { service in
Text(service).tag(service)
}
}
.buttonStyle(.borderedProminent)
}
}
if service == Constants.serviceList.privateEucharist || service == Constants.serviceList.other{
Toggle("Include in ASA", isOn: $includeInASA)
.frame(width: 200)
}
if service == Constants.serviceList.burial || service == Constants.serviceList.marriage {
Toggle("Include in ASA", isOn: $includeInASA)
.frame(width: 225)
Toggle("Celebrate Eucharist", isOn: $addEuch)
.frame(width: 225)
if addEuch {
Text("Communions: ")
TextField("Communions", text: $communions )
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .communion)
.numbersOnly($communions)
.frame(width: 125)
}
}
Text("Attendance: ")
.padding(.top, 20)
TextField("Count", text: $count)
.textFieldStyle(.roundedBorder)
.frame(width:125)
.numbersOnly($count)
.keyboardType(.numberPad)
.focused($focus, equals: .count)
if service == Constants.serviceList.eucharist ||
service == Constants.serviceList.weekdayEucharist ||
service == Constants.serviceList.privateEucharist {
Text("Communions: ")
TextField("Communions", text: $communions )
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .communion)
.numbersOnly($communions)
.keyboardType(.numberPad)
.frame(width: 125)
}
HStack {
TextField("Proper", text: $prop)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .prop)}
.padding(.top, 30)
TextField("Place of service", text: $place)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .place)
HStack {
TextField("Presider", text: $presider)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .presider)
TextField("Preacher", text: $preacher)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .preacher)
TextField("Server", text: $server)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .server)
}
TextField("Memo", text: $memo)
.textFieldStyle(.roundedBorder)
.focused($focus, equals: .memo)
Button {
addService()
dismiss()
} label: {
Text("Add")
}
.buttonStyle(.borderedProminent)
.font(.title)
.disabled(service.isEmpty)
.disabled(count.isEmpty)
.padding(.top, 30)
Spacer()
}
.padding()
}
.navigationBarBackButtonHidden()
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button {
focus = nil
} label: {
Image(systemName: "keyboard.chevron.compact.down")
}
}
}
.onAppear {
UITextField.appearance().clearButtonMode = .whileEditing
focus = .prop
}
}
}
}