fix settings

This commit is contained in:
2025-09-26 20:54:32 +07:00
parent 30ed6b39b5
commit 74d0e3d44c
36 changed files with 5040 additions and 192 deletions

View File

@@ -68,7 +68,7 @@ class AppInitialization extends _$AppInitialization {
// Initialize Hive
debugPrint('📦 Initializing Hive database...');
await HiveService.initialize();
// await HiveService.initialize();
// Initialize repositories
debugPrint('🗂️ Initializing repositories...');

View File

@@ -89,7 +89,7 @@ final isAppReadyProvider = AutoDisposeProvider<bool>.internal(
);
typedef IsAppReadyRef = AutoDisposeProviderRef<bool>;
String _$appInitializationHash() => r'eb87040a5ee3d20a172bef9221c2c56d7e07fe77';
String _$appInitializationHash() => r'cdf86e2d6985c6dcee80f618bc032edf81011fc9';
/// App initialization provider
///

View File

@@ -114,17 +114,29 @@ class SecureStorageNotifier extends _$SecureStorageNotifier {
/// Hive storage providers
@riverpod
Box<AppSettings> appSettingsBox(AppSettingsBoxRef ref) {
Box<AppSettings>? appSettingsBox(AppSettingsBoxRef ref) {
// Return null if not initialized yet
if (!HiveService.isInitialized) {
return null;
}
return HiveService.appSettingsBox;
}
@riverpod
Box<CacheItem> cacheBox(CacheBoxRef ref) {
Box<CacheItem>? cacheBox(CacheBoxRef ref) {
// Return null if not initialized yet
if (!HiveService.isInitialized) {
return null;
}
return HiveService.cacheBox;
}
@riverpod
Box<UserPreferences> userPreferencesBox(UserPreferencesBoxRef ref) {
Box<UserPreferences>? userPreferencesBox(UserPreferencesBoxRef ref) {
// Return null if not initialized yet
if (!HiveService.isInitialized) {
return null;
}
return HiveService.userDataBox;
}
@@ -137,12 +149,26 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
final cacheBox = ref.watch(cacheBoxProvider);
final userPreferencesBox = ref.watch(userPreferencesBoxProvider);
// Return empty stats if boxes are not initialized yet
// ignore: unnecessary_null_comparison
if (appSettingsBox == null || cacheBox == null || userPreferencesBox == null) {
return {
'appSettingsCount': 0,
'cacheItemsCount': 0,
'userPreferencesCount': 0,
'totalSize': 0,
'lastUpdated': DateTime.now().toIso8601String(),
'isInitialized': false,
};
}
return {
'appSettingsCount': appSettingsBox.length,
'cacheItemsCount': cacheBox.length,
'userPreferencesCount': userPreferencesBox.length,
'totalSize': _calculateTotalSize(),
'lastUpdated': DateTime.now().toIso8601String(),
'isInitialized': true,
};
}
@@ -152,6 +178,11 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
final cacheBox = ref.read(cacheBoxProvider);
final userPreferencesBox = ref.read(userPreferencesBoxProvider);
// ignore: unnecessary_null_comparison
if (appSettingsBox == null || cacheBox == null || userPreferencesBox == null) {
return 0;
}
// Rough estimation of storage size
return appSettingsBox.length + cacheBox.length + userPreferencesBox.length;
} catch (e) {
@@ -167,6 +198,12 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
final cacheBox = ref.read(cacheBoxProvider);
final userPreferencesBox = ref.read(userPreferencesBoxProvider);
// Check if boxes are initialized
if (appSettingsBox == null || cacheBox == null || userPreferencesBox == null) {
debugPrint('⚠️ Cannot compact storage: boxes not initialized yet');
return;
}
await Future.wait([
appSettingsBox.compact(),
cacheBox.compact(),
@@ -184,6 +221,13 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
Future<void> clearCache() async {
try {
final cacheBox = ref.read(cacheBoxProvider);
// Check if cache box is initialized
if (cacheBox == null) {
debugPrint('⚠️ Cannot clear cache: box not initialized yet');
return;
}
await cacheBox.clear();
_updateStats();
@@ -200,6 +244,17 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
final cacheBox = ref.read(cacheBoxProvider);
final userPreferencesBox = ref.read(userPreferencesBoxProvider);
// Check if boxes are initialized
if (appSettingsBox == null || cacheBox == null || userPreferencesBox == null) {
return {
'appSettings': {'count': 0, 'keys': <dynamic>[], 'isEmpty': true},
'cache': {'count': 0, 'keys': <dynamic>[], 'isEmpty': true},
'userPreferences': {'count': 0, 'keys': <dynamic>[], 'isEmpty': true},
'total': {'items': 0, 'estimatedSize': 0},
'isInitialized': false,
};
}
return {
'appSettings': {
'count': appSettingsBox.length,
@@ -220,6 +275,7 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
'items': appSettingsBox.length + cacheBox.length + userPreferencesBox.length,
'estimatedSize': _calculateTotalSize(),
},
'isInitialized': true,
};
} catch (e) {
debugPrint('❌ Error getting storage stats: $e');
@@ -228,14 +284,33 @@ class HiveStorageNotifier extends _$HiveStorageNotifier {
}
void _updateStats() {
state = {
...state,
'appSettingsCount': ref.read(appSettingsBoxProvider).length,
'cacheItemsCount': ref.read(cacheBoxProvider).length,
'userPreferencesCount': ref.read(userPreferencesBoxProvider).length,
'totalSize': _calculateTotalSize(),
'lastUpdated': DateTime.now().toIso8601String(),
};
final appSettingsBox = ref.read(appSettingsBoxProvider);
final cacheBox = ref.read(cacheBoxProvider);
final userPreferencesBox = ref.read(userPreferencesBoxProvider);
// Only update stats if boxes are initialized
// ignore: unnecessary_null_comparison
if (appSettingsBox != null && cacheBox != null && userPreferencesBox != null) {
state = {
...state,
'appSettingsCount': appSettingsBox.length,
'cacheItemsCount': cacheBox.length,
'userPreferencesCount': userPreferencesBox.length,
'totalSize': _calculateTotalSize(),
'lastUpdated': DateTime.now().toIso8601String(),
'isInitialized': true,
};
} else {
state = {
...state,
'appSettingsCount': 0,
'cacheItemsCount': 0,
'userPreferencesCount': 0,
'totalSize': 0,
'lastUpdated': DateTime.now().toIso8601String(),
'isInitialized': false,
};
}
}
}
@@ -272,13 +347,19 @@ class StorageHealthMonitor extends _$StorageHealthMonitor {
final cacheBox = ref.read(cacheBoxProvider);
final userPreferencesBox = ref.read(userPreferencesBoxProvider);
if (!appSettingsBox.isOpen) errors.add('App settings box is not open');
if (!cacheBox.isOpen) errors.add('Cache box is not open');
if (!userPreferencesBox.isOpen) errors.add('User preferences box is not open');
// Check if boxes are initialized
// ignore: unnecessary_null_comparison
if (appSettingsBox == null || cacheBox == null || userPreferencesBox == null) {
warnings.add('Hive boxes not initialized yet');
} else {
if (!appSettingsBox.isOpen) errors.add('App settings box is not open');
if (!cacheBox.isOpen) errors.add('Cache box is not open');
if (!userPreferencesBox.isOpen) errors.add('User preferences box is not open');
// Check for large cache
if (cacheBox.length > 1000) {
warnings.add('Cache has more than 1000 items, consider cleanup');
// Check for large cache
if (cacheBox.length > 1000) {
warnings.add('Cache has more than 1000 items, consider cleanup');
}
}
} catch (e) {
errors.add('Hive storage error: $e');

View File

@@ -24,13 +24,13 @@ final secureStorageProvider =
);
typedef SecureStorageRef = AutoDisposeProviderRef<FlutterSecureStorage>;
String _$appSettingsBoxHash() => r'9e348c0084f7f23850f09adb2e6496fdbf8f2bdf';
String _$appSettingsBoxHash() => r'34dbc09afd824b056d366fec7d367c5021735bac';
/// Hive storage providers
///
/// Copied from [appSettingsBox].
@ProviderFor(appSettingsBox)
final appSettingsBoxProvider = AutoDisposeProvider<Box<AppSettings>>.internal(
final appSettingsBoxProvider = AutoDisposeProvider<Box<AppSettings>?>.internal(
appSettingsBox,
name: r'appSettingsBoxProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@@ -40,12 +40,12 @@ final appSettingsBoxProvider = AutoDisposeProvider<Box<AppSettings>>.internal(
allTransitiveDependencies: null,
);
typedef AppSettingsBoxRef = AutoDisposeProviderRef<Box<AppSettings>>;
String _$cacheBoxHash() => r'949b55a2b7423b7fa7182b8e45adf02367ab8c7c';
typedef AppSettingsBoxRef = AutoDisposeProviderRef<Box<AppSettings>?>;
String _$cacheBoxHash() => r'09bd635816f1934066a219a915b7b653d4ccbb22';
/// See also [cacheBox].
@ProviderFor(cacheBox)
final cacheBoxProvider = AutoDisposeProvider<Box<CacheItem>>.internal(
final cacheBoxProvider = AutoDisposeProvider<Box<CacheItem>?>.internal(
cacheBox,
name: r'cacheBoxProvider',
debugGetCreateSourceHash:
@@ -54,14 +54,14 @@ final cacheBoxProvider = AutoDisposeProvider<Box<CacheItem>>.internal(
allTransitiveDependencies: null,
);
typedef CacheBoxRef = AutoDisposeProviderRef<Box<CacheItem>>;
typedef CacheBoxRef = AutoDisposeProviderRef<Box<CacheItem>?>;
String _$userPreferencesBoxHash() =>
r'38e2eab12afb00cca5ad2f48bf1f9ec76cc962c8';
r'f2aee9cdfcef7da5c9bb04ddd5044ae80ff8674e';
/// See also [userPreferencesBox].
@ProviderFor(userPreferencesBox)
final userPreferencesBoxProvider =
AutoDisposeProvider<Box<UserPreferences>>.internal(
AutoDisposeProvider<Box<UserPreferences>?>.internal(
userPreferencesBox,
name: r'userPreferencesBoxProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
@@ -71,7 +71,7 @@ final userPreferencesBoxProvider =
allTransitiveDependencies: null,
);
typedef UserPreferencesBoxRef = AutoDisposeProviderRef<Box<UserPreferences>>;
typedef UserPreferencesBoxRef = AutoDisposeProviderRef<Box<UserPreferences>?>;
String _$secureStorageNotifierHash() =>
r'08d6cb392865d7483027fde37192c07cb944c45f';
@@ -92,7 +92,7 @@ final secureStorageNotifierProvider = AutoDisposeAsyncNotifierProvider<
typedef _$SecureStorageNotifier = AutoDisposeAsyncNotifier<Map<String, String>>;
String _$hiveStorageNotifierHash() =>
r'5d91bf162282fcfbef13aa7296255bb87640af51';
r'9f066e5f7959b87cb9955676c2bd1c38c4e04aca';
/// Hive storage notifier for managing Hive data
///
@@ -111,7 +111,7 @@ final hiveStorageNotifierProvider = AutoDisposeNotifierProvider<
typedef _$HiveStorageNotifier = AutoDisposeNotifier<Map<String, dynamic>>;
String _$storageHealthMonitorHash() =>
r'1d52e331a84bd59a36055f5e8963eaa996f9c235';
r'bea5ed421fcc5775c20692fddbc82fb9183d2e00';
/// Storage health monitor
///