Skip to content

Commit c28a784

Browse files
fixup! fixup! feat: add chat input provider with select images logic
1 parent 773d5e5 commit c28a784

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

lib/config/providers/chat_input_provider.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ class ChatInputNotifier extends FamilyNotifier<ChatInputState, String> {
6666
}
6767

6868
Future<void> handleImagesSelected() async {
69-
final imagePaths = await _imagePickerService.pickMultipleImages();
70-
if (imagePaths.isNotEmpty) {
71-
state = state.copyWith(
72-
showMediaSelector: false,
73-
selectedImages: [...state.selectedImages, ...imagePaths],
74-
);
75-
} else {
69+
try {
70+
final imagePaths = await _imagePickerService.pickMultipleImages();
71+
if (imagePaths.isNotEmpty) {
72+
state = state.copyWith(
73+
showMediaSelector: false,
74+
selectedImages: [...state.selectedImages, ...imagePaths],
75+
);
76+
} else {
77+
state = state.copyWith(showMediaSelector: false);
78+
}
79+
} catch (e) {
80+
_logger.warning('Failed to select images for group $_groupId', e);
7681
state = state.copyWith(showMediaSelector: false);
7782
}
7883
}

test/config/providers/chat_input_provider_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import 'package:whitenoise/ui/chat/states/chat_input_state.dart';
88

99
class MockImagePickerService extends ImagePickerService {
1010
List<String>? imagesToReturn;
11+
Exception? errorToThrow;
1112

1213
@override
1314
Future<List<String>> pickMultipleImages() async {
15+
if (errorToThrow != null) {
16+
throw errorToThrow!;
17+
}
1418
return imagesToReturn ?? [];
1519
}
1620
}
@@ -167,6 +171,37 @@ void main() {
167171
expect(state.selectedImages[2], '/path/to/image3.jpg');
168172
});
169173
});
174+
175+
group('when image picker service throws an error', () {
176+
setUp(() {
177+
mockImagePicker.errorToThrow = Exception('Image picker failed');
178+
});
179+
180+
test('keeps selectedImages empty', () async {
181+
await notifier.handleImagesSelected();
182+
final state = container.read(chatInputProvider(testGroupId));
183+
expect(state.selectedImages, isEmpty);
184+
});
185+
186+
test('sets showMediaSelector to false', () async {
187+
await notifier.handleImagesSelected();
188+
final state = container.read(chatInputProvider(testGroupId));
189+
expect(state.showMediaSelector, false);
190+
});
191+
192+
test('does not add images when error occurs with previous images', () async {
193+
mockImagePicker.errorToThrow = null;
194+
mockImagePicker.imagesToReturn = ['/path/to/image1.jpg'];
195+
await notifier.handleImagesSelected();
196+
197+
mockImagePicker.errorToThrow = Exception('Image picker failed');
198+
await notifier.handleImagesSelected();
199+
200+
final state = container.read(chatInputProvider(testGroupId));
201+
expect(state.selectedImages.length, 1);
202+
expect(state.selectedImages[0], '/path/to/image1.jpg');
203+
});
204+
});
170205
});
171206

172207
group('removeImage', () {

0 commit comments

Comments
 (0)