diff --git a/lib/widgets/chat_compression_selector.dart b/lib/widgets/chat_compression_selector.dart index 5bddf0cd..864a7f1e 100644 --- a/lib/widgets/chat_compression_selector.dart +++ b/lib/widgets/chat_compression_selector.dart @@ -81,6 +81,9 @@ class _SelectorView { final bool busy; final bool failed; + /// Counts failed saves, so the profile picker drops an unsaved choice. + final int failures; + const _SelectorView({ required this.mode, required this.enabled, @@ -88,6 +91,7 @@ class _SelectorView { required this.profileId, required this.busy, required this.failed, + required this.failures, }); } @@ -95,6 +99,7 @@ class _ChatCompressionSelectorState extends State { late final ValueNotifier<_SelectorView> _view = ValueNotifier(_snapshot()); bool _busy = false; bool _failed = false; + int _failures = 0; _SelectorView _snapshot() => _SelectorView( mode: widget.mode, @@ -103,6 +108,7 @@ class _ChatCompressionSelectorState extends State { profileId: widget.effectiveProfileId, busy: _busy, failed: _failed, + failures: _failures, ); @override @@ -125,6 +131,7 @@ class _ChatCompressionSelectorState extends State { setState(() { _busy = busy; _failed = failed; + if (failed) _failures++; }); _view.value = _snapshot(); } @@ -252,8 +259,9 @@ class _ChatCompressionSelectorState extends State { Padding( padding: const EdgeInsets.fromLTRB(16, 8, 16, 0), child: DropdownButtonFormField( - // Rebuild with the saved value after a change or failure. - key: ValueKey(profileValue), + // The field keeps a picked value internally, so rebuild + // it with the saved value after a change or a failure. + key: ValueKey('$profileValue#${view.failures}'), isExpanded: true, initialValue: profileValue, decoration: InputDecoration( diff --git a/test/widgets/chat_compression_selector_test.dart b/test/widgets/chat_compression_selector_test.dart index a3ad6633..ec32ff8d 100644 --- a/test/widgets/chat_compression_selector_test.dart +++ b/test/widgets/chat_compression_selector_test.dart @@ -260,6 +260,27 @@ void main() { expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget); }); + testWidgets('a failed profile save shows the saved profile again', ( + tester, + ) async { + await pumpHost(tester, mode: ChatCompressionMode.cyr2lat, profileId: 'ru'); + await openSheet(tester); + + await tester.ensureVisible(find.text('Russian')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Russian')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Serbian').last); + await tester.pump(); + expect(profileCalls, ['sr']); + pending.single.completeError(StateError('disk full')); + await tester.pumpAndSettle(); + + expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget); + expect(find.text('Russian'), findsOneWidget); + expect(find.text('Serbian'), findsNothing); + }); + testWidgets('disabled shows the saved mode, and the sheet says why and ' 'allows no change', (tester) async { final semantics = tester.ensureSemantics();