Successfully converted the nightguard iOS app's Main storyboard and MainViewController from UIKit to SwiftUI.
Location: nightguard/MainView.swift
This file now contains a comprehensive SwiftUI implementation with:
- Published Properties: All UI state including BG values, colors, delta values, care data, error states, etc.
- Timer Management: Periodic updates every 30 seconds
- Data Loading: Integration with NightscoutCacheService for BG data, chart data, and care data
- Alarm Management: Alarm activation state evaluation and snooze functionality
- Reactive Updates: Observes UserDefaults changes and updates UI accordingly
- Complete UI Recreation: All elements from the storyboard recreated in SwiftUI
- BG value display with color coding
- Delta value and trend arrows
- Time and last update information
- Battery, IOB, COB, reservoir information
- Care data (CAGE, SAGE, BAGE)
- Loop data (active profile, temporary basal, temporary target)
- Statistics panel (when enabled)
- Blood glucose chart using SpriteKit
- Error message overlay
- Slide-to-snooze functionality
- Actions menu button
- ChartView: UIViewRepresentable wrapper for SpriteKit ChartScene with gesture support
- BasicStatsPanel: UIViewRepresentable wrapper for existing BasicStatsPanelView
- SlideToSnooze: UIViewRepresentable wrapper for existing SlideToSnoozeView
- NightscoutViewRepresentable: Bridge to existing Nightscout storyboard
- SnoozeAlarmRepresentable: Bridge to snooze alarm functionality
Location: nightguard/AppDelegate.swift
- Import SwiftUI: Added SwiftUI framework import
- Updated didFinishLaunchingWithOptions:
- Creates MainView (SwiftUI)
- Wraps it in UIHostingController
- Replaces the first tab (Main) in TabBarController with SwiftUI view
- Preserves tab bar items and styling
- Maintains compatibility with other UIKit-based tabs
The conversion follows the MVVM (Model-View-ViewModel) pattern:
- Model: Existing data models (NightscoutData, BloodSugar, DeviceStatusData, etc.)
- ViewModel: MainViewModel manages state and business logic
- View: MainView provides declarative UI
The conversion uses a hybrid UIKit/SwiftUI approach:
- SwiftUI: Main tab with MainView
- UIKit: Other tabs remain unchanged (using storyboards)
- Bridges: UIViewRepresentable wrappers for existing UIKit views
- Real-time BG Monitoring: 30-second update interval
- Chart Display: SpriteKit chart with pan/pinch gestures
- Alarm System: Alarm activation and snoozing
- Care Data: CAGE, SAGE, BAGE tracking
- Loop Integration: IOB, COB, temporary basal, temporary target
- Statistics: 24-hour statistics panel
- Error Handling: Network error display
- Settings Integration: Reactive to user preference changes
- Watch Connectivity: Maintains WatchConnectivity integration
- Background Updates: Preserved background task scheduling
- Reactive UI: SwiftUI's declarative syntax with automatic updates
- Better State Management: Centralized in MainViewModel
- Improved Code Organization: Separated concerns (View/ViewModel)
- Type Safety: SwiftUI's type-safe view builders
- Preview Support: Can use SwiftUI previews for development
- No longer using Main.storyboard for MainViewController
- MainViewController.swift (UIKit) is effectively replaced
- All service classes remain unchanged
- All data models remain unchanged
- Other view controllers and storyboards remain unchanged
- Existing UIKit custom views wrapped in UIViewRepresentable
-
Functional Testing:
- BG value display and color coding
- Delta value and trend arrows
- Chart pan/pinch gestures
- Timer-based updates (30-second interval)
- Alarm activation and snoozing
- Care data display (CAGE, SAGE, BAGE)
- Loop data display (IOB, COB, TB, TT)
- Statistics panel visibility toggle
- Error message display
- Nightscout navigation
- Actions menu functionality
-
UI Testing:
- Layout on different device sizes
- Dark mode consistency
- Tab bar integration
- Navigation bar hiding
- Orientation handling (portrait only)
-
Integration Testing:
- Watch connectivity
- Background updates
- User defaults observation
- Apple Health sync
- Alarm sound playback
- Open Xcode: Open
nightguard.xcodeproj - Clean Build Folder: Product → Clean Build Folder (⇧⌘K)
- Build: Product → Build (⌘B)
- Run: Product → Run (⌘R)
- Complete SwiftUI Migration: Convert other view controllers to SwiftUI
- SwiftUI-Native Charts: Replace SpriteKit chart with SwiftUI Charts (iOS 16+)
- Animations: Add SwiftUI animations for state transitions
- Accessibility: Enhance accessibility labels and hints
- Widget Integration: Use SwiftUI for widgets
- Consider removing or archiving MainViewController.swift
- Update documentation to reflect SwiftUI architecture
- Add unit tests for MainViewModel
- Hybrid Architecture: Still uses UIKit TabBarController as root
- UIViewRepresentable Wrappers: Some views still UIKit-based
- Storyboard Dependencies: Other tabs still use storyboards
- iOS Version: Requires iOS 13+ for SwiftUI support
The conversion successfully maintains all original functionality while modernizing the codebase with SwiftUI. The hybrid approach ensures a smooth transition without disrupting other parts of the application.