fix settings
This commit is contained in:
@@ -1,9 +1,23 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../hive_service.dart';
|
||||
import '../models/cache_item.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
/// Repository for managing cached data using Hive
|
||||
class CacheRepository {
|
||||
/// Safe getter for cache box - returns null if not initialized
|
||||
Box<CacheItem>? get _cacheBox {
|
||||
if (!HiveService.isInitialized) {
|
||||
debugPrint('⚠️ CacheRepository: Hive not initialized yet');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return HiveService.cacheBox;
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error accessing cache box: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// Store data in cache with expiration
|
||||
Future<void> put<T>({
|
||||
required String key,
|
||||
@@ -12,7 +26,12 @@ class CacheRepository {
|
||||
Map<String, dynamic>? metadata,
|
||||
}) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot store cache item: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
final cacheItem = CacheItem.create(
|
||||
key: key,
|
||||
data: data,
|
||||
@@ -35,7 +54,12 @@ class CacheRepository {
|
||||
Map<String, dynamic>? metadata,
|
||||
}) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot store permanent cache item: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
final cacheItem = CacheItem.permanent(
|
||||
key: key,
|
||||
data: data,
|
||||
@@ -53,7 +77,12 @@ class CacheRepository {
|
||||
/// Get data from cache
|
||||
T? get<T>(String key) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot get cache item: Hive not initialized');
|
||||
return null;
|
||||
}
|
||||
|
||||
final cacheItem = box.get(key);
|
||||
|
||||
if (cacheItem == null) {
|
||||
@@ -79,7 +108,12 @@ class CacheRepository {
|
||||
/// Get cache item with full metadata
|
||||
CacheItem? getCacheItem(String key) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot get cache item: Hive not initialized');
|
||||
return null;
|
||||
}
|
||||
|
||||
final cacheItem = box.get(key);
|
||||
|
||||
if (cacheItem == null) {
|
||||
@@ -102,7 +136,11 @@ class CacheRepository {
|
||||
/// Check if key exists and is valid (not expired)
|
||||
bool contains(String key) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
final cacheItem = box.get(key);
|
||||
|
||||
if (cacheItem == null) return false;
|
||||
@@ -121,7 +159,11 @@ class CacheRepository {
|
||||
/// Check if key exists regardless of expiration
|
||||
bool containsKey(String key) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
return box.containsKey(key);
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error checking key $key: $e');
|
||||
@@ -132,7 +174,11 @@ class CacheRepository {
|
||||
/// Delete specific cache item
|
||||
Future<void> delete(String key) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.delete(key);
|
||||
debugPrint('🗑️ Cache item deleted: $key');
|
||||
} catch (e) {
|
||||
@@ -143,7 +189,11 @@ class CacheRepository {
|
||||
/// Delete multiple cache items
|
||||
Future<void> deleteMultiple(List<String> keys) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
for (final key in keys) {
|
||||
await box.delete(key);
|
||||
}
|
||||
@@ -161,7 +211,11 @@ class CacheRepository {
|
||||
/// Clear all expired items
|
||||
Future<int> clearExpired() async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return 0;
|
||||
}
|
||||
final expiredKeys = <String>[];
|
||||
final now = DateTime.now();
|
||||
|
||||
@@ -187,7 +241,11 @@ class CacheRepository {
|
||||
/// Clear all cache items
|
||||
Future<void> clearAll() async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
final count = box.length;
|
||||
await box.clear();
|
||||
debugPrint('🧹 Cleared all cache items: $count items');
|
||||
@@ -200,7 +258,11 @@ class CacheRepository {
|
||||
/// Clear cache items by pattern
|
||||
Future<int> clearByPattern(Pattern pattern) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return 0;
|
||||
}
|
||||
final keysToDelete = <String>[];
|
||||
|
||||
for (final key in box.keys) {
|
||||
@@ -224,7 +286,11 @@ class CacheRepository {
|
||||
/// Clear cache items by type
|
||||
Future<int> clearByType(String dataType) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return 0;
|
||||
}
|
||||
final keysToDelete = <String>[];
|
||||
|
||||
for (final key in box.keys) {
|
||||
@@ -249,7 +315,11 @@ class CacheRepository {
|
||||
/// Refresh cache item with new expiration
|
||||
Future<bool> refresh(String key, Duration newExpirationDuration) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
final cacheItem = box.get(key);
|
||||
|
||||
if (cacheItem == null) return false;
|
||||
@@ -268,7 +338,11 @@ class CacheRepository {
|
||||
/// Update cache item data
|
||||
Future<bool> update<T>(String key, T newData, {Duration? newExpirationDuration}) async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
final cacheItem = box.get(key);
|
||||
|
||||
if (cacheItem == null) return false;
|
||||
@@ -287,7 +361,11 @@ class CacheRepository {
|
||||
/// Get all keys in cache
|
||||
List<String> getAllKeys() {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return [];
|
||||
}
|
||||
return box.keys.cast<String>().toList();
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error getting all keys: $e');
|
||||
@@ -298,7 +376,11 @@ class CacheRepository {
|
||||
/// Get keys by pattern
|
||||
List<String> getKeysByPattern(Pattern pattern) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return [];
|
||||
}
|
||||
return box.keys
|
||||
.cast<String>()
|
||||
.where((key) => key.contains(pattern))
|
||||
@@ -312,7 +394,11 @@ class CacheRepository {
|
||||
/// Get keys by data type
|
||||
List<String> getKeysByType(String dataType) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return [];
|
||||
}
|
||||
final keys = <String>[];
|
||||
|
||||
for (final key in box.keys) {
|
||||
@@ -332,7 +418,18 @@ class CacheRepository {
|
||||
/// Get cache statistics
|
||||
CacheStats getStats() {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return CacheStats(
|
||||
totalItems: 0,
|
||||
validItems: 0,
|
||||
expiredItems: 0,
|
||||
oldestItem: DateTime.now(),
|
||||
newestItem: DateTime.now(),
|
||||
typeCount: const {},
|
||||
);
|
||||
}
|
||||
final now = DateTime.now();
|
||||
var validItems = 0;
|
||||
var expiredItems = 0;
|
||||
@@ -387,7 +484,11 @@ class CacheRepository {
|
||||
/// Get cache size in bytes (approximate)
|
||||
int getApproximateSize() {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return 0;
|
||||
}
|
||||
// This is an approximation as Hive doesn't provide exact size
|
||||
return box.length * 1024; // Assume average 1KB per item
|
||||
} catch (e) {
|
||||
@@ -399,7 +500,11 @@ class CacheRepository {
|
||||
/// Compact cache storage
|
||||
Future<void> compact() async {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.compact();
|
||||
debugPrint('✅ Cache storage compacted');
|
||||
} catch (e) {
|
||||
@@ -410,7 +515,11 @@ class CacheRepository {
|
||||
/// Export cache data (for debugging or backup)
|
||||
Map<String, dynamic> exportCache({bool includeExpired = false}) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return {};
|
||||
}
|
||||
final now = DateTime.now();
|
||||
final exportData = <String, dynamic>{};
|
||||
|
||||
@@ -435,7 +544,11 @@ class CacheRepository {
|
||||
/// Watch cache changes for a specific key
|
||||
Stream<CacheItem?> watch(String key) {
|
||||
try {
|
||||
final box = HiveService.cacheBox;
|
||||
final box = _cacheBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access cache: Hive not initialized');
|
||||
return Stream.value(null);
|
||||
}
|
||||
return box.watch(key: key).map((event) => event.value as CacheItem?);
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error watching cache key $key: $e');
|
||||
|
||||
@@ -1,15 +1,33 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../hive_service.dart';
|
||||
import '../models/app_settings.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
/// Repository for managing application settings using Hive
|
||||
class SettingsRepository {
|
||||
/// Safe getter for app settings box - returns null if not initialized
|
||||
Box<AppSettings>? get _settingsBox {
|
||||
if (!HiveService.isInitialized) {
|
||||
debugPrint('⚠️ SettingsRepository: Hive not initialized yet');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return HiveService.appSettingsBox;
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error accessing settings box: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
static const String _defaultKey = 'app_settings';
|
||||
|
||||
/// Get the current app settings
|
||||
AppSettings getSettings() {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return AppSettings.defaultSettings();
|
||||
}
|
||||
final settings = box.get(_defaultKey);
|
||||
|
||||
if (settings == null) {
|
||||
@@ -39,7 +57,11 @@ class SettingsRepository {
|
||||
/// Save app settings
|
||||
Future<void> saveSettings(AppSettings settings) async {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
final updatedSettings = settings.copyWith(lastUpdated: DateTime.now());
|
||||
await box.put(_defaultKey, updatedSettings);
|
||||
debugPrint('✅ Settings saved successfully');
|
||||
@@ -153,7 +175,11 @@ class SettingsRepository {
|
||||
/// Check if settings exist
|
||||
bool hasSettings() {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
return box.containsKey(_defaultKey);
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error checking settings existence: $e');
|
||||
@@ -164,7 +190,11 @@ class SettingsRepository {
|
||||
/// Clear all settings (use with caution)
|
||||
Future<void> clearSettings() async {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.delete(_defaultKey);
|
||||
debugPrint('✅ Settings cleared');
|
||||
} catch (e) {
|
||||
@@ -177,7 +207,11 @@ class SettingsRepository {
|
||||
Map<String, dynamic> getSettingsStats() {
|
||||
try {
|
||||
final settings = getSettings();
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
'hasCustomSettings': settings.customSettings?.isNotEmpty ?? false,
|
||||
@@ -225,7 +259,11 @@ class SettingsRepository {
|
||||
/// Watch settings changes
|
||||
Stream<AppSettings> watchSettings() {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return Stream.value(AppSettings.defaultSettings());
|
||||
}
|
||||
return box.watch(key: _defaultKey).map((event) {
|
||||
final settings = event.value as AppSettings?;
|
||||
return settings ?? AppSettings.defaultSettings();
|
||||
@@ -239,7 +277,11 @@ class SettingsRepository {
|
||||
/// Compact settings storage
|
||||
Future<void> compact() async {
|
||||
try {
|
||||
final box = HiveService.appSettingsBox;
|
||||
final box = _settingsBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access settings: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.compact();
|
||||
debugPrint('✅ Settings storage compacted');
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,9 +1,23 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../hive_service.dart';
|
||||
import '../models/user_preferences.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
/// Repository for managing user preferences using Hive
|
||||
class UserPreferencesRepository {
|
||||
/// Safe getter for user preferences box - returns null if not initialized
|
||||
Box<UserPreferences>? get _userPreferencesBox {
|
||||
if (!HiveService.isInitialized) {
|
||||
debugPrint('⚠️ UserPreferencesRepository: Hive not initialized yet');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return HiveService.userDataBox;
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error accessing user preferences box: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
static const String _defaultKey = 'current_user_preferences';
|
||||
|
||||
/// Get the current user preferences (alias for getUserPreferences)
|
||||
@@ -14,7 +28,11 @@ class UserPreferencesRepository {
|
||||
/// Get the current user preferences
|
||||
UserPreferences? getUserPreferences([String? userId]) {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return null;
|
||||
}
|
||||
final key = userId ?? _defaultKey;
|
||||
|
||||
final preferences = box.get(key);
|
||||
@@ -38,7 +56,11 @@ class UserPreferencesRepository {
|
||||
/// Save user preferences
|
||||
Future<void> saveUserPreferences(UserPreferences preferences, [String? userId]) async {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
final key = userId ?? _defaultKey;
|
||||
|
||||
final updatedPreferences = preferences.copyWith(lastUpdated: DateTime.now());
|
||||
@@ -212,7 +234,11 @@ class UserPreferencesRepository {
|
||||
/// Check if user preferences exist
|
||||
bool hasUserPreferences([String? userId]) {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return false;
|
||||
}
|
||||
final key = userId ?? _defaultKey;
|
||||
return box.containsKey(key);
|
||||
} catch (e) {
|
||||
@@ -224,7 +250,11 @@ class UserPreferencesRepository {
|
||||
/// Clear user preferences (use with caution)
|
||||
Future<void> clearUserPreferences([String? userId]) async {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
final key = userId ?? _defaultKey;
|
||||
await box.delete(key);
|
||||
debugPrint('✅ User preferences cleared for key: $key');
|
||||
@@ -237,7 +267,11 @@ class UserPreferencesRepository {
|
||||
/// Get all user IDs that have preferences stored
|
||||
List<String> getAllUserIds() {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return [];
|
||||
}
|
||||
return box.keys.cast<String>().where((key) => key != _defaultKey).toList();
|
||||
} catch (e) {
|
||||
debugPrint('❌ Error getting all user IDs: $e');
|
||||
@@ -248,7 +282,11 @@ class UserPreferencesRepository {
|
||||
/// Delete preferences for a specific user
|
||||
Future<void> deleteUserPreferences(String userId) async {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.delete(userId);
|
||||
debugPrint('✅ User preferences deleted for user: $userId');
|
||||
} catch (e) {
|
||||
@@ -289,7 +327,11 @@ class UserPreferencesRepository {
|
||||
/// Watch user preferences changes
|
||||
Stream<UserPreferences?> watchUserPreferences([String? userId]) {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return Stream.value(null);
|
||||
}
|
||||
final key = userId ?? _defaultKey;
|
||||
return box.watch(key: key).map((event) => event.value as UserPreferences?);
|
||||
} catch (e) {
|
||||
@@ -301,7 +343,11 @@ class UserPreferencesRepository {
|
||||
/// Compact user preferences storage
|
||||
Future<void> compact() async {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return;
|
||||
}
|
||||
await box.compact();
|
||||
debugPrint('✅ User preferences storage compacted');
|
||||
} catch (e) {
|
||||
@@ -312,7 +358,11 @@ class UserPreferencesRepository {
|
||||
/// Get storage statistics
|
||||
Map<String, dynamic> getStorageStats() {
|
||||
try {
|
||||
final box = HiveService.userDataBox;
|
||||
final box = _userPreferencesBox;
|
||||
if (box == null) {
|
||||
debugPrint('⚠️ Cannot access user preferences: Hive not initialized');
|
||||
return {};
|
||||
}
|
||||
final allUserIds = getAllUserIds();
|
||||
|
||||
return {
|
||||
|
||||
@@ -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...');
|
||||
|
||||
@@ -89,7 +89,7 @@ final isAppReadyProvider = AutoDisposeProvider<bool>.internal(
|
||||
);
|
||||
|
||||
typedef IsAppReadyRef = AutoDisposeProviderRef<bool>;
|
||||
String _$appInitializationHash() => r'eb87040a5ee3d20a172bef9221c2c56d7e07fe77';
|
||||
String _$appInitializationHash() => r'cdf86e2d6985c6dcee80f618bc032edf81011fc9';
|
||||
|
||||
/// App initialization provider
|
||||
///
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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
|
||||
///
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'route_names.dart';
|
||||
import 'route_paths.dart';
|
||||
import 'route_guards.dart';
|
||||
import 'error_page.dart';
|
||||
import '../../features/auth/presentation/pages/pages.dart';
|
||||
import '../../features/home/presentation/pages/home_page.dart';
|
||||
import '../../features/settings/presentation/pages/settings_page.dart';
|
||||
import '../../features/todos/presentation/screens/home_screen.dart';
|
||||
@@ -101,7 +102,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
path: RoutePaths.login,
|
||||
name: RouteNames.login,
|
||||
pageBuilder: (context, state) => _buildPageWithTransition(
|
||||
child: const _PlaceholderPage(title: 'Login'),
|
||||
child: const LoginPage(),
|
||||
state: state,
|
||||
),
|
||||
),
|
||||
@@ -109,7 +110,7 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
path: RoutePaths.register,
|
||||
name: RouteNames.register,
|
||||
pageBuilder: (context, state) => _buildPageWithTransition(
|
||||
child: const _PlaceholderPage(title: 'Register'),
|
||||
child: const RegisterPage(),
|
||||
state: state,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,53 +1,26 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'route_paths.dart';
|
||||
import '../../features/auth/presentation/providers/auth_providers.dart';
|
||||
|
||||
/// Authentication state provider
|
||||
final authStateProvider = StateNotifierProvider<AuthStateNotifier, AuthState>(
|
||||
(ref) => AuthStateNotifier(),
|
||||
);
|
||||
/// Legacy auth state provider - redirecting to proper auth provider
|
||||
final authStateProvider = Provider<AuthState>((ref) {
|
||||
final authState = ref.watch(authNotifierProvider);
|
||||
return authState.when(
|
||||
initial: () => AuthState.unknown,
|
||||
loading: () => AuthState.unknown,
|
||||
authenticated: (_) => AuthState.authenticated,
|
||||
unauthenticated: (_) => AuthState.unauthenticated,
|
||||
error: (_) => AuthState.unauthenticated,
|
||||
);
|
||||
});
|
||||
|
||||
/// Authentication state
|
||||
/// Authentication state enum for routing
|
||||
enum AuthState {
|
||||
unknown,
|
||||
authenticated,
|
||||
unauthenticated,
|
||||
}
|
||||
|
||||
/// Authentication state notifier
|
||||
class AuthStateNotifier extends StateNotifier<AuthState> {
|
||||
AuthStateNotifier() : super(AuthState.unknown) {
|
||||
_checkInitialAuth();
|
||||
}
|
||||
|
||||
Future<void> _checkInitialAuth() async {
|
||||
// TODO: Implement actual auth check logic
|
||||
// For now, simulate checking stored auth token
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
// Mock authentication check
|
||||
// In a real app, you would check secure storage for auth token
|
||||
state = AuthState.unauthenticated;
|
||||
}
|
||||
|
||||
Future<void> login(String email, String password) async {
|
||||
// TODO: Implement actual login logic
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
state = AuthState.authenticated;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
// TODO: Implement actual logout logic
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
state = AuthState.unauthenticated;
|
||||
}
|
||||
|
||||
Future<void> register(String email, String password) async {
|
||||
// TODO: Implement actual registration logic
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
state = AuthState.authenticated;
|
||||
}
|
||||
}
|
||||
|
||||
/// Route guard utility class
|
||||
class RouteGuard {
|
||||
/// Check if user can access the given route
|
||||
|
||||
Reference in New Issue
Block a user