feat: Remember homeserver on search page

This commit is contained in:
Christian Pauly 2021-12-29 11:22:11 +01:00
parent 9f040acee0
commit 61e0aaa2e9

View File

@ -1,3 +1,5 @@
//@dart=2.12
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -7,12 +9,13 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart'; import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/utils/famedlysdk_store.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/public_room_bottom_sheet.dart'; import 'package:fluffychat/widgets/public_room_bottom_sheet.dart';
import 'search_view.dart'; import 'search_view.dart';
class Search extends StatefulWidget { class Search extends StatefulWidget {
const Search({Key key}) : super(key: key); const Search({Key? key}) : super(key: key);
@override @override
SearchController createState() => SearchController(); SearchController createState() => SearchController();
@ -20,13 +23,13 @@ class Search extends StatefulWidget {
class SearchController extends State<Search> { class SearchController extends State<Search> {
final TextEditingController controller = TextEditingController(); final TextEditingController controller = TextEditingController();
Future<QueryPublicRoomsResponse> publicRoomsResponse; Future<QueryPublicRoomsResponse>? publicRoomsResponse;
String lastServer; String? lastServer;
Timer _coolDown; Timer? _coolDown;
String genericSearchTerm; String? genericSearchTerm;
void search(String query) async { void search(String query) async {
setState(() => null); setState(() {});
_coolDown?.cancel(); _coolDown?.cancel();
_coolDown = Timer( _coolDown = Timer(
const Duration(milliseconds: 500), const Duration(milliseconds: 500),
@ -49,30 +52,33 @@ class SearchController extends State<Search> {
); );
} }
String server; String? server;
static const String _serverStoreNamespace = 'im.fluffychat.search.server';
void setServer() async { void setServer() async {
final newServer = await showTextInputDialog( final newServer = await showTextInputDialog(
useRootNavigator: false, useRootNavigator: false,
title: L10n.of(context).changeTheHomeserver, title: L10n.of(context)!.changeTheHomeserver,
context: context, context: context,
okLabel: L10n.of(context).ok, okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context).cancel, cancelLabel: L10n.of(context)!.cancel,
textFields: [ textFields: [
DialogTextField( DialogTextField(
prefixText: 'https://', prefixText: 'https://',
hintText: Matrix.of(context).client.homeserver.host, hintText: Matrix.of(context).client.homeserver?.host,
initialText: server, initialText: server,
keyboardType: TextInputType.url, keyboardType: TextInputType.url,
) )
]); ]);
if (newServer == null) return; if (newServer == null) return;
Store().setItem(_serverStoreNamespace, newServer.single);
setState(() { setState(() {
server = newServer.single; server = newServer.single;
}); });
} }
String currentSearchTerm; String? currentSearchTerm;
List<Profile> foundProfiles = []; List<Profile> foundProfiles = [];
static const searchUserDirectoryLimit = 10; static const searchUserDirectoryLimit = 10;
@ -84,9 +90,9 @@ class SearchController extends State<Search> {
}); });
} }
currentSearchTerm = text; currentSearchTerm = text;
if (currentSearchTerm.isEmpty) return; if (currentSearchTerm?.isEmpty ?? true) return;
final matrix = Matrix.of(context); final matrix = Matrix.of(context);
SearchUserDirectoryResponse response; SearchUserDirectoryResponse? response;
try { try {
response = await matrix.client.searchUserDirectory( response = await matrix.client.searchUserDirectory(
text, text,
@ -100,21 +106,22 @@ class SearchController extends State<Search> {
'user_id': text, 'user_id': text,
})); }));
} }
setState(() => null); setState(() {});
} }
bool _init = false;
@override @override
Widget build(BuildContext context) { void initState() {
if (!_init) { super.initState();
_init = true;
controller.text = VRouter.of(context).queryParameters['query'] ?? ''; controller.text = VRouter.of(context).queryParameters['query'] ?? '';
WidgetsBinding.instance WidgetsBinding.instance?.addPostFrameCallback((_) async {
.addPostFrameCallback((_) => search(controller.text)); final server = await Store().getItem(_serverStoreNamespace) as String?;
if (server?.isNotEmpty ?? false) {
this.server = server;
}
search(controller.text);
});
} }
return SearchView(this); @override
Widget build(BuildContext context) => SearchView(this);
} }
}
// #fluffychat:matrix.org