add favorite

This commit is contained in:
Phuoc Nguyen
2025-11-18 11:23:07 +07:00
parent 192c322816
commit a5eb95fa64
25 changed files with 2506 additions and 978 deletions

View File

@@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:worker/core/database/database_manager.dart';
import 'package:worker/core/database/hive_service.dart';
@@ -53,6 +54,9 @@ class HiveInitializer {
final dbManager = DatabaseManager();
// Migration: Delete old favoriteBox (deprecated, replaced with favoriteProductsBox)
await _deleteLegacyFavoriteBox(verbose);
// Clear expired cache on app start
await dbManager.clearExpiredCache();
@@ -97,6 +101,33 @@ class HiveInitializer {
await hiveService.clearUserData();
}
/// Delete legacy favoriteBox (migration helper)
///
/// The old favoriteBox stored FavoriteModel which has been removed.
/// This method deletes the old box to prevent typeId errors.
static Future<void> _deleteLegacyFavoriteBox(bool verbose) async {
try {
const legacyBoxName = 'favorite_box';
// Check if the old box exists
if (await Hive.boxExists(legacyBoxName)) {
if (verbose) {
debugPrint('HiveInitializer: Deleting legacy favoriteBox...');
}
// Delete the box from disk
await Hive.deleteBoxFromDisk(legacyBoxName);
if (verbose) {
debugPrint('HiveInitializer: Legacy favoriteBox deleted successfully');
}
}
} catch (e) {
debugPrint('HiveInitializer: Error deleting legacy favoriteBox: $e');
// Don't rethrow - this is just a cleanup operation
}
}
/// Get database statistics
///
/// Returns statistics about all Hive boxes.

View File

@@ -156,8 +156,8 @@ class HiveService {
// Notification box (non-sensitive)
Hive.openBox<dynamic>(HiveBoxNames.notificationBox),
// Favorites box (non-sensitive)
Hive.openBox<dynamic>(HiveBoxNames.favoriteBox),
// Favorite products box (non-sensitive) - caches Product entities from wishlist API
Hive.openBox<dynamic>(HiveBoxNames.favoriteProductsBox),
]);
// Open potentially encrypted boxes (sensitive data)