2021-04-17 11:24:00 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-04-17 11:24:00 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
import 'views/search_view.dart';
|
2021-04-17 11:24:00 +02:00
|
|
|
|
|
|
|
class Search extends StatefulWidget {
|
2021-05-23 13:11:55 +02:00
|
|
|
const Search({Key key}) : super(key: key);
|
2021-04-17 11:24:00 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
SearchController createState() => SearchController();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchController extends State<Search> {
|
|
|
|
final TextEditingController controller = TextEditingController();
|
2021-08-18 17:24:59 +02:00
|
|
|
Future<QueryPublicRoomsResponse> publicRoomsResponse;
|
2021-04-17 11:24:00 +02:00
|
|
|
String lastServer;
|
|
|
|
Timer _coolDown;
|
|
|
|
String genericSearchTerm;
|
|
|
|
|
|
|
|
void search(String query) async {
|
|
|
|
setState(() => null);
|
|
|
|
_coolDown?.cancel();
|
|
|
|
_coolDown = Timer(
|
|
|
|
Duration(milliseconds: 500),
|
|
|
|
() => setState(() {
|
|
|
|
genericSearchTerm = query;
|
|
|
|
publicRoomsResponse = null;
|
|
|
|
searchUser(context, controller.text);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> _joinRoomAndWait(
|
|
|
|
BuildContext context,
|
|
|
|
String roomId,
|
|
|
|
String alias,
|
|
|
|
) async {
|
|
|
|
if (Matrix.of(context).client.getRoomById(roomId) != null) {
|
|
|
|
return roomId;
|
|
|
|
}
|
|
|
|
final newRoomId = await Matrix.of(context)
|
|
|
|
.client
|
2021-05-20 13:59:55 +02:00
|
|
|
.joinRoom(alias?.isNotEmpty ?? false ? alias : roomId);
|
2021-09-10 10:44:24 +02:00
|
|
|
await Matrix.of(context).client.onSync.stream.firstWhere(
|
|
|
|
(update) => update.rooms?.join?.containsKey(newRoomId) ?? false);
|
2021-04-17 11:24:00 +02:00
|
|
|
return newRoomId;
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:24:59 +02:00
|
|
|
void joinGroupAction(PublicRoomsChunk room) async {
|
2021-04-17 11:24:00 +02:00
|
|
|
if (await showOkCancelAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-17 11:24:00 +02:00
|
|
|
context: context,
|
|
|
|
okLabel: L10n.of(context).joinRoom,
|
|
|
|
title: '${room.name} (${room.numJoinedMembers ?? 0})',
|
|
|
|
message: room.topic ?? L10n.of(context).noDescription,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
|
|
|
) ==
|
|
|
|
OkCancelResult.cancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final success = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => _joinRoomAndWait(
|
|
|
|
context,
|
|
|
|
room.roomId,
|
2021-08-05 18:16:52 +02:00
|
|
|
room.canonicalAlias ?? room.aliases?.first,
|
2021-04-17 11:24:00 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if (success.error == null) {
|
2021-08-15 13:26:16 +02:00
|
|
|
VRouter.of(context).toSegments(['rooms', success.result]);
|
2021-04-17 11:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String server;
|
|
|
|
|
|
|
|
void setServer() async {
|
|
|
|
final newServer = await showTextInputDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-17 11:24:00 +02:00
|
|
|
title: L10n.of(context).changeTheHomeserver,
|
|
|
|
context: context,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
|
|
|
textFields: [
|
|
|
|
DialogTextField(
|
|
|
|
prefixText: 'https://',
|
|
|
|
hintText: Matrix.of(context).client.homeserver.host,
|
|
|
|
initialText: server,
|
|
|
|
keyboardType: TextInputType.url,
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
if (newServer == null) return;
|
|
|
|
setState(() {
|
|
|
|
server = newServer.single;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String currentSearchTerm;
|
|
|
|
List<Profile> foundProfiles = [];
|
|
|
|
|
|
|
|
void searchUser(BuildContext context, String text) async {
|
|
|
|
if (text.isEmpty) {
|
|
|
|
setState(() {
|
|
|
|
foundProfiles = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
currentSearchTerm = text;
|
|
|
|
if (currentSearchTerm.isEmpty) return;
|
|
|
|
final matrix = Matrix.of(context);
|
2021-08-18 17:24:59 +02:00
|
|
|
SearchUserDirectoryResponse response;
|
2021-04-17 11:24:00 +02:00
|
|
|
try {
|
2021-05-20 13:59:55 +02:00
|
|
|
response = await matrix.client.searchUserDirectory(text, limit: 10);
|
2021-04-17 11:24:00 +02:00
|
|
|
} catch (_) {}
|
|
|
|
foundProfiles = List<Profile>.from(response?.results ?? []);
|
|
|
|
if (foundProfiles.isEmpty && text.isValidMatrixId && text.sigil == '@') {
|
|
|
|
foundProfiles.add(Profile.fromJson({
|
|
|
|
'displayname': text.localpart,
|
|
|
|
'user_id': text,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:07:02 +02:00
|
|
|
bool _init = false;
|
|
|
|
|
2021-04-17 11:24:00 +02:00
|
|
|
@override
|
2021-05-23 13:11:55 +02:00
|
|
|
Widget build(BuildContext context) {
|
2021-05-24 11:07:02 +02:00
|
|
|
if (!_init) {
|
|
|
|
_init = true;
|
|
|
|
controller.text = VRouter.of(context).queryParameters['query'] ?? '';
|
|
|
|
WidgetsBinding.instance
|
|
|
|
.addPostFrameCallback((_) => search(controller.text));
|
|
|
|
}
|
2021-05-24 11:03:08 +02:00
|
|
|
|
2021-05-23 13:11:55 +02:00
|
|
|
return SearchView(this);
|
2021-04-17 11:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-24 12:16:46 +02:00
|
|
|
// #fluffychat:matrix.org
|