diff --git a/packages/stream_chat_flutter/lib/src/media_list_view_controller.dart b/packages/stream_chat_flutter/lib/src/media_list_view_controller.dart new file mode 100644 index 00000000..9ec3f594 --- /dev/null +++ b/packages/stream_chat_flutter/lib/src/media_list_view_controller.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; + +/// Controller for MediaListView Widget +class MediaListViewController extends ChangeNotifier { + var _shouldUpdateMedia = false; + + /// Getter that knows if the media should be updated. + bool get shouldUpdateMedia => _shouldUpdateMedia; + + /// Method that update shouldUpdateMedia and notify all listeners + /// about this update. + void updateMedia({required bool newValue}) { + _shouldUpdateMedia = newValue; + notifyListeners(); + } +} diff --git a/packages/stream_chat_flutter/test/src/media_list_view_controller_test.dart b/packages/stream_chat_flutter/test/src/media_list_view_controller_test.dart new file mode 100644 index 00000000..3500d30b --- /dev/null +++ b/packages/stream_chat_flutter/test/src/media_list_view_controller_test.dart @@ -0,0 +1,37 @@ +import 'package:stream_chat_flutter/src/media_list_view_controller.dart'; +import 'package:test/test.dart'; + +void main() { + test('should update media', () { + final controller = MediaListViewController(); + + expect(controller.shouldUpdateMedia, false); + + controller.updateMedia(newValue: true); + expect(controller.shouldUpdateMedia, true); + + controller.dispose(); + }); + + test('should notify listeners on update media', () { + final controller = MediaListViewController(); + + var callCount = 0; + void updateCallsSpy() => callCount++; + + controller.addListener(updateCallsSpy); + + expect(callCount, 0); + controller.updateMedia(newValue: false); + expect(controller.shouldUpdateMedia, false); + expect(callCount, 1); + + controller.updateMedia(newValue: true); + expect(controller.shouldUpdateMedia, true); + expect(callCount, 2); + + controller + ..removeListener(updateCallsSpy) + ..dispose(); + }); +}