1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/api_client_provider.dart';
import '../utils/error_mappers.dart';
/// Admin-only rescan screen.
///
/// Design notes:
/// - The user taps "Trigger Rescan" to start a library rescan on the server.
/// - After triggering, the screen polls [getScanProgress] every 2 seconds
/// while the scan is running, displaying live progress (file counts, current
/// set name).
/// - The poll timer is stored in [_pollTimer] and cancelled in [dispose] to
/// prevent memory leaks and spurious setState calls after the widget is gone.
/// - A generation counter prevents stale polling results from overwriting
/// the state after the user navigates away and back again.
/// - The trigger button is disabled while a scan is actively running to
/// prevent duplicate scans.
/// - All async continuations guard on [mounted] to prevent setState/context
/// calls after widget disposal.
class AdminRescanScreen extends ConsumerStatefulWidget {
const AdminRescanScreen({super.key});
@override
ConsumerState<AdminRescanScreen> createState() => _AdminRescanScreenState();
}
class _AdminRescanScreenState extends ConsumerState<AdminRescanScreen> {
// Interval between progress poll requests while a scan is running.
static const _pollInterval = Duration(seconds: 2);
// Null before the first status fetch, non-null after.
_ScanStatus? _status;
// Non-null when the last API call failed.
String? _error;
// True while the trigger request is in flight.
bool _isTriggering = false;
// Active polling timer; cancelled in dispose and whenever the scan finishes.
Timer? _pollTimer;
// Generation counter: async completions discard results if they captured a
// stale generation value (prevents out-of-order result clobbering).
int _generation = 0;
@override
void initState() {
super.initState();
// Fetch the current scan status immediately so the user sees whether a
// scan is already running (e.g. started by another admin session).
WidgetsBinding.instance.addPostFrameCallback((_) => _fetchStatus());
}
@override
void dispose() {
// Always cancel the polling timer to avoid calling setState after disposal
// and to release the periodic timer resource.
_pollTimer?.cancel();
super.dispose();
}
// ---------------------------------------------------------------------------
// Status fetching
// ---------------------------------------------------------------------------
/// Fetches the current scan progress and updates [_status].
///
/// If the scan is running, a poll timer is started (or kept running).
/// If the scan is idle/complete, any active poll timer is cancelled.
Future<void> _fetchStatus() async {
if (!mounted) return;
final generation = ++_generation;
try {
final raw = await ref.read(apiClientProvider).getScanProgress();
if (!mounted || generation != _generation) return;
final status = _ScanStatus.fromMap(raw);
setState(() {
_status = status;
_error = null;
});
_updatePolling(status.isRunning);
} catch (e) {
if (!mounted || generation != _generation) return;
setState(() => _error = adminRescanErrorMessage(e));
// Stop polling on error to avoid hammering a broken endpoint; the user
// can retry manually via the refresh button.
_pollTimer?.cancel();
_pollTimer = null;
}
}
/// Starts or stops the background polling timer based on [scanRunning].
///
/// Starts a new periodic timer when [scanRunning] is true and no timer is
/// active; cancels any active timer when [scanRunning] is false.
void _updatePolling(bool scanRunning) {
if (scanRunning && _pollTimer == null) {
// Poll every 2 seconds while the scan is running to show live progress.
_pollTimer = Timer.periodic(_pollInterval, (_) => _fetchStatus());
} else if (!scanRunning) {
_pollTimer?.cancel();
_pollTimer = null;
}
}
// ---------------------------------------------------------------------------
// Trigger rescan action
// ---------------------------------------------------------------------------
/// Sends a trigger-rescan request and immediately begins polling for progress.
Future<void> _triggerRescan() async {
if (!mounted || _isTriggering) return;
setState(() {
_isTriggering = true;
_error = null;
});
try {
await ref.read(apiClientProvider).triggerRescan();
if (!mounted) return;
setState(() => _isTriggering = false);
// Start polling immediately so the user sees progress as soon as the
// server reports the scan has begun.
await _fetchStatus();
} catch (e) {
if (!mounted) return;
setState(() {
_isTriggering = false;
_error = adminRescanErrorMessage(e);
});
}
}
// ---------------------------------------------------------------------------
// Build
// ---------------------------------------------------------------------------
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Rescan Library'),
actions: [
IconButton(
key: const Key('admin_rescan_refresh'),
icon: const Icon(Icons.refresh),
tooltip: 'Check status',
onPressed: _fetchStatus,
),
],
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24),
child: _buildBody(context),
),
),
);
}
/// Builds the screen body: status card + trigger button.
Widget _buildBody(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_StatusCard(status: _status, error: _error),
const SizedBox(height: 32),
_TriggerButton(
isRunning: _status?.isRunning ?? false,
isTriggering: _isTriggering,
onTap: _triggerRescan,
),
],
);
}
}
// ---------------------------------------------------------------------------
// Data model for scan progress
// ---------------------------------------------------------------------------
/// Parsed scan progress state returned by GET /api/v1/admin/scan-progress.
///
/// Kept as a plain data class (no business logic) so [_AdminRescanScreenState]
/// and the sub-widgets stay focused on their own concerns (SRP).
class _ScanStatus {
const _ScanStatus({
required this.isRunning,
required this.currentSet,
required this.setsTotal,
required this.setsDone,
required this.filesTotal,
required this.filesDone,
this.lastError,
});
/// Parses the raw progress map returned by the server.
factory _ScanStatus.fromMap(Map<String, dynamic> map) {
return _ScanStatus(
isRunning: map['running'] as bool? ?? false,
currentSet: map['current_set'] as String? ?? '',
setsTotal: map['sets_total'] as int? ?? 0,
setsDone: map['sets_done'] as int? ?? 0,
filesTotal: map['files_total'] as int? ?? 0,
filesDone: map['files_done'] as int? ?? 0,
lastError: map['last_error'] as String?,
);
}
final bool isRunning;
final String currentSet;
final int setsTotal;
final int setsDone;
final int filesTotal;
final int filesDone;
final String? lastError;
}
// ---------------------------------------------------------------------------
// Sub-widgets
// ---------------------------------------------------------------------------
/// Card that displays the current scan status and progress.
///
/// Shows a spinner + live counters while running; shows "Idle" or "Scan
/// complete" when not running; shows a loading placeholder before the first
/// status fetch completes.
class _StatusCard extends StatelessWidget {
const _StatusCard({required this.status, required this.error});
final _ScanStatus? status;
final String? error;
@override
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: const EdgeInsets.all(20),
child: _cardContent(context),
),
);
}
/// Returns the inner content of the status card.
Widget _cardContent(BuildContext context) {
// Show error state if there was an API failure.
if (error != null) {
return _ErrorRow(message: error!);
}
// Show a spinner while the initial status fetch is in progress.
final s = status;
if (s == null) {
return const Center(
key: Key('admin_rescan_status_loading'),
child: CircularProgressIndicator(),
);
}
if (s.isRunning) {
return _RunningContent(status: s);
}
return _IdleContent(status: s);
}
}
/// Status card content while a scan is running.
class _RunningContent extends StatelessWidget {
const _RunningContent({required this.status});
final _ScanStatus status;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
),
const SizedBox(width: 12),
Text(
'Scan running…',
key: const Key('admin_rescan_running_label'),
style: Theme.of(context).textTheme.titleSmall,
),
],
),
if (status.currentSet.isNotEmpty) ...[
const SizedBox(height: 12),
Text(
'Current set: ${status.currentSet}',
style: Theme.of(context).textTheme.bodyMedium,
),
],
if (status.setsTotal > 0) ...[
const SizedBox(height: 6),
Text('Sets: ${status.setsDone} / ${status.setsTotal}'),
],
if (status.filesTotal > 0) ...[
const SizedBox(height: 6),
Text('Files: ${status.filesDone} / ${status.filesTotal}'),
],
],
);
}
}
/// Status card content when no scan is running.
class _IdleContent extends StatelessWidget {
const _IdleContent({required this.status});
final _ScanStatus status;
@override
Widget build(BuildContext context) {
// Show a "Scan complete" summary when there are files already scanned;
// otherwise show the neutral "Idle" state.
final hasScanned = status.filesTotal > 0 || status.setsDone > 0;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Icon(
hasScanned ? Icons.check_circle_outline : Icons.schedule_outlined,
color: hasScanned
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 12),
Text(
hasScanned ? 'Scan complete' : 'Idle — no scan running',
key: const Key('admin_rescan_idle_label'),
style: Theme.of(context).textTheme.titleSmall,
),
],
),
if (hasScanned && status.filesTotal > 0) ...[
const SizedBox(height: 8),
Text('Files scanned: ${status.filesDone} / ${status.filesTotal}'),
],
if (status.lastError != null && status.lastError!.isNotEmpty) ...[
const SizedBox(height: 8),
Text(
'Last error: ${status.lastError}',
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
],
],
);
}
}
/// Inline error row shown inside the status card.
class _ErrorRow extends StatelessWidget {
const _ErrorRow({required this.message});
final String message;
@override
Widget build(BuildContext context) {
return Row(
children: [
Icon(Icons.error_outline, color: Theme.of(context).colorScheme.error),
const SizedBox(width: 12),
Expanded(
child: Text(
message,
key: const Key('admin_rescan_error'),
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
),
],
);
}
}
/// Button that triggers a rescan.
///
/// Disabled while a scan is running or a trigger request is in flight,
/// preventing duplicate scans and accidental double-taps.
class _TriggerButton extends StatelessWidget {
const _TriggerButton({
required this.isRunning,
required this.isTriggering,
required this.onTap,
});
final bool isRunning;
final bool isTriggering;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
// Disable the button while a scan is active or the trigger is in flight.
final canTrigger = !isRunning && !isTriggering;
return FilledButton.icon(
key: const Key('admin_rescan_trigger'),
onPressed: canTrigger ? onTap : null,
icon: isTriggering
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Icon(Icons.sync_outlined),
label: Text(isRunning ? 'Scan in progress…' : 'Trigger Rescan'),
);
}
}
|