diff --git a/lib/pages/forward/forward.dart b/lib/pages/forward/forward.dart index a9da4c7c3e..881a3ec9c3 100644 --- a/lib/pages/forward/forward.dart +++ b/lib/pages/forward/forward.dart @@ -52,8 +52,11 @@ class ForwardController extends State with SearchRecentChat { void initState() { super.initState(); sendFromRoomId = widget.sendFromRoomId; - _listenToSearch(); SchedulerBinding.instance.addPostFrameCallback((_) async { + listenToSearch( + context: context, + filteredRoomsForAll: filteredRoomsForAll, + ); recentlyChatsNotifier.value = filteredRoomsForAll; }); } @@ -149,15 +152,6 @@ class ForwardController extends State with SearchRecentChat { context.go('/rooms/${widget.sendFromRoomId}'); } - void _listenToSearch() { - searchTextEditingController.addListener( - () => handleSearchAction( - context: context, - filteredRoomsForAll: filteredRoomsForAll, - ), - ); - } - @override Widget build(BuildContext context) => ForwardView(this); } diff --git a/lib/pages/share/share.dart b/lib/pages/share/share.dart index a5f176ae40..d7d0dc3f6b 100644 --- a/lib/pages/share/share.dart +++ b/lib/pages/share/share.dart @@ -5,10 +5,13 @@ import 'package:fluffychat/event/twake_event_types.dart'; import 'package:fluffychat/pages/share/share_view.dart'; import 'package:fluffychat/presentation/enum/chat_list/chat_list_enum.dart'; import 'package:fluffychat/presentation/extensions/client_extension.dart'; +import 'package:fluffychat/presentation/mixins/search_recent_chat_mixin.dart'; import 'package:fluffychat/presentation/mixins/send_files_mixin.dart'; import 'package:fluffychat/presentation/model/chat/chat_router_input_argument.dart'; +import 'package:fluffychat/utils/extension/value_notifier_extension.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; import 'package:go_router/go_router.dart'; import 'package:matrix/matrix.dart'; import 'package:scroll_to_index/scroll_to_index.dart'; @@ -20,15 +23,12 @@ class Share extends StatefulWidget { State createState() => ShareController(); } -class ShareController extends State with SendFilesMixin { +class ShareController extends State + with SendFilesMixin, SearchRecentChat { final sendFileInteractor = getIt.get(); - final isShowRecentlyChatsNotifier = ValueNotifier(true); - final isSearchModeNotifier = ValueNotifier(false); - final textEditingController = TextEditingController(); - final AutoScrollController recentChatScrollController = AutoScrollController(); @@ -36,6 +36,28 @@ class ShareController extends State with SendFilesMixin { final selectedRoomsNotifier = ValueNotifier([]); + @override + void initState() { + super.initState(); + SchedulerBinding.instance.addPostFrameCallback((_) async { + listenToSearch( + context: context, + filteredRoomsForAll: filteredRoomsForAll, + ); + recentlyChatsNotifier.value = filteredRoomsForAll; + }); + } + + @override + void dispose() { + isSearchModeNotifier.dispose(); + recentChatScrollController.dispose(); + searchFocusNode.dispose(); + selectedRoomsNotifier.dispose(); + disposeSearchRecentChat(); + super.dispose(); + } + void onSelectChat(String id) { if (selectedRoomsNotifier.value.contains(id)) { selectedRoomsNotifier.value.remove(id); @@ -47,12 +69,10 @@ class ShareController extends State with SendFilesMixin { ); } - void toggleRecentlyChats() { - isShowRecentlyChatsNotifier.value = !isShowRecentlyChatsNotifier.value; + void toggleSearchMode() { + isSearchModeNotifier.toggle(); } - void toggleSearchMode() {} - void shareTo(String roomId) async { final room = Room( id: selectedRoomsNotifier.value.first, diff --git a/lib/pages/share/share_view.dart b/lib/pages/share/share_view.dart index 8971eef507..7e613fcb7d 100644 --- a/lib/pages/share/share_view.dart +++ b/lib/pages/share/share_view.dart @@ -8,6 +8,7 @@ import 'package:fluffychat/widgets/app_bars/searchable_app_bar_style.dart'; import 'package:fluffychat/widgets/twake_components/twake_icon_button.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:matrix/matrix.dart'; class ShareView extends StatelessWidget { const ShareView(this.controller, {super.key}); @@ -22,7 +23,7 @@ class ShareView extends StatelessWidget { child: SearchableAppBar( title: L10n.of(context)!.selectChat, searchModeNotifier: controller.isSearchModeNotifier, - textEditingController: controller.textEditingController, + textEditingController: controller.searchTextEditingController, openSearchBar: controller.toggleSearchMode, closeSearchBar: controller.toggleSearchMode, focusNode: controller.searchFocusNode, @@ -34,12 +35,12 @@ class ShareView extends StatelessWidget { child: Column( children: [ const RecentChatsTitle(), - ValueListenableBuilder( - valueListenable: controller.isShowRecentlyChatsNotifier, - builder: (context, isShowRecentlyChat, child) { - if (isShowRecentlyChat) { + ValueListenableBuilder>( + valueListenable: controller.recentlyChatsNotifier, + builder: (context, rooms, child) { + if (rooms.isNotEmpty) { return RecentChatList( - rooms: controller.filteredRoomsForAll, + rooms: rooms, selectedEventsNotifier: controller.selectedRoomsNotifier, onSelectedChat: (roomId) => controller.onSelectChat(roomId), diff --git a/lib/presentation/mixins/search_recent_chat_mixin.dart b/lib/presentation/mixins/search_recent_chat_mixin.dart index 34de86224c..3b6321b5e8 100644 --- a/lib/presentation/mixins/search_recent_chat_mixin.dart +++ b/lib/presentation/mixins/search_recent_chat_mixin.dart @@ -1,25 +1,34 @@ +import 'package:debounce_throttle/debounce_throttle.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; import 'package:flutter/material.dart'; import 'package:matrix/matrix.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; mixin SearchRecentChat { + static const _debouncerIntervalInMilliseconds = 300; + final TextEditingController searchTextEditingController = TextEditingController(); + final Debouncer debouncer = Debouncer( + const Duration(milliseconds: _debouncerIntervalInMilliseconds), + initialValue: '', + ); + final recentlyChatsNotifier = ValueNotifier>([]); void handleSearchAction({ required BuildContext context, required List filteredRoomsForAll, + required String keyword, }) { - if (searchTextEditingController.text.isNotEmpty) { + if (keyword.isNotEmpty) { final matchedRooms = filteredRoomsForAll .where( (room) => room .getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)) .toLowerCase() - .contains(searchTextEditingController.text.toLowerCase()), + .contains(keyword.toLowerCase()), ) .toList(); recentlyChatsNotifier.value = matchedRooms; @@ -32,4 +41,20 @@ mixin SearchRecentChat { recentlyChatsNotifier.dispose(); searchTextEditingController.dispose(); } + + void listenToSearch({ + required BuildContext context, + required List filteredRoomsForAll, + }) { + searchTextEditingController.addListener( + () => debouncer.value = searchTextEditingController.text, + ); + debouncer.values.listen( + (keyword) => handleSearchAction( + context: context, + filteredRoomsForAll: filteredRoomsForAll, + keyword: keyword, + ), + ); + } } diff --git a/pubspec.lock b/pubspec.lock index 7a2545d1ad..d78066e52c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -933,16 +933,16 @@ packages: dependency: transitive description: name: flutter_math_fork - sha256: a143a3a89131b578043ecbdb5e759c1033a1b3e9174f5cd1b979d93f4a7fb41c + sha256: "94bee4642892a94939af0748c6a7de0ff8318feee588379dcdfea7dc5cba06c8" url: "https://pub.dev" source: hosted - version: "0.7.1" + version: "0.7.2" flutter_matrix_html: dependency: "direct main" description: path: "." ref: master - resolved-ref: 608fb381790d068664f73e33c3277671e5e172ca + resolved-ref: "608fb381790d068664f73e33c3277671e5e172ca" url: "https://github.com/linagora/flutter_matrix_html.git" source: git version: "1.2.0"