Code review improvements: concurrency, tests, bug fixes - #2
Open
WolfTheDesigner wants to merge 10 commits into
Open
Code review improvements: concurrency, tests, bug fixes#2WolfTheDesigner wants to merge 10 commits into
WolfTheDesigner wants to merge 10 commits into
Conversation
Replace nonisolated(unsafe) global CBUUID variables with BLEUUIDs enum containing static properties for better encapsulation and namespacing.
…gic numbers, fix MainActor.assumeIsolated - Remove isLocked toggle from startScan() — lock state now only changes on explicit user command - Replace 5 global @mainactor variables with BLEConnectionState singleton class - Replace all print() calls with structured os.Logger (info/debug/error/warning levels) - Extract magic numbers into named constants (scanTimeoutInterval, keyboardScrollOffset) - Replace brittle MainActor.assumeIsolated with safe Task { @mainactor in }
Provides a clear diagnostic crash message if the view controller is instantiated without setting the peripheral property.
Documents the intent of full strict concurrency checking, which is already the default for Swift 6.0 language mode.
Add BadBirdTests target with 8 tests across 3 suites: - BLEUUIDTests: verify Nordic UART service/tx/rx UUIDs - Mi365CommandTests: verify lock/unlock byte sequences and diff - BLEConnectionStateTests: verify singleton and default state
…rialize stateful tests
- Move updateIncomingData() from viewDidLoad to viewDidAppear
to re-register observer after viewDidDisappear removes it
- Remove Task { @mainactor in } wrapper since callback already
runs on main queue (queue: .main)
- Add .serialized trait to BLEConnectionStateTests to prevent
data races on shared singleton in parallel test execution
…story bug - Remove lock/unlock command from outgoingData() — text send should not toggle scooter lock state - Update BLEConnectionState.isLocked in switchAction to keep state in sync with physical switch position - Clear consoleAsciiText in viewDidAppear to prevent stale history reappearing after navigation
- Remove writeCharacteristic() calls from switchAction — sending a legacy 1-byte value after the 9-byte Mi365 command corrupts the BLE payload - Guard against duplicate notification observers by removing existing observer before registering new one in updateIncomingData - Clear rssiValues alongside peripherals in startScan() to prevent array index mismatch and potential crash
Revert outgoingData() to send lock/unlock commands on Send press and toggle isLocked state, matching original app behavior. Restore writeCharacteristic() calls in switchAction to preserve the dual-write pattern expected by the scooter firmware.
…, add MainActor isolation - Clear BLE state (peripheral, characteristics) on disconnect to prevent memory leak in BLEConnectionState singleton - Add error check in didUpdateValueFor to avoid processing stale data - Use MainActor.assumeIsolated for notification closure (queue: .main guarantees main thread execution) - Fix tautological test — verify fresh instance default instead of set-then-check pattern - Make BLEConnectionState.init internal for testability
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BLEUUIDsenum namespace, removingnonisolated(unsafe)globalsBLEConnectionStatesingleton with@MainActorisolationisLockedtoggle bug instartScan()— lock state no longer changes on scanprint()with structuredos.Logger(info/debug/error/warning levels)peripheralpropertySWIFT_STRICT_CONCURRENCY = completeto build settingsdidUpdateValueForcharacteristic callbackMainActor.assumeIsolatedfor notification closure isolationOriginal behavior preserved
All lock/unlock flows, BLE write patterns, and UI interactions from cb7bb12 are unchanged.
Test plan