Skip to content

Commit

Permalink
TW-1690: Handle public rooms actions
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui committed May 20, 2024
1 parent 4d40786 commit 1a27996
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions lib/pages/search/public_room/search_public_room_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/search/public_room_state.dart';
import 'package:fluffychat/domain/usecase/search/public_room_interactor.dart';
import 'package:fluffychat/pages/search/public_room/public_room_actions.dart';
import 'package:fluffychat/pages/search/search_debouncer_mixin.dart';
import 'package:fluffychat/presentation/model/search/public_room/presentation_search_public_room.dart';
import 'package:fluffychat/presentation/model/search/public_room/presentation_search_public_room_empty.dart';
import 'package:fluffychat/presentation/model/search/public_room/presentation_search_public_room_state.dart';
import 'package:fluffychat/utils/dialog/twake_dialog.dart';
import 'package:fluffychat/utils/string_extension.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';

class SearchPublicRoomController with SearchDebouncerMixin {
Expand All @@ -32,12 +36,21 @@ class SearchPublicRoomController with SearchDebouncerMixin {
bool get searchTermIsNotEmpty =>
_filter?.genericSearchTerm?.isNotEmpty == true;

String? get genericSearchTerm => _filter?.genericSearchTerm;

void init() {
initializeDebouncer((keyword) {
_updateFilter(keyword);
_updateServer(keyword);
_resetSearchResults();
_searchPublicRoom();

if (keyword.isEmpty) return;

if (keyword.isRoomAlias()) {
_resetSearchResults();
_searchPublicRoom();
} else if (keyword.isRoomId()) {
_handleKeywordIsRoomId();
}
});
}

Expand Down Expand Up @@ -87,8 +100,63 @@ class SearchPublicRoomController with SearchDebouncerMixin {
});
}

void _handleKeywordIsRoomId() {
searchResultsNotifier.value = PresentationSearchPublicRoomEmpty();
}

void _viewRoom(BuildContext context, String roomId) {
context.go('/rooms/$roomId');
}

void joinRoom(BuildContext context, String roomIdOrAlias) async {
final client = Matrix.of(context).client;
final result = await TwakeDialog.showFutureLoadingDialogFullScreen(
future: () => client.joinRoom(roomIdOrAlias),
);
if (result.error == null) {
if (client.getRoomById(result.result!) == null) {
await client.onSync.stream.firstWhere(
(sync) => sync.rooms?.join?.containsKey(result.result) ?? false,
);
}
if (!client.getRoomById(result.result!)!.isSpace) {
context.go('/rooms/${result.result!}');
}
Navigator.of(context, rootNavigator: false).pop();
return;
}
}

void handlePublicRoomActions(
BuildContext context,
PublicRoomsChunk room,
PublicRoomActions action,
) {
switch (action) {
case PublicRoomActions.join:
joinRoom(context, room.canonicalAlias ?? room.roomId);
break;
case PublicRoomActions.view:
_viewRoom(context, room.roomId);
break;
}
}

PublicRoomActions? getAction(
BuildContext context,
PublicRoomsChunk room,
) {
final client = Matrix.of(context).client;
if (client.getRoomById(room.roomId) != null) {
return PublicRoomActions.view;
} else if (room.joinRule == 'public') {
return PublicRoomActions.join;
}
return null;
}

void onSearchBarChanged(String keyword) {
if (keyword.isRoomAlias()) {
if (keyword.isRoomAlias() || keyword.isRoomId()) {
setDebouncerValue(keyword);
} else {
setDebouncerValue('');
Expand Down

0 comments on commit 1a27996

Please sign in to comment.