|
| 1 | +// |
| 2 | +// DiscussionAtAGlanceView.swift |
| 3 | +// Hax |
| 4 | +// |
| 5 | +// Created by Luis Fariña on 9/12/25. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +struct DiscussionAtAGlanceView< |
| 11 | + Model: DiscussionAtAGlanceViewModelProtocol |
| 12 | +>: View { |
| 13 | + |
| 14 | + // MARK: Properties |
| 15 | + |
| 16 | + @State var model: Model |
| 17 | + |
| 18 | + // MARK: Body |
| 19 | + |
| 20 | + var body: some View { |
| 21 | + VStack(alignment: .leading, spacing: 10) { |
| 22 | + Group { |
| 23 | + HStack(spacing: 5) { |
| 24 | + Image(systemName: "apple.intelligence") |
| 25 | + Text("Discussion at a glance") |
| 26 | + .bold() |
| 27 | + .textCase(.uppercase) |
| 28 | + } |
| 29 | + |
| 30 | + if model.state == .tapToGenerate { |
| 31 | + Text( |
| 32 | + """ |
| 33 | + Tap to generate a summary of the story's \ |
| 34 | + top comments |
| 35 | + """ |
| 36 | + ) |
| 37 | + } else if model.state == .generating { |
| 38 | + Text("Generating summary…") |
| 39 | + } |
| 40 | + } |
| 41 | + .foregroundStyle(.secondary) |
| 42 | + |
| 43 | + if case .generated(let summary) = model.state { |
| 44 | + if let summary { |
| 45 | + Text(LocalizedStringKey(summary)) |
| 46 | + } else { |
| 47 | + Text( |
| 48 | + "There was an error generating the summary." |
| 49 | + ) |
| 50 | + .foregroundStyle(.secondary) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + .animation(.default, value: model.state) |
| 55 | + .font(.footnote) |
| 56 | + .listRowBackground(listRowBackground) |
| 57 | + .listRowSeparator(.hidden) |
| 58 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 59 | + .contentShape(Rectangle()) |
| 60 | + .onTapGesture { |
| 61 | + Task { |
| 62 | + await model.onTap() |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// MARK: - Private extension |
| 69 | + |
| 70 | +private extension DiscussionAtAGlanceView { |
| 71 | + |
| 72 | + // MARK: Properties |
| 73 | + |
| 74 | + var listRowBackground: some View { |
| 75 | + AngularGradient( |
| 76 | + gradient: Gradient( |
| 77 | + colors: [ |
| 78 | + Color( |
| 79 | + red: .zero, |
| 80 | + green: 0.5, |
| 81 | + blue: 1, |
| 82 | + opacity: 0.8 |
| 83 | + ), |
| 84 | + Color( |
| 85 | + red: 0.5, |
| 86 | + green: .zero, |
| 87 | + blue: 1, |
| 88 | + opacity: 0.7 |
| 89 | + ), |
| 90 | + Color( |
| 91 | + red: 1, |
| 92 | + green: .zero, |
| 93 | + blue: 0.5, |
| 94 | + opacity: 0.6 |
| 95 | + ), |
| 96 | + Color( |
| 97 | + red: 1, |
| 98 | + green: 0.5, |
| 99 | + blue: .zero, |
| 100 | + opacity: 0.8 |
| 101 | + ), |
| 102 | + Color( |
| 103 | + red: .zero, |
| 104 | + green: 0.5, |
| 105 | + blue: 1, |
| 106 | + opacity: 0.8 |
| 107 | + ) |
| 108 | + ] |
| 109 | + ), |
| 110 | + center: .center |
| 111 | + ) |
| 112 | + .blur(radius: 50) |
| 113 | + .overlay(Color(UIColor.systemBackground).opacity(0.25)) |
| 114 | + .clipped() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#if DEBUG |
| 119 | + |
| 120 | +// MARK: - Previews |
| 121 | + |
| 122 | +// MARK: Types |
| 123 | + |
| 124 | +private struct PreviewDiscussionAtAGlanceViewModel: DiscussionAtAGlanceViewModelProtocol { |
| 125 | + |
| 126 | + // MARK: Properties |
| 127 | + |
| 128 | + let state: DiscussionAtAGlanceViewState |
| 129 | + |
| 130 | + // MARK: Methods |
| 131 | + |
| 132 | + func onTap() async { |
| 133 | + // Do nothing |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// MARK: Previews |
| 138 | + |
| 139 | +@available(iOS 26.0, *) |
| 140 | +#Preview { |
| 141 | + List { |
| 142 | + DiscussionAtAGlanceView( |
| 143 | + model: PreviewDiscussionAtAGlanceViewModel( |
| 144 | + state: .tapToGenerate |
| 145 | + ) |
| 146 | + ) |
| 147 | + Spacer() |
| 148 | + DiscussionAtAGlanceView( |
| 149 | + model: PreviewDiscussionAtAGlanceViewModel( |
| 150 | + state: .generating |
| 151 | + ) |
| 152 | + ) |
| 153 | + Spacer() |
| 154 | + DiscussionAtAGlanceView( |
| 155 | + model: PreviewDiscussionAtAGlanceViewModel( |
| 156 | + state: .generated( |
| 157 | + summary: """ |
| 158 | + Lorem ipsum dolor sit amet, consectetur \ |
| 159 | + adipiscing elit. Aliquam suscipit ullamcorper \ |
| 160 | + lacus, in fringilla nulla sagittis eget. Ut \ |
| 161 | + pretium semper ligula, non vestibulum neque \ |
| 162 | + posuere sit amet. Quisque ut eros finibus, \ |
| 163 | + dictum orci vel, vestibulum neque. |
| 164 | + """ |
| 165 | + ) |
| 166 | + ) |
| 167 | + ) |
| 168 | + Spacer() |
| 169 | + DiscussionAtAGlanceView( |
| 170 | + model: PreviewDiscussionAtAGlanceViewModel( |
| 171 | + state: .generated(summary: nil) |
| 172 | + ) |
| 173 | + ) |
| 174 | + } |
| 175 | + .listStyle(.plain) |
| 176 | +} |
| 177 | + |
| 178 | +#endif |
0 commit comments