|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct PublishButton: View { |
| 4 | + @ObservedObject var viewModel: PublishButtonViewModel |
| 5 | + |
| 6 | + var body: some View { |
| 7 | + ZStack { |
| 8 | + Button(action: viewModel.onSubmitTapped) { |
| 9 | + Text(viewModel.title) |
| 10 | + .font(.title3.weight(.medium)) |
| 11 | + .frame(maxWidth: .infinity) |
| 12 | + .opacity(isDisabled ? 0 : 1) |
| 13 | + } |
| 14 | + .buttonStyle(.borderedProminent) |
| 15 | + .controlSize(.large) |
| 16 | + .disabled(isDisabled) |
| 17 | + .buttonBorderShape(.roundedRectangle(radius: 8)) |
| 18 | + |
| 19 | + switch viewModel.state { |
| 20 | + case .default: |
| 21 | + EmptyView() |
| 22 | + case .loading: |
| 23 | + ProgressView() |
| 24 | + .tint(Color.secondary) |
| 25 | + case let .uploading(title, progress): |
| 26 | + HStack(spacing: 10) { |
| 27 | + ProgressView() |
| 28 | + .tint(Color.secondary) |
| 29 | + |
| 30 | + VStack(alignment: .leading) { |
| 31 | + Text(title) |
| 32 | + .font(.subheadline.weight(.medium)) |
| 33 | + if let progress { |
| 34 | + Text(Strings.progress(progress)) |
| 35 | + .foregroundStyle(Color.secondary) |
| 36 | + .font(.footnote) |
| 37 | + .monospacedDigit() |
| 38 | + } |
| 39 | + } |
| 40 | + .lineLimit(1) |
| 41 | + .foregroundStyle(Color.primary) |
| 42 | + |
| 43 | + Spacer() |
| 44 | + } |
| 45 | + .padding(.horizontal) |
| 46 | + case let .failed(title, details, onRetryTapped): |
| 47 | + HStack { |
| 48 | + Image(systemName: "exclamationmark.triangle.fill") |
| 49 | + .foregroundStyle(Color.red) |
| 50 | + VStack(alignment: .leading) { |
| 51 | + Text(title) |
| 52 | + .font(.subheadline.weight(.medium)) |
| 53 | + .foregroundStyle(.primary) |
| 54 | + if let details { |
| 55 | + Text(details) |
| 56 | + .font(.footnote) |
| 57 | + .foregroundStyle(.secondary) |
| 58 | + } |
| 59 | + } |
| 60 | + .lineLimit(1) |
| 61 | + |
| 62 | + Spacer() |
| 63 | + |
| 64 | + if let onRetryTapped { |
| 65 | + Button(Strings.retry, action: onRetryTapped) |
| 66 | + .font(.subheadline) |
| 67 | + } |
| 68 | + } |
| 69 | + .padding(.horizontal) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private var isDisabled: Bool { |
| 75 | + switch viewModel.state { |
| 76 | + case .default: false |
| 77 | + case .loading, .uploading, .failed: true |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +final class PublishButtonViewModel: ObservableObject { |
| 83 | + let title: String |
| 84 | + let onSubmitTapped: () -> Void |
| 85 | + @Published var state: PublishButtonState = .default |
| 86 | + |
| 87 | + init(title: String, onSubmitTapped: @escaping () -> Void, state: PublishButtonState = .default) { |
| 88 | + self.title = title |
| 89 | + self.onSubmitTapped = onSubmitTapped |
| 90 | + self.state = state |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +enum PublishButtonState { |
| 95 | + case `default` |
| 96 | + case loading |
| 97 | + case uploading(title: String, progress: Progress?) |
| 98 | + case failed(title: String, details: String? = nil, onRetryTapped: (() -> Void)? = nil) |
| 99 | + |
| 100 | + struct Progress { |
| 101 | + let completed: Int64 |
| 102 | + let total: Int64 |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +private enum Strings { |
| 107 | + static func progress(_ progress: PublishButtonState.Progress) -> String { |
| 108 | + let format = NSLocalizedString("publishButton.progress", value: "%@ of %@", comment: "Shows the download or upload progress with two parameters: preformatted completed and total bytes") |
| 109 | + return String(format: format, ByteCountFormatter.string(fromByteCount: progress.completed, countStyle: .file), ByteCountFormatter.string(fromByteCount: progress.total, countStyle: .file)) |
| 110 | + } |
| 111 | + |
| 112 | + static let retry = NSLocalizedString("publishButton.retry", value: "Retry", comment: "Retry button title") |
| 113 | +} |
| 114 | + |
| 115 | +#Preview { |
| 116 | + VStack(spacing: 16) { |
| 117 | + PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .default)) |
| 118 | + PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .loading)) |
| 119 | + PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .uploading(title: "Uploading media...", progress: .init(completed: 100, total: 2000)))) |
| 120 | + PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .failed(title: "Failed to upload media"))) |
| 121 | + PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .failed(title: "Failed to upload media", details: "Not connected to Internet", onRetryTapped: {}))) |
| 122 | + } |
| 123 | + .padding() |
| 124 | +} |
0 commit comments