generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
public struct ListInfoButton: View { // TODO: move to SpeziViews! | ||
private let label: Text | ||
private let action: () -> Void | ||
|
||
public var body: some View { | ||
Button(action: action) { | ||
Label { | ||
label | ||
} icon: { | ||
Image(systemName: "info.circle") // swiftlint:disable:this accessibility_label_for_image | ||
} | ||
} | ||
.labelStyle(.iconOnly) | ||
.font(.title3) | ||
.foregroundColor(.accentColor) | ||
.buttonStyle(.plain) // ensure button is clickable next to the other button | ||
.accessibilityAction(named: label, action) | ||
#if TEST || targetEnvironment(simulator) | ||
.accessibilityHidden(true) // accessibility actions cannot be unit tested | ||
#endif | ||
} | ||
|
||
public init(_ label: Text, action: @escaping () -> Void) { | ||
self.label = label | ||
self.action = action | ||
} | ||
|
||
public init(_ resource: LocalizedStringResource, action: @escaping () -> Void) { | ||
self.label = Text(resource) | ||
self.action = action | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SpeziBluetooth | ||
@_spi(TestingSupport) import SpeziDevices | ||
import SwiftUI | ||
|
||
|
||
public struct PeripheralLabel: View { | ||
private let peripheral: any GenericBluetoothPeripheral | ||
|
||
public var body: some View { | ||
Text(peripheral.label) | ||
.accessibilityLabel(Text(peripheral.accessibilityLabel)) | ||
} | ||
|
||
init(_ peripheral: any GenericBluetoothPeripheral) { | ||
self.peripheral = peripheral | ||
} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
List { | ||
PeripheralLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .connected)) | ||
} | ||
} | ||
#endif |
72 changes: 72 additions & 0 deletions
72
Sources/SpeziDevicesUI/Scanning/PeripheralSecondaryLabel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SpeziBluetooth | ||
@_spi(TestingSupport) import SpeziDevices | ||
import SwiftUI | ||
|
||
|
||
public struct PeripheralSecondaryLabel: View { | ||
private let peripheral: any GenericBluetoothPeripheral | ||
|
||
private var localizationSecondaryLabel: LocalizedStringResource? { | ||
if peripheral.requiresUserAttention { | ||
return .init("Intervention Required", bundle: .atURL(from: .module)) | ||
} | ||
switch peripheral.state { | ||
case .connecting: | ||
return .init("Connecting", bundle: .atURL(from: .module)) | ||
case .connected: | ||
return .init("Connected", bundle: .atURL(from: .module)) | ||
case .disconnecting: | ||
return .init("Disconnecting", bundle: .atURL(from: .module)) | ||
case .disconnected: | ||
return nil | ||
} | ||
} | ||
|
||
public var body: some View { | ||
Group { | ||
if peripheral.requiresUserAttention { | ||
Text("Requires Attention", bundle: .module) | ||
} else { | ||
switch peripheral.state { | ||
case .connecting, .disconnecting: | ||
ProgressView() | ||
.accessibilityRemoveTraits(.updatesFrequently) | ||
case .connected: | ||
Text("Connected", bundle: .module) | ||
case .disconnected: | ||
EmptyView() | ||
} | ||
} | ||
} | ||
.accessibilityRepresentation { | ||
if let localizationSecondaryLabel { | ||
Text(localizationSecondaryLabel) | ||
} | ||
} | ||
} | ||
|
||
init(_ peripheral: any GenericBluetoothPeripheral) { | ||
self.peripheral = peripheral | ||
} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
List { | ||
PeripheralSecondaryLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .connecting)) | ||
PeripheralSecondaryLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .connected)) | ||
PeripheralSecondaryLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .connected, requiresUserAttention: true)) | ||
PeripheralSecondaryLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .disconnecting)) | ||
PeripheralSecondaryLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .disconnected)) | ||
} | ||
} | ||
#endif |