Add real-time inventory syncing across devices v1.1.0

🔄 Inventory Sync Features:
- Automatic sync to Firebase when adding/updating/deleting items
- Real-time listener pulls changes from other devices
- Bi-directional sync keeps all household devices in sync
- Conflict resolution based on lastModified timestamp
- Firebase version always wins on conflicts

📱 How It Works:
Device A adds item → syncs to Firebase → Device B receives update
Device B updates item → syncs to Firebase → Device A receives update
Device A deletes item → syncs to Firebase → Device B removes item

🔧 Technical Implementation:
- InventorySyncService: Real-time Firestore listener
- Repository hooks: add/update/delete sync to Firebase
- HomeScreen lifecycle: starts/stops sync automatically
- Conflict resolution: newer timestamp wins
- Local Hive + Cloud Firestore hybrid architecture

📁 New Files:
- lib/features/household/services/inventory_sync_service.dart

 Updated Files:
- lib/features/inventory/repositories/inventory_repository_impl.dart
  - Added Firebase sync on add/update/delete operations
  - Maintains local Hive for offline access
- lib/features/home/screens/home_screen.dart
  - Starts sync service on init if in household
  - Stops sync service on dispose

⚠️ Requirements:
- Firebase must be configured (see FIREBASE_SETUP.md)
- Internet connection required for cross-device sync
- Local Hive works offline, syncs when online

 Build Status:
- APK: 63.4MB
- Package: com.github.mystiatech.sage
- Version: 1.1.0+2

🎯 Next Steps for User:
1. Set up Firebase (FIREBASE_SETUP.md)
2. Replace google-services.json with real file
3. Rebuild APK
4. Install on both devices
5. Create/join household
6. Add items → they sync! 🎉

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-04 15:47:53 -04:00
parent 6c29751e49
commit 3388f24eb4
4 changed files with 185 additions and 8 deletions

View File

@@ -1,11 +1,13 @@
import 'package:hive/hive.dart';
import '../../../data/local/hive_database.dart';
import '../../settings/models/app_settings.dart';
import '../../household/services/firebase_household_service.dart';
import '../models/food_item.dart';
import 'inventory_repository.dart';
/// Hive implementation of InventoryRepository
/// Hive implementation of InventoryRepository with Firebase sync
class InventoryRepositoryImpl implements InventoryRepository {
final _firebaseService = FirebaseHouseholdService();
Future<Box<FoodItem>> get _box async => await HiveDatabase.getFoodBox();
/// Get the current household ID from settings
@@ -47,17 +49,57 @@ class InventoryRepositoryImpl implements InventoryRepository {
final box = await _box;
item.lastModified = DateTime.now();
await box.add(item);
// Sync to Firebase if in a household
if (item.householdId != null && item.key != null) {
try {
await _firebaseService.addFoodItem(
item.householdId!,
item,
item.key.toString(),
);
} catch (e) {
print('Failed to sync item to Firebase: $e');
}
}
}
@override
Future<void> updateItem(FoodItem item) async {
item.lastModified = DateTime.now();
await item.save();
// Sync to Firebase if in a household
if (item.householdId != null && item.key != null) {
try {
await _firebaseService.updateFoodItem(
item.householdId!,
item,
item.key.toString(),
);
} catch (e) {
print('Failed to sync item update to Firebase: $e');
}
}
}
@override
Future<void> deleteItem(int id) async {
final box = await _box;
final item = box.get(id);
// Sync deletion to Firebase if in a household
if (item != null && item.householdId != null) {
try {
await _firebaseService.deleteFoodItem(
item.householdId!,
id.toString(),
);
} catch (e) {
print('Failed to sync item deletion to Firebase: $e');
}
}
await box.delete(id);
}