DayByDay/Profiles/ProfileEditorView.swift
149 lines · 5810 bytes
2 symbols in this file
1//
2// ProfileEditorView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// Creates a profile (`profile == nil`) or edits an existing one.
9struct ProfileEditorView: View {
10 let profile: Profile?
11
12 @Environment(ProfileSession.self) private var session
13 @Environment(\.dismiss) private var dismiss
14 @State private var name = ""
15 @State private var avatar: AvatarAnimal = .cat
16 @State private var color: ProfileColor = .blue
17 @State private var hasBirthday = false
18 @State private var month = 1
19 @State private var day = 1
20 @State private var error: ProfileError?
21 @State private var pictures: [DayOfWeek: DayPictureKind] = [:]
22
23 private let avatarColumns = [GridItem(.adaptive(minimum: 64), spacing: 12)]
24
25 init(profile: Profile?) {
26 self.profile = profile
27 if let profile {
28 _name = State(initialValue: profile.name)
29 _avatar = State(initialValue: profile.avatar)
30 _color = State(initialValue: profile.color)
31 _hasBirthday = State(initialValue: profile.birthdayMonth != nil)
32 _month = State(initialValue: profile.birthdayMonth ?? 1)
33 _day = State(initialValue: profile.birthdayDay ?? 1)
34 }
35 }
36
37 var body: some View {
38 Form {
39 Section("Name") {
40 TextField("Child's name", text: $name)
41 .textInputAutocapitalization(.words)
42 if error == .blankName {
43 Text("Enter a name.").foregroundStyle(.red)
44 }
45 }
46
47 Section("Animal") {
48 LazyVGrid(columns: avatarColumns, spacing: 12) {
49 ForEach(AvatarAnimal.allCases) { option in
50 Button { avatar = option } label: {
51 AvatarBadge(avatar: option, color: color)
52 .overlay(Circle().strokeBorder(.primary, lineWidth: avatar == option ? 4 : 0))
53 }
54 .buttonStyle(.plain)
55 .accessibilityLabel(option.spokenName)
56 .accessibilityAddTraits(avatar == option ? .isSelected : [])
57 }
58 }
59 .padding(.vertical, 6)
60 }
61
62 Section("Color") {
63 LazyVGrid(columns: avatarColumns, spacing: 12) {
64 ForEach(ProfileColor.allCases) { option in
65 Button { color = option } label: {
66 Circle()
67 .fill(option.color)
68 .frame(width: 52, height: 52)
69 .overlay(Circle().strokeBorder(.primary, lineWidth: color == option ? 4 : 0))
70 }
71 .buttonStyle(.plain)
72 .accessibilityLabel(String(describing: option))
73 .accessibilityAddTraits(color == option ? .isSelected : [])
74 }
75 }
76 .padding(.vertical, 6)
77 }
78
79 Section {
80 ForEach(DayOfWeek.allCases) { day in
81 Picker(day.name, selection: Binding(
82 get: { pictures[day] },
83 set: { pictures[day] = $0 }
84 )) {
85 Text("None").tag(DayPictureKind?.none)
86 ForEach(DayPictureKind.allCases) { kind in
87 Label(kind.name.capitalized, systemImage: kind.symbol).tag(DayPictureKind?.some(kind))
88 }
89 }
90 }
91 } header: {
92 Text("Day pictures")
93 } footer: {
94 Text("Shown on that day's card and said in the Today routine, e.g. \"Today is swim day!\"")
95 }
96
97 Section("Birthday") {
98 Toggle("Add birthday", isOn: $hasBirthday)
99 if hasBirthday {
100 Picker("Month", selection: $month) {
101 ForEach(1...12, id: \.self) { m in
102 Text(AppCalendar.current.monthSymbols[m - 1]).tag(m)
103 }
104 }
105 Picker("Day", selection: $day) {
106 ForEach(1...ProfileStore.daysInMonth[month - 1], id: \.self) { d in
107 Text("\(d)").tag(d)
108 }
109 }
110 }
111 }
112 }
113 .navigationTitle(profile == nil ? "New Child" : "Edit \(profile!.name)")
114 .toolbar {
115 ToolbarItem(placement: .confirmationAction) {
116 Button("Save", action: save)
117 }
118 }
119 .onAppear {
120 guard let profile, pictures.isEmpty else { return }
121 for day in DayOfWeek.allCases {
122 pictures[day] = session.store.picture(on: day, for: profile)
123 }
124 }
125 .onChange(of: month) {
126 day = min(day, ProfileStore.daysInMonth[month - 1])
127 }
128 }
129
130 private func save() {
131 let birthday = hasBirthday ? (month: month, day: day) : nil
132 do {
133 let saved: Profile
134 if let profile {
135 try session.store.update(profile, name: name, avatar: avatar, color: color, birthday: birthday)
136 saved = profile
137 } else {
138 saved = try session.store.create(name: name, avatar: avatar, color: color, birthday: birthday)
139 }
140 for day in DayOfWeek.allCases {
141 session.store.setPicture(pictures[day], on: day, for: saved)
142 }
143 session.profilesChanged()
144 dismiss()
145 } catch let profileError as ProfileError {
146 error = profileError
147 } catch {}
148 }
149}