Phase 6 - User Session Management complete

This commit is contained in:
gitea
2025-11-05 21:14:34 +01:00
parent 53d11d49ff
commit 13ec9e5a9d
8 changed files with 758 additions and 8 deletions
+60 -5
View File
@@ -42,21 +42,35 @@ class LocalStorageService {
/// Initializes the database and cache directory.
///
/// [sessionDbPath] - Optional database path for session-specific storage.
/// [sessionCacheDir] - Optional cache directory for session-specific storage.
///
/// Must be called before using any other methods.
///
/// Throws [Exception] if initialization fails.
Future<void> initialize() async {
Future<void> initialize({
String? sessionDbPath,
Directory? sessionCacheDir,
}) async {
try {
// Initialize database
final dbPath = _testDbPath ?? await _getDatabasePath();
// Close existing database if switching sessions
if (_database != null) {
await _database!.close();
_database = null;
}
// Initialize database with session-specific or default path
final dbPath = sessionDbPath ?? _testDbPath ?? await _getDatabasePath();
_database = await openDatabase(
dbPath,
version: 1,
onCreate: _onCreate,
);
// Initialize cache directory
if (_testCacheDir != null) {
// Initialize cache directory with session-specific or default path
if (sessionCacheDir != null) {
_cacheDirectory = sessionCacheDir;
} else if (_testCacheDir != null) {
_cacheDirectory = _testCacheDir;
} else {
final appDir = await getApplicationDocumentsDirectory();
@@ -66,11 +80,52 @@ class LocalStorageService {
if (!await _cacheDirectory!.exists()) {
await _cacheDirectory!.create(recursive: true);
}
// Clear in-memory cache status when switching sessions
_cacheStatus.clear();
} catch (e) {
throw Exception('Failed to initialize LocalStorageService: $e');
}
}
/// Reinitializes the service with a new database path (for session switching).
///
/// [newDbPath] - New database path to use.
/// [newCacheDir] - New cache directory to use.
///
/// Throws [Exception] if reinitialization fails.
Future<void> reinitializeForSession({
required String newDbPath,
required Directory newCacheDir,
}) async {
await initialize(
sessionDbPath: newDbPath,
sessionCacheDir: newCacheDir,
);
}
/// Clears all cached data (for session logout).
///
/// Throws [Exception] if clearing fails.
Future<void> clearAllData() async {
_ensureInitialized();
try {
// Clear all items from database
await _database!.delete('items');
// Clear cache directory
if (_cacheDirectory != null && await _cacheDirectory!.exists()) {
await _cacheDirectory!.delete(recursive: true);
await _cacheDirectory!.create(recursive: true);
}
// Clear in-memory cache status
_cacheStatus.clear();
} catch (e) {
throw Exception('Failed to clear all data: $e');
}
}
/// Creates the database schema if it doesn't exist.
Future<void> _onCreate(Database db, int version) async {
await db.execute('''