|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:provider/provider.dart'; |
| 3 | +import 'package:wger/l10n/generated/app_localizations.dart'; |
| 4 | +import 'package:wger/providers/exercises.dart'; |
| 5 | + |
| 6 | +class SettingsExerciseCache extends StatefulWidget { |
| 7 | + const SettingsExerciseCache({super.key}); |
| 8 | + |
| 9 | + @override |
| 10 | + State<SettingsExerciseCache> createState() => _SettingsExerciseCacheState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _SettingsExerciseCacheState extends State<SettingsExerciseCache> { |
| 14 | + bool _isRefreshLoading = false; |
| 15 | + String _subtitle = ''; |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + final exerciseProvider = Provider.of<ExercisesProvider>(context, listen: false); |
| 20 | + final i18n = AppLocalizations.of(context); |
| 21 | + |
| 22 | + return ListTile( |
| 23 | + enabled: !_isRefreshLoading, |
| 24 | + title: Text(i18n.settingsExerciseCacheDescription), |
| 25 | + subtitle: _subtitle.isNotEmpty ? Text(_subtitle) : null, |
| 26 | + trailing: Row(mainAxisSize: MainAxisSize.min, children: [ |
| 27 | + IconButton( |
| 28 | + key: const ValueKey('cacheIconExercisesRefresh'), |
| 29 | + icon: _isRefreshLoading |
| 30 | + ? const SizedBox( |
| 31 | + width: 24, |
| 32 | + height: 24, |
| 33 | + child: CircularProgressIndicator(strokeWidth: 2), |
| 34 | + ) |
| 35 | + : const Icon(Icons.refresh), |
| 36 | + onPressed: _isRefreshLoading |
| 37 | + ? null |
| 38 | + : () async { |
| 39 | + setState(() => _isRefreshLoading = true); |
| 40 | + |
| 41 | + // Note: status messages are currently left in English on purpose |
| 42 | + try { |
| 43 | + setState(() => _subtitle = 'Clearing cache...'); |
| 44 | + await exerciseProvider.clearAllCachesAndPrefs(); |
| 45 | + |
| 46 | + if (mounted) { |
| 47 | + setState(() => _subtitle = 'Loading languages and units...'); |
| 48 | + } |
| 49 | + await exerciseProvider.fetchAndSetInitialData(); |
| 50 | + |
| 51 | + if (mounted) { |
| 52 | + setState(() => _subtitle = 'Loading all exercises from server...'); |
| 53 | + } |
| 54 | + await exerciseProvider.fetchAndSetAllExercises(); |
| 55 | + |
| 56 | + if (mounted) { |
| 57 | + setState(() => _subtitle = ''); |
| 58 | + } |
| 59 | + } finally { |
| 60 | + if (mounted) { |
| 61 | + setState(() => _isRefreshLoading = false); |
| 62 | + } |
| 63 | + if (context.mounted) { |
| 64 | + ScaffoldMessenger.of(context).showSnackBar( |
| 65 | + SnackBar(content: Text(i18n.success)), |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + ), |
| 71 | + IconButton( |
| 72 | + key: const ValueKey('cacheIconExercisesDelete'), |
| 73 | + icon: const Icon(Icons.delete), |
| 74 | + onPressed: () async { |
| 75 | + await exerciseProvider.clearAllCachesAndPrefs(); |
| 76 | + |
| 77 | + if (context.mounted) { |
| 78 | + final snackBar = SnackBar( |
| 79 | + content: Text(i18n.settingsCacheDeletedSnackbar), |
| 80 | + ); |
| 81 | + |
| 82 | + ScaffoldMessenger.of(context).showSnackBar(snackBar); |
| 83 | + } |
| 84 | + }, |
| 85 | + ) |
| 86 | + ]), |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments