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.
# Decompose Nearby Device Row ## ♻️ Current situation & Problem The `NearbyDeviceRow` was a monolith-style view that encompassed all functionality without a lot of configuration possibilities to vitalizes a nearby device in, e.g., a Bluetooth scanning view. This design is not really flexible if you want to show your own information or want to adjust the layout. Previously you were forced to write your own view completely from scratch. This PR decomposes the view into multiple subview allowing you to build your custom `NearbyDeviceRow`-like experience without reinventing the wheel completely. The new `PeripheralLabel`, `PeripheralSecondaryLabel`, and `InfoButton` views (see StanfordSpezi/SpeziViews#50) allow you to fully customize the appearance of your `NearbyDeviceRow`. ## ⚙️ Release Notes * Decompose the `NearbyDeviceRow` into `PeripheralLabel`, `PeripheralSecondaryLabel`, and `InfoButton` views for greater customizability. ## 📚 Documentation Added new views to the DocC documentation target. ## ✅ Testing The new tests are implicitly tested via the `NearbyDeviceRow`. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
9 changed files
with
180 additions
and
90 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
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,37 @@ | ||
// | ||
// 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 | ||
|
||
|
||
/// The label of a bluetooth peripheral. | ||
public struct PeripheralLabel: View { | ||
private let peripheral: any GenericBluetoothPeripheral | ||
|
||
public var body: some View { | ||
Text(peripheral.label) | ||
.accessibilityLabel(Text(peripheral.accessibilityLabel)) | ||
} | ||
|
||
/// Create a new bluetooth peripheral label. | ||
/// - Parameter peripheral: The peripheral to describe. | ||
public init(_ peripheral: any GenericBluetoothPeripheral) { | ||
self.peripheral = peripheral | ||
} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
List { | ||
PeripheralLabel(MockBluetoothPeripheral(label: "MyDevice 1", state: .connected)) | ||
} | ||
} | ||
#endif |
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
// | ||
// 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 | ||
|
||
|
||
/// A secondary label of a Bluetooth peripheral. | ||
/// | ||
/// The secondary label describes the state of the Bluetooth peripheral (e.g., `connecting` or `connected`). | ||
/// It might be empty in a `disconnected` state. | ||
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: | ||
EmptyView() | ||
case .connected: | ||
Text("Connected", bundle: .module) | ||
case .disconnected: | ||
EmptyView() | ||
} | ||
} | ||
} | ||
.accessibilityRepresentation { | ||
if let localizationSecondaryLabel { | ||
Text(localizationSecondaryLabel) | ||
} | ||
} | ||
} | ||
|
||
/// Create a new secondary peripheral label. | ||
/// - Parameter peripheral: The Bluetooth peripheral. | ||
public 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 |
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
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
Oops, something went wrong.