Crossmate/Views/Components/AnnouncementBanner.swift (5821B)
1import SwiftUI
2
3
4
5
6struct AnnouncementBanner: View {
7 enum ContentSize {
8 case compact
9 case prominent
10
11 var childSpacing: CGFloat {
12 switch self {
13 case .compact: return 8
14 case .prominent: return 10
15 }
16 }
17
18 var iconFont: Font {
19 switch self {
20 case .compact: return .footnote.weight(.semibold)
21 case .prominent: return .subheadline.weight(.semibold)
22 }
23 }
24
25 var titleFont: Font {
26 switch self {
27 case .compact: return .subheadline.weight(.semibold)
28 case .prominent: return .callout.weight(.semibold)
29 }
30 }
31
32 var bodyFont: Font {
33 switch self {
34 case .compact: return .subheadline
35 case .prominent: return .callout
36 }
37 }
38 }
39
40 let announcement: Announcement
41
42
43
44 var fillsAvailableHeight = false
45
46
47 var showsIcon = true
48
49
50 var contentSize: ContentSize = .compact
51
52
53
54 var background: AnyShapeStyle = AnyShapeStyle(Color(.secondarySystemGroupedBackground))
55 let onDismiss: (() -> Void)?
56
57 var body: some View {
58 let severityColor = tint(for: announcement.severity)
59 HStack(alignment: .center, spacing: contentSize.childSpacing) {
60 if showsIcon {
61 Image(systemName: iconName(for: announcement.severity))
62 .font(contentSize.iconFont)
63 .foregroundStyle(severityColor)
64 .frame(width: 24)
65 .accessibilityHidden(true)
66 }
67
68 VStack(alignment: .leading, spacing: 3) {
69 if let title = announcement.title, !title.isEmpty {
70 Text(title)
71 .font(contentSize.titleFont)
72 .lineLimit(1)
73 }
74 Text(announcement.body)
75 .font(contentSize.bodyFont)
76 .foregroundStyle(.primary)
77 .multilineTextAlignment(.leading)
78 .lineLimit(3)
79 }
80 .frame(maxWidth: .infinity, alignment: .leading)
81
82 if case .manual = announcement.dismissal {
83 Button {
84 onDismiss?()
85 } label: {
86 Image(systemName: "xmark.circle.fill")
87 .font(.callout)
88 .symbolRenderingMode(.hierarchical)
89 }
90 .buttonStyle(.plain)
91 .foregroundStyle(.secondary)
92 .accessibilityLabel("Dismiss announcement")
93 }
94 }
95 .padding(.horizontal, 10)
96 .padding(.vertical, 10)
97
98
99 .padding(.leading, showsIcon ? 4 : 12)
100 .frame(
101 maxWidth: .infinity,
102 maxHeight: fillsAvailableHeight ? .infinity : nil,
103 alignment: .leading
104 )
105 .background(background, in: RoundedRectangle(cornerRadius: 8, style: .continuous))
106
107
108
109
110 .overlay(alignment: .leading) {
111 Rectangle()
112 .fill(severityColor)
113 .frame(width: 4)
114 .accessibilityHidden(true)
115 }
116 .overlay {
117 RoundedRectangle(cornerRadius: 8, style: .continuous)
118 .strokeBorder(Color.primary.opacity(0.08), lineWidth: 1)
119 }
120 .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
121 .contentShape(Rectangle())
122 .onTapGesture {
123
124
125 guard case .manual = announcement.dismissal else { return }
126 onDismiss?()
127 }
128 .accessibilityElement(children: .combine)
129 }
130
131
132 private func iconName(for severity: Announcement.Severity) -> String {
133 switch severity {
134 case .tip: return "lightbulb"
135 case .info: return "bell"
136 case .warning: return "exclamationmark.triangle"
137 case .error: return "xmark.octagon"
138 }
139 }
140
141 private func tint(for severity: Announcement.Severity) -> Color {
142 switch severity {
143 case .tip: return .yellow
144 case .info: return .blue
145 case .warning: return .orange
146 case .error: return .red
147 }
148 }
149}