rastrillo / native Public

Clone
git clone https://amadan.net/rastrillo/native

Plain git — no account needed to clone.

Download

Download this file

1import RastrilloNative
2import SwiftUI
3
4/// A thin inset bar shown while `origin` is offline. Entering the offline
5/// state is announced to assistive technologies; leaving it is not.
6///
7/// Pass `summary` once the app's edits are kept in an `Outbox`: the banner
8/// then says they are saved on this device, which is only true from then on.
9public struct NativeOfflineBanner: View {
10 private let connectivity: NativeConnectivity
11 private let origin: URL
12 private let message: String
13
14 @MainActor public init(connectivity: NativeConnectivity? = nil, origin: URL, message: String? = nil) {
15 self.connectivity = connectivity ?? .shared; self.origin = origin
16 self.message = message ?? NativeSyncStrings.kit.offlineWaiting
17 }
18
19 /// The durable wording, for an app whose changes go through an outbox.
20 @MainActor public init(connectivity: NativeConnectivity? = nil, origin: URL, summary: OutboxSummary,
21 strings: NativeSyncStrings = .kit) {
22 self.init(connectivity: connectivity, origin: origin, message: strings.offlineDurable)
23 }
24
25 public var body: some View {
26 let offline = connectivity.isOffline(origin)
27 Group {
28 if offline {
29 Label(message, systemImage: "wifi.slash")
30 .font(.callout)
31 .frame(maxWidth: .infinity)
32 .padding(.horizontal, 12).padding(.vertical, 6)
33 .background(.bar)
34 .accessibilityElement(children: .combine)
35 }
36 }
37 .onChange(of: offline) { _, now in
38 if now { AccessibilityNotification.Announcement(message).post() }
39 }
40 }
41}
42