DayByDay/Parent/ParentAreaView.swift
88 lines · 3071 bytes
2 symbols in this file
1//
2// ParentAreaView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// Grown-up screen for adding, editing, and deleting profiles, and for
9/// each child's progress.
10struct ParentAreaView: View {
11 let onDone: () -> Void
12
13 @Environment(ProfileSession.self) private var session
14 @State private var pendingDelete: Profile?
15
16 var body: some View {
17 NavigationStack {
18 List {
19 Section("Children") {
20 ForEach(session.profiles) { profile in
21 NavigationLink {
22 ProfileEditorView(profile: profile)
23 } label: {
24 ProfileRow(profile: profile)
25 }
26 .swipeActions {
27 Button("Delete", role: .destructive) { pendingDelete = profile }
28 }
29 }
30 NavigationLink {
31 ProfileEditorView(profile: nil)
32 } label: {
33 HStack(spacing: 14) {
34 Image(systemName: "plus.circle.fill")
35 .foregroundStyle(Color.accentColor)
36 Text("Add a child")
37 }
38 }
39 }
40 if !session.profiles.isEmpty {
41 Section("Progress") {
42 ForEach(session.profiles) { profile in
43 NavigationLink {
44 ChildProgressView(profile: profile)
45 } label: {
46 ProfileRow(profile: profile)
47 }
48 }
49 }
50 }
51 }
52 .navigationTitle("Grown-Ups")
53 .toolbar {
54 ToolbarItem(placement: .confirmationAction) {
55 Button("Done", action: onDone)
56 }
57 }
58 .confirmationDialog(
59 "Delete \(pendingDelete?.name ?? "")?",
60 isPresented: Binding(get: { pendingDelete != nil }, set: { if !$0 { pendingDelete = nil } }),
61 titleVisibility: .visible
62 ) {
63 Button("Delete Profile and Progress", role: .destructive) {
64 if let profile = pendingDelete {
65 session.store.delete(profile)
66 session.profilesChanged()
67 }
68 pendingDelete = nil
69 }
70 } message: {
71 Text("This removes their stickers and progress. It can't be undone.")
72 }
73 }
74 }
75}
76
77private struct ProfileRow: View {
78 let profile: Profile
79
80 var body: some View {
81 HStack(spacing: 14) {
82 AvatarBadge(avatar: profile.avatar, color: profile.color)
83 .frame(width: 44, height: 44)
84 Text(profile.name)
85 .font(.system(.title3, design: .rounded, weight: .semibold))
86 }
87 }
88}