Skip to content

Code review improvements: concurrency, tests, bug fixes - #2

Open
WolfTheDesigner wants to merge 10 commits into
masterfrom
fix/review-improvements
Open

Code review improvements: concurrency, tests, bug fixes#2
WolfTheDesigner wants to merge 10 commits into
masterfrom
fix/review-improvements

Conversation

@WolfTheDesigner

Copy link
Copy Markdown
Owner

Summary

  • Refactor BLE UUIDs into BLEUUIDs enum namespace, removing nonisolated(unsafe) globals
  • Encapsulate 5 global mutable variables into BLEConnectionState singleton with @MainActor isolation
  • Fix isLocked toggle bug in startScan() — lock state no longer changes on scan
  • Replace all print() with structured os.Logger (info/debug/error/warning levels)
  • Extract magic numbers into named constants
  • Add guard for force-unwrapped peripheral property
  • Add SWIFT_STRICT_CONCURRENCY = complete to build settings
  • Add Swift Testing test target with 8 tests across 3 suites
  • Fix notification observer lifecycle (viewDidAppear/viewDidDisappear)
  • Fix memory leak — clear BLE state on disconnect
  • Add error handling in didUpdateValueFor characteristic callback
  • Add MainActor.assumeIsolated for notification closure isolation

Original behavior preserved

All lock/unlock flows, BLE write patterns, and UI interactions from cb7bb12 are unchanged.

Test plan

  • Build succeeds with zero warnings (Swift 6.0, strict concurrency complete)
  • 8/8 Swift Testing tests pass
  • Verify BLE scan and connection on physical device
  • Verify lock/unlock via UISwitch works correctly
  • Verify Send button sends lock/unlock command

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant