Skip to content

Commit

Permalink
fix: blynk when scroll in search room
Browse files Browse the repository at this point in the history
  • Loading branch information
drminh2807 authored and hoangdat committed Oct 2, 2023
1 parent 2d4c8b7 commit 2e26607
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class ChatController extends State<Chat>
return;
}
hideKeyboardChatScreen();
hideSearchKeyboardIfNeeded();
setReadMarker();
if (!scrollController.hasClients) return;
if (scrollController.position.pixels ==
Expand Down
28 changes: 24 additions & 4 deletions lib/pages/chat/chat_room_search_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:async';

import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/room/chat_room_search_state.dart';
import 'package:fluffychat/domain/app_state/search/search_state.dart';
import 'package:fluffychat/domain/usecase/room/chat_room_search_interactor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:dartz/dartz.dart';
import 'package:debounce_throttle/debounce_throttle.dart';
Expand All @@ -16,6 +19,7 @@ mixin ChatRoomSearchMixin {

final isSearchingNotifier = ValueNotifier(false);
final searchTextController = TextEditingController();
final searchFocusNode = FocusNode();
final searchStatus =
ValueNotifier<Either<Failure, Success>>(Right(SearchInitial()));
final canGoUp = ValueNotifier(false);
Expand All @@ -25,14 +29,16 @@ mixin ChatRoomSearchMixin {

int _historyCount = 100;
Timeline? Function()? _getTimeline;
void Function(int)? _scrollToIndex;
Future Function(int)? _scrollToIndex;

static const _debouncerDuration = Duration(milliseconds: 300);
final _debouncer = Debouncer(
_debouncerDuration,
initialValue: '',
);

var _scrollingToIndexCount = 0;

void closeSearch() {
isSearchingNotifier.value = false;
clearSearch();
Expand All @@ -49,7 +55,7 @@ mixin ChatRoomSearchMixin {

void initializeSearch({
required Timeline? Function()? getTimeline,
Function(int)? scrollToIndex,
Future Function(int)? scrollToIndex,
required int historyCount,
}) {
_scrollToIndex = scrollToIndex;
Expand Down Expand Up @@ -97,10 +103,17 @@ mixin ChatRoomSearchMixin {
canGoUp.value = false;
}

void _scrollToEvent(Either<Failure, Success> event) {
Future _scrollToEvent(Either<Failure, Success> event) async {
final index = event.getSuccessOrNull<ChatRoomSearchSuccess>()?.eventIndex;
if (index != null) {
_scrollToIndex?.call(index);
_scrollingToIndexCount++;
try {
await _scrollToIndex?.call(index);
} catch (e) {
Logs().e('ChatRoomSearchMixin::_scrollToEvent $e');
} finally {
_scrollingToIndexCount--;
}
}
}

Expand Down Expand Up @@ -158,4 +171,11 @@ mixin ChatRoomSearchMixin {
},
);
}

void hideSearchKeyboardIfNeeded() {
if (_scrollingToIndexCount > 0 || !searchFocusNode.hasFocus || kIsWeb) {
return;
}
searchFocusNode.unfocus();
}
}
1 change: 1 addition & 0 deletions lib/pages/chat/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class ChatView extends StatelessWidget {
if (isSearching) {
return TextField(
controller: controller.searchTextController,
focusNode: controller.searchFocusNode,
autofocus: true,
onChanged: controller.onSearchChanged,
decoration: InputDecoration(
Expand Down

0 comments on commit 2e26607

Please sign in to comment.