fluffychat/lib/pages/search/search.dart

128 lines
3.5 KiB
Dart
Raw Normal View History

//@dart=2.12
2021-04-17 11:24:00 +02:00
import 'dart:async';
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-04-17 11:24:00 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/famedlysdk_store.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/public_room_bottom_sheet.dart';
2021-11-09 21:32:16 +01:00
import 'search_view.dart';
2021-04-17 11:24:00 +02:00
class Search extends StatefulWidget {
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();
Future<QueryPublicRoomsResponse>? publicRoomsResponse;
String? lastServer;
Timer? _coolDown;
String? genericSearchTerm;
2021-04-17 11:24:00 +02:00
void search(String query) async {
setState(() {});
2021-04-17 11:24:00 +02:00
_coolDown?.cancel();
_coolDown = Timer(
2021-10-14 18:09:30 +02:00
const Duration(milliseconds: 500),
2021-04-17 11:24:00 +02:00
() => setState(() {
genericSearchTerm = query;
publicRoomsResponse = null;
searchUser(context, controller.text);
}),
);
}
2021-11-14 13:56:36 +01:00
void joinGroupAction(PublicRoomsChunk room) {
showModalBottomSheet(
2021-04-17 11:24:00 +02:00
context: context,
2021-11-14 13:56:36 +01:00
builder: (c) => PublicRoomBottomSheet(
2022-01-28 18:21:20 +01:00
roomAlias: room.canonicalAlias ?? room.roomId,
2021-11-14 13:56:36 +01:00
outerContext: context,
chunk: room,
2021-04-17 11:24:00 +02:00
),
);
}
String? server;
static const String _serverStoreNamespace = 'im.fluffychat.search.server';
2021-04-17 11:24:00 +02:00
void setServer() async {
final newServer = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
title: L10n.of(context)!.changeTheHomeserver,
2021-04-17 11:24:00 +02:00
context: context,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-04-17 11:24:00 +02:00
textFields: [
DialogTextField(
prefixText: 'https://',
hintText: Matrix.of(context).client.homeserver?.host,
2021-04-17 11:24:00 +02:00
initialText: server,
keyboardType: TextInputType.url,
)
]);
if (newServer == null) return;
Store().setItem(_serverStoreNamespace, newServer.single);
2021-04-17 11:24:00 +02:00
setState(() {
server = newServer.single;
});
}
String? currentSearchTerm;
2021-04-17 11:24:00 +02:00
List<Profile> foundProfiles = [];
2021-10-26 18:47:05 +02:00
static const searchUserDirectoryLimit = 10;
2021-04-17 11:24:00 +02:00
void searchUser(BuildContext context, String text) async {
if (text.isEmpty) {
setState(() {
foundProfiles = [];
});
}
currentSearchTerm = text;
if (currentSearchTerm?.isEmpty ?? true) return;
2021-04-17 11:24:00 +02:00
final matrix = Matrix.of(context);
SearchUserDirectoryResponse? response;
2021-04-17 11:24:00 +02:00
try {
2021-10-26 18:47:05 +02:00
response = await matrix.client.searchUserDirectory(
text,
limit: searchUserDirectoryLimit,
);
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-04-17 11:24:00 +02:00
}
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((_) async {
2021-12-30 09:53:55 +01:00
controller.text = VRouter.of(context).queryParameters['query'] ?? '';
final server = await Store().getItem(_serverStoreNamespace) as String?;
if (server?.isNotEmpty ?? false) {
this.server = server;
}
search(controller.text);
});
2021-04-17 11:24:00 +02:00
}
@override
Widget build(BuildContext context) => SearchView(this);
2021-04-17 11:24:00 +02:00
}