|
| 1 | +// |
| 2 | +// Wire |
| 3 | +// Copyright (C) 2025 Wire Swiss GmbH |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | +// |
| 18 | + |
| 19 | +import SwiftUI |
| 20 | +import WireDesign |
| 21 | +import WireFoundation |
| 22 | + |
| 23 | +struct WireCellsLargeDocumentPreviewView: View { |
| 24 | + enum Constants { |
| 25 | + static let errorColor = ColorTheme.Base.error.color |
| 26 | + } |
| 27 | + |
| 28 | + let headerIcon: Image |
| 29 | + let headerText: String |
| 30 | + let labelText: String |
| 31 | + let progress: Double? |
| 32 | + let isError: Bool |
| 33 | + let url: URL? |
| 34 | + |
| 35 | + var body: some View { |
| 36 | + WireCellsAttachmentPreview( |
| 37 | + progress: progress, |
| 38 | + progressColor: isError |
| 39 | + ? Constants.errorColor : ColorTheme.Base.primary.color |
| 40 | + ) { |
| 41 | + VStack { |
| 42 | + WireCellsDocumentHeaderView( |
| 43 | + headerIcon: headerIcon, |
| 44 | + headerText: headerText, |
| 45 | + labelText: labelText, |
| 46 | + progress: progress, |
| 47 | + isError: isError |
| 48 | + ) |
| 49 | + .background(ColorTheme.Backgrounds.surfaceVariant.color) |
| 50 | + .frame(height: 74) |
| 51 | + .frame(maxWidth: .infinity) |
| 52 | + previewContainer { |
| 53 | + if url != nil { |
| 54 | + asyncImage(url: url!) |
| 55 | + } else { |
| 56 | + errorView(text: "Invalid URL") |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @ViewBuilder |
| 64 | + private func asyncImage(url: URL) -> some View { |
| 65 | + AsyncImage(url: url) { phase in |
| 66 | + switch phase { |
| 67 | + case .empty: |
| 68 | + loadingView() |
| 69 | + case let .success(image): |
| 70 | + image |
| 71 | + .resizable() |
| 72 | + .scaledToFill() |
| 73 | + case .failure: |
| 74 | + errorView(text: "Unable to display preview") |
| 75 | + @unknown default: |
| 76 | + EmptyView() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @ViewBuilder |
| 82 | + private func previewContainer(@ViewBuilder content: () -> some View) |
| 83 | + -> some View { |
| 84 | + Color.clear |
| 85 | + .aspectRatio(CGFloat(8.0 / 3.0), contentMode: .fit) |
| 86 | + .background(alignment: .top) { |
| 87 | + content() |
| 88 | + } |
| 89 | + .clipped() |
| 90 | + } |
| 91 | + |
| 92 | + @ViewBuilder |
| 93 | + private func loadingView() -> some View { |
| 94 | + ZStack { |
| 95 | + Color(ColorTheme.Backdrop.background) |
| 96 | + VStack { |
| 97 | + ProgressView() |
| 98 | + Text("Loading...") |
| 99 | + .foregroundStyle(ColorTheme.Base.onError.color) |
| 100 | + .wireTextStyle(.subline1) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @ViewBuilder |
| 106 | + private func errorView(text: String) -> some View { |
| 107 | + ZStack { |
| 108 | + Color(ColorTheme.Backdrop.background) |
| 109 | + VStack { |
| 110 | + Image(systemName: "exclamationmark.triangle") |
| 111 | + .fontWeight(.semibold) |
| 112 | + .font(.system(size: 14)) |
| 113 | + .foregroundStyle(Constants.errorColor) |
| 114 | + Text(text) |
| 115 | + .foregroundStyle(ColorTheme.Base.onError.color) |
| 116 | + .wireTextStyle(.subline1) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +#Preview { |
| 123 | + WireCellsLargeDocumentPreviewView( |
| 124 | + headerIcon: Image(FileIcon.pdf.resource), |
| 125 | + headerText: "PDF (336 KB)", |
| 126 | + labelText: "CDR_20220120 Accessibility Review Reviewed Final Plus", |
| 127 | + progress: 0.7, |
| 128 | + isError: false, |
| 129 | + url: URL( |
| 130 | + string: |
| 131 | + "https://i.kym-cdn.com/entries/icons/facebook/000/018/012/this_is_fine.jpg" |
| 132 | + ) |
| 133 | + ) |
| 134 | + .environment(\.wireTextStyleMapping, WireTextStyleMapping()) |
| 135 | +} |
0 commit comments