|
| 1 | +// SPDX-FileCopyrightText: Nextcloud GmbH |
| 2 | +// SPDX-FileCopyrightText: 2026 Marino Faggiana |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +import SwiftUI |
| 6 | +import LucidBanner |
| 7 | + |
| 8 | +@MainActor |
| 9 | +func showAlertActionBannerView(scene: UIWindowScene?, |
| 10 | + title: String? = nil, |
| 11 | + subtitle: String? = nil, |
| 12 | + onConfirm: (() -> Void)? = nil) { |
| 13 | + let isPad = scene?.traitCollection.userInterfaceIdiom == .pad |
| 14 | + let horizontalLayout: LucidBanner.HorizontalLayout = |
| 15 | + isPad |
| 16 | + ? .centered(width: 450) |
| 17 | + : .stretch(margins: 20) |
| 18 | + |
| 19 | + let payload = LucidBannerPayload( |
| 20 | + title: title, |
| 21 | + subtitle: subtitle, |
| 22 | + vPosition: .top, |
| 23 | + horizontalLayout: horizontalLayout, |
| 24 | + swipeToDismiss: true |
| 25 | + ) |
| 26 | + |
| 27 | + LucidBanner.shared.show(scene: scene, |
| 28 | + payload: payload, |
| 29 | + policy: .replace) { _, _ in |
| 30 | + LucidBanner.shared.dismiss() |
| 31 | + } content: { state in |
| 32 | + AlertActionBannerView( |
| 33 | + state: state, |
| 34 | + onConfirm: { |
| 35 | + onConfirm?() |
| 36 | + LucidBanner.shared.dismiss() |
| 37 | + }, |
| 38 | + onCancel: { |
| 39 | + LucidBanner.shared.dismiss() |
| 40 | + } |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +// MARK: - SwiftUI |
| 47 | + |
| 48 | +struct AlertActionBannerView: View { |
| 49 | + @ObservedObject var state: LucidBannerState |
| 50 | + |
| 51 | + let onConfirm: (() -> Void)? |
| 52 | + let onCancel: (() -> Void)? |
| 53 | + |
| 54 | + init( |
| 55 | + state: LucidBannerState, |
| 56 | + onConfirm: (() -> Void)? = nil, |
| 57 | + onCancel: (() -> Void)? = nil |
| 58 | + ) { |
| 59 | + self.state = state |
| 60 | + self.onConfirm = onConfirm |
| 61 | + self.onCancel = onCancel |
| 62 | + } |
| 63 | + |
| 64 | + var body: some View { |
| 65 | + alertActionContainerView { |
| 66 | + VStack(spacing: 20) { |
| 67 | + // Title |
| 68 | + if let title = state.payload.title, !title.isEmpty { |
| 69 | + Text(title) |
| 70 | + .font(.headline.weight(.semibold)) |
| 71 | + .foregroundStyle(state.payload.textColor) |
| 72 | + .multilineTextAlignment(.center) |
| 73 | + } |
| 74 | + |
| 75 | + // Subtitle |
| 76 | + if let subtitle = state.payload.subtitle, !subtitle.isEmpty { |
| 77 | + Text(subtitle) |
| 78 | + .font(.subheadline) |
| 79 | + .foregroundStyle(state.payload.textColor) |
| 80 | + .multilineTextAlignment(.center) |
| 81 | + } |
| 82 | + |
| 83 | + // Buttons |
| 84 | + HStack(spacing: 12) { |
| 85 | + Button("_cancel_") { |
| 86 | + onCancel?() |
| 87 | + } |
| 88 | + .padding(.vertical, 8) |
| 89 | + .frame(maxWidth: .infinity) |
| 90 | + .background( |
| 91 | + Capsule() |
| 92 | + .stroke(Color.secondary.opacity(0.5), lineWidth: 1) |
| 93 | + ) |
| 94 | + .foregroundStyle(.primary) |
| 95 | + |
| 96 | + Button("_ok_") { |
| 97 | + onConfirm?() |
| 98 | + } |
| 99 | + .padding(.vertical, 8) |
| 100 | + .frame(maxWidth: .infinity) |
| 101 | + .background( |
| 102 | + Capsule().fill(Color.accentColor) |
| 103 | + ) |
| 104 | + .foregroundStyle(.white) |
| 105 | + } |
| 106 | + .frame(maxWidth: .infinity) |
| 107 | + } |
| 108 | + .padding(20) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // MARK: - Container |
| 113 | + |
| 114 | + @ViewBuilder |
| 115 | + func alertActionContainerView<Content: View>(@ViewBuilder _ content: () -> Content) -> some View { |
| 116 | + let cornerRadius: CGFloat = 22 |
| 117 | + let backgroundColor = Color(.systemBackground).opacity(0.9) |
| 118 | + |
| 119 | + if #available(iOS 26, *) { |
| 120 | + content() |
| 121 | + .background( |
| 122 | + RoundedRectangle(cornerRadius: cornerRadius) |
| 123 | + .fill(backgroundColor) |
| 124 | + .id(backgroundColor) |
| 125 | + ) |
| 126 | + .glassEffect(.clear, in: RoundedRectangle(cornerRadius: cornerRadius)) |
| 127 | + .shadow(color: .black.opacity(0.5), radius: 10, x: 0, y: 4) |
| 128 | + } else { |
| 129 | + content() |
| 130 | + .background( |
| 131 | + RoundedRectangle(cornerRadius: cornerRadius) |
| 132 | + .fill(backgroundColor) |
| 133 | + .id(backgroundColor) |
| 134 | + ) |
| 135 | + .background( |
| 136 | + RoundedRectangle(cornerRadius: cornerRadius) |
| 137 | + .fill(.ultraThinMaterial) |
| 138 | + ) |
| 139 | + .overlay( |
| 140 | + RoundedRectangle(cornerRadius: cornerRadius) |
| 141 | + .stroke(backgroundColor, lineWidth: 0.6) |
| 142 | + .allowsHitTesting(false) |
| 143 | + ) |
| 144 | + .shadow(color: .black.opacity(0.5), radius: 10, x: 0, y: 4) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// MARK: - Preview |
| 150 | + |
| 151 | +#Preview("Alert - Light") { |
| 152 | + |
| 153 | + let payload = LucidBannerPayload( |
| 154 | + title: "Title ?", |
| 155 | + subtitle: "Subtitle.", |
| 156 | + stage: .warning, |
| 157 | + vPosition: .center, |
| 158 | + blocksTouches: true |
| 159 | + ) |
| 160 | + |
| 161 | + let state = LucidBannerState(payload: payload) |
| 162 | + |
| 163 | + return ZStack { |
| 164 | + Color.black.opacity(0.15) |
| 165 | + .ignoresSafeArea() |
| 166 | + |
| 167 | + AlertActionBannerView( |
| 168 | + state: state, |
| 169 | + onConfirm: { |
| 170 | + print("Confirm tapped") |
| 171 | + }, |
| 172 | + onCancel: { |
| 173 | + print("Cancel tapped") |
| 174 | + } |
| 175 | + ) |
| 176 | + .padding() |
| 177 | + } |
| 178 | +} |
0 commit comments