Skip to content

Commit

Permalink
TW-1051: Apply search for share screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored and hoangdat committed Nov 24, 2023
1 parent 7d82710 commit 68920d5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 30 deletions.
14 changes: 4 additions & 10 deletions lib/pages/forward/forward.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class ForwardController extends State<Forward> with SearchRecentChat {
void initState() {
super.initState();
sendFromRoomId = widget.sendFromRoomId;
_listenToSearch();
SchedulerBinding.instance.addPostFrameCallback((_) async {
listenToSearch(
context: context,
filteredRoomsForAll: filteredRoomsForAll,
);
recentlyChatsNotifier.value = filteredRoomsForAll;
});
}
Expand Down Expand Up @@ -149,15 +152,6 @@ class ForwardController extends State<Forward> with SearchRecentChat {
context.go('/rooms/${widget.sendFromRoomId}');
}

void _listenToSearch() {
searchTextEditingController.addListener(
() => handleSearchAction(
context: context,
filteredRoomsForAll: filteredRoomsForAll,
),
);
}

@override
Widget build(BuildContext context) => ForwardView(this);
}
Expand Down
38 changes: 29 additions & 9 deletions lib/pages/share/share.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,22 +23,41 @@ class Share extends StatefulWidget {
State<Share> createState() => ShareController();
}

class ShareController extends State<Share> with SendFilesMixin {
class ShareController extends State<Share>
with SendFilesMixin, SearchRecentChat {
final sendFileInteractor = getIt.get<SendFileInteractor>();

final isShowRecentlyChatsNotifier = ValueNotifier(true);

final isSearchModeNotifier = ValueNotifier(false);

final textEditingController = TextEditingController();

final AutoScrollController recentChatScrollController =
AutoScrollController();

final searchFocusNode = FocusNode();

final selectedRoomsNotifier = ValueNotifier(<String>[]);

@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);
Expand All @@ -47,12 +69,10 @@ class ShareController extends State<Share> 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,
Expand Down
13 changes: 7 additions & 6 deletions lib/pages/share/share_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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,
Expand All @@ -34,12 +35,12 @@ class ShareView extends StatelessWidget {
child: Column(
children: [
const RecentChatsTitle(),
ValueListenableBuilder<bool>(
valueListenable: controller.isShowRecentlyChatsNotifier,
builder: (context, isShowRecentlyChat, child) {
if (isShowRecentlyChat) {
ValueListenableBuilder<List<Room>>(
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),
Expand Down
29 changes: 27 additions & 2 deletions lib/presentation/mixins/search_recent_chat_mixin.dart
Original file line number Diff line number Diff line change
@@ -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<String> debouncer = Debouncer(
const Duration(milliseconds: _debouncerIntervalInMilliseconds),
initialValue: '',
);

final recentlyChatsNotifier = ValueNotifier<List<Room>>([]);

void handleSearchAction({
required BuildContext context,
required List<Room> 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;
Expand All @@ -32,4 +41,20 @@ mixin SearchRecentChat {
recentlyChatsNotifier.dispose();
searchTextEditingController.dispose();
}

void listenToSearch({
required BuildContext context,
required List<Room> filteredRoomsForAll,
}) {
searchTextEditingController.addListener(
() => debouncer.value = searchTextEditingController.text,
);
debouncer.values.listen(
(keyword) => handleSearchAction(
context: context,
filteredRoomsForAll: filteredRoomsForAll,
keyword: keyword,
),
);
}
}
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 68920d5

Please sign in to comment.