perf(library): optimize CIFS scan recovery

This commit is contained in:
zarzet
2026-09-01 12:00:37 +07:00
parent 0d148113f0
commit 4ba3ab2fc7
5 changed files with 227 additions and 55 deletions
+7
View File
@@ -357,6 +357,13 @@ class _EagerInitializationState extends ConsumerState<_EagerInitialization>
final now = DateTime.now();
final prefs = await SharedPreferences.getInstance();
final pendingSourceId = prefs.getString(localLibraryActiveScanSourceKey);
if (pendingSourceId != null && pendingSourceId.trim().isNotEmpty) {
await ref
.read(localLibraryProvider.notifier)
.startSourceScan(pendingSourceId.trim());
return;
}
final lastScanned = readLocalLibraryLastScannedAt(prefs);
if (lastScanned != null) {
+18 -1
View File
@@ -531,6 +531,7 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
folderPath,
isCancelled: () => _scanCancelRequested,
);
var ingested = false;
try {
if (_scanCancelRequested) return null;
state = state.copyWith(
@@ -560,9 +561,10 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
'Stream-ingested ${result.inserted}/${scanFile.expectedCount} scan rows '
'(${result.skipped} downloads excluded)',
);
ingested = true;
return result;
} finally {
await scanFile.delete();
if (ingested) await scanFile.delete();
}
}
@@ -600,6 +602,12 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
_scanInProgress = true;
_scanCancelRequested = false;
try {
final prefs = await _prefs;
await prefs.setString(localLibraryActiveScanSourceKey, activeSourceId);
} catch (e) {
_log.w('Failed to persist active library scan marker: $e');
}
_log.i(
'Starting library scan: $folderPath (incremental: ${!forceFullScan})',
);
@@ -930,6 +938,15 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
}
_stopProgressPolling();
_scanInProgress = false;
try {
final prefs = await _prefs;
if (prefs.getString(localLibraryActiveScanSourceKey) ==
activeSourceId) {
await prefs.remove(localLibraryActiveScanSourceKey);
}
} catch (e) {
_log.w('Failed to clear active library scan marker: $e');
}
state = state.copyWith(clearScanningSourceId: true);
}
}
+1
View File
@@ -1,6 +1,7 @@
import 'package:shared_preferences/shared_preferences.dart';
const localLibraryLastScannedAtKey = 'local_library_last_scanned_at';
const localLibraryActiveScanSourceKey = 'local_library_active_scan_source';
DateTime? readLocalLibraryLastScannedAt(SharedPreferences prefs) {
final lastScannedAtStr = prefs.getString(localLibraryLastScannedAtKey);