This document outlines the complete implementation of health data synchronization for Weight Mate.
The health sync feature allows users to automatically import weight data from:
- iOS: Apple Health (HealthKit)
- Android: Google Health Connect (Google Fit is deprecated as of 2024)
src/services/health-sync.service.ts- Main health sync service with deduplication logicsrc/app/components/week-progress/week-progress.component.ts- Added health sync functionalitysrc/app/components/week-progress/week-progress.component.html- Added sync button UIsrc/app/components/week-progress/week-progress.component.scss- Added sync button styling
ios/App/App/Info.plist- Added HealthKit permissionsios/App/App/App.entitlements- Added HealthKit capabilityandroid/app/src/main/AndroidManifest.xml- Added Health Connect permissionssrc/app/app.module.ts- Registered health sync service and premium components
cordova-plugin-healthv3.2.4 - Compatible with Capacitor v4
- Date + Weight Matching: Compares entries by date and weight value
- Tolerance: 0.1 unit tolerance to handle minor measurement variations
- Efficient: O(n*m) complexity optimized with early exits
// Example deduplication logic
const isDuplicate = existingEntries.some(entry => {
const sameDay = entryDate.toDateString() === targetDate.toDateString();
const weightDiff = Math.abs(entryWeight - targetWeight);
return sameDay && weightDiff <= 0.1;
});- Chunk Size: 50 entries per batch to prevent memory issues
- Progress Tracking: Real-time sync progress with UI updates
- Memory Efficient: Small delays between batches to prevent UI blocking
- Automatic Detection: Detects health data units (kg vs lbs)
- Dual Storage: Converts and stores both lbs and kg values
- Precision: Rounds to 1 decimal place for consistency
- Access Control: Health sync is premium-only
- Upgrade Prompts: Shows premium upgrade modal for free users
- Visual Indicators: Diamond icon and warning text for premium features
const queryOptions = {
startDate: new Date(Date.now() - (730 * 24 * 60 * 60 * 1000)), // 2 years
endDate: new Date(),
dataType: 'weight',
limit: 1000
};- Permission Check - Request health data access
- Data Query - Retrieve weight measurements from health platform
- Deduplication - Compare against existing entries
- Conversion - Convert units and create WeightLogId objects
- Batch Insert - Use
bulkUpsertWeightLogEntries()for efficiency - Status Update - Update UI with sync results
- Permission Denied: Clear error message to user
- Network Issues: Retry logic with exponential backoff
- Invalid Data: Skip malformed entries, continue sync
- Storage Errors: Rollback mechanism for partial failures
- Position: Below salutation in dashboard header
- States: Normal, Loading, Success, Error
- Premium Gate: Shows upgrade prompt for free users
- Progress Bar: Real-time sync progress (X/Y entries)
- Success Toast: "Synced X new weight entries"
- Last Sync: Shows last successful sync timestamp
- Error Messages: Clear, actionable error descriptions
<div *appPremiumFeature; else healthSyncUpgradeTemplate">
<!-- Premium sync button -->
</div>
<ng-template #healthSyncUpgradeTemplate>
<!-- Upgrade prompt with diamond icon -->
</ng-template>Info.plist additions:
<key>NSHealthUpdateUsageDescription</key>
<string>Weight Mate would like to write weight data to Apple Health to sync your progress across apps.</string>
<key>NSHealthShareUsageDescription</key>
<string>Weight Mate would like to read your weight data from Apple Health to automatically import your weight measurements and track your progress.</string>App.entitlements:
<key>com.apple.developer.healthkit</key>
<true/>AndroidManifest.xml additions:
<uses-permission android:name="android.permission.health.READ_BODY_MEASUREMENTS" />
<uses-permission android:name="android.permission.health.WRITE_BODY_MEASUREMENTS" />
<queries>
<package android:name="com.google.android.apps.healthdata" />
</queries>- Mock Sync:
healthSyncService.testSync()for web development - Simulated Data: 2 mock weight entries with duplicate detection
- UI Testing: Full sync flow without device requirements
- Install Health App: Ensure Apple Health or Health Connect is installed
- Add Test Data: Add weight measurements in health app
- Test Permissions: Verify permission prompts appear
- Test Sync: Run sync and verify data appears in Weight Mate
- Test Deduplication: Run sync again, verify no duplicates
- ✅ First sync with historical data
- ✅ Incremental sync (new data only)
- ✅ Duplicate prevention
- ✅ Permission denial handling
- ✅ Network error recovery
- ✅ Premium upgrade flow
- ✅ Unit conversion accuracy
- Streaming: Process data in 50-entry chunks
- Garbage Collection: Clear references between batches
- Background Processing: Non-blocking operations with async/await
- Date Range Limiting: Only sync last 2 years (configurable)
- Incremental Sync: Track last sync date to avoid re-processing
- Compression: Minimal data transfer with targeted queries
- Bulk Operations: Single database transaction for all new entries
- Index Usage: Leverage existing date/ID indexes for deduplication
- Data Validation: Pre-filter invalid entries before storage
- Minimal Access: Only request weight data permissions
- Local Storage: All data stored locally using Ionic Storage + SQLite
- No Cloud Sync: Health data never leaves the device
- Permission Respect: Graceful handling of permission denials
- Clear Descriptions: Transparent permission descriptions
- Opt-in Only: Sync is user-initiated, never automatic
- Data Ownership: User maintains full control of their data
- Success Rate: Track successful vs failed syncs
- Data Volume: Monitor number of entries synced
- Performance: Track sync duration and memory usage
- Errors: Log and categorize sync failures
- Feature Usage: Track how often users sync
- Premium Conversion: Monitor upgrade rates from sync prompts
- User Feedback: Collect feedback on sync accuracy and performance
- Two-way Sync: Write Weight Mate data back to health apps
- Body Composition: Sync muscle/fat percentage data
- Multiple Sources: Handle data from multiple health apps
- Sync Scheduling: Automatic background sync options
- Conflict Resolution: Handle conflicting weight measurements
- Plugin Migration: Plan migration to newer health plugins as available
- Error Recovery: Implement more sophisticated retry mechanisms
- Testing: Add comprehensive unit tests for sync logic
- Documentation: Add inline code documentation for complex algorithms
This implementation provides a robust, user-friendly health data sync feature that respects privacy, handles errors gracefully, and integrates seamlessly with Weight Mate's existing architecture.