Add public room discovery

This commit is contained in:
Christian Pauly 2020-02-22 20:05:04 +01:00
parent 020cba5679
commit 5c4d9cc12f
10 changed files with 170 additions and 23 deletions

View File

@ -48,7 +48,7 @@ flutter pub run intl_translation:extract_to_arb --output-dir=lib/i18n lib/i18n/i
4. Update the translations with this command:
```
flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/i18n --no-use-deferred-loading lib/i18n/I18n.dart lib/i18n/intl_*.arb
flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/i18n --no-use-deferred-loading lib/i18n/i18n.dart lib/i18n/intl_*.arb
```
5. Make sure your language is in `supportedLocales` in `/lib/main.dart`.

View File

@ -0,0 +1,48 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:flutter/material.dart';
import '../../i18n/i18n.dart';
import '../../utils/app_route.dart';
import '../../views/chat.dart';
import '../avatar.dart';
import '../matrix.dart';
class PublicRoomListItem extends StatelessWidget {
final PublicRoomEntry publicRoomEntry;
const PublicRoomListItem(this.publicRoomEntry, {Key key}) : super(key: key);
void joinAction(BuildContext context) async {
final success = await Matrix.of(context)
.tryRequestWithLoadingDialog(publicRoomEntry.join());
if (success != false) {
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
ChatView(publicRoomEntry.roomId),
),
);
}
}
@override
Widget build(BuildContext context) {
final bool hasTopic =
publicRoomEntry.topic != null && publicRoomEntry.topic.isNotEmpty;
return ListTile(
leading:
Avatar(MxContent(publicRoomEntry.avatarUrl), publicRoomEntry.name),
title: Text(hasTopic
? "${publicRoomEntry.name} (${publicRoomEntry.numJoinedMembers})"
: publicRoomEntry.name),
subtitle: Text(
hasTopic
? publicRoomEntry.topic
: I18n.of(context).countParticipants(
publicRoomEntry.numJoinedMembers?.toString() ?? "0"),
maxLines: 1,
),
onTap: () => joinAction(context),
);
}
}

View File

@ -188,6 +188,8 @@ class I18n {
String get changeTheNameOfTheGroup =>
Intl.message("Change the name of the group");
String get changeTheServer => Intl.message("Change the server");
String get channelCorruptedDecryptError =>
Intl.message("The encryption has been corrupted");
@ -478,6 +480,8 @@ class I18n {
String get pleaseEnterYourUsername =>
Intl.message("Please enter your username");
String get publicRooms => Intl.message("Public Rooms");
String get rejoin => Intl.message("Rejoin");
String redactedAnEvent(String username) => Intl.message(

View File

@ -216,6 +216,11 @@
"type": "text",
"placeholders": {}
},
"Change the server": "Ändere den Server",
"@Change the server": {
"type": "text",
"placeholders": {}
},
"The encryption has been corrupted": "Die Verschlüsselung wurde korrumpiert",
"@The encryption has been corrupted": {
"type": "text",
@ -761,6 +766,11 @@
"type": "text",
"placeholders": {}
},
"Public Rooms": "Öffentliche Räume",
"@Public Rooms": {
"type": "text",
"placeholders": {}
},
"Rejoin": "Wieder beitreten",
"@Rejoin": {
"type": "text",

View File

@ -1,5 +1,5 @@
{
"@@last_modified": "2020-02-22T08:18:47.225173",
"@@last_modified": "2020-02-22T20:02:42.757437",
"About": "About",
"@About": {
"type": "text",
@ -216,6 +216,11 @@
"type": "text",
"placeholders": {}
},
"Change the server": "Change the server",
"@Change the server": {
"type": "text",
"placeholders": {}
},
"The encryption has been corrupted": "The encryption has been corrupted",
"@The encryption has been corrupted": {
"type": "text",
@ -761,6 +766,11 @@
"type": "text",
"placeholders": {}
},
"Public Rooms": "Public Rooms",
"@Public Rooms": {
"type": "text",
"placeholders": {}
},
"Rejoin": "Rejoin",
"@Rejoin": {
"type": "text",

View File

@ -150,6 +150,7 @@ class MessageLookup extends MessageLookupByLibrary {
"Ban from chat" : MessageLookupByLibrary.simpleMessage("Aus dem Chat verbannen"),
"Banned" : MessageLookupByLibrary.simpleMessage("Banned"),
"Change the name of the group" : MessageLookupByLibrary.simpleMessage("Gruppenname ändern"),
"Change the server" : MessageLookupByLibrary.simpleMessage("Ändere den Server"),
"Change your style" : MessageLookupByLibrary.simpleMessage("Change your style"),
"Changelog" : MessageLookupByLibrary.simpleMessage("Changelog"),
"Chat details" : MessageLookupByLibrary.simpleMessage("Gruppeninfo"),
@ -234,6 +235,7 @@ class MessageLookup extends MessageLookupByLibrary {
"Please enter a matrix identifier" : MessageLookupByLibrary.simpleMessage("Bitte eine Matrix ID eingeben"),
"Please enter your password" : MessageLookupByLibrary.simpleMessage("Bitte dein Passwort eingeben"),
"Please enter your username" : MessageLookupByLibrary.simpleMessage("Bitte deinen Benutzernamen eingeben"),
"Public Rooms" : MessageLookupByLibrary.simpleMessage("Öffentliche Räume"),
"Rejoin" : MessageLookupByLibrary.simpleMessage("Wieder beitreten"),
"Remove" : MessageLookupByLibrary.simpleMessage("Entfernen"),
"Remove all other devices" : MessageLookupByLibrary.simpleMessage("Alle anderen Geräte entfernen"),

View File

@ -150,6 +150,7 @@ class MessageLookup extends MessageLookupByLibrary {
"Ban from chat" : MessageLookupByLibrary.simpleMessage("Ban from chat"),
"Banned" : MessageLookupByLibrary.simpleMessage("Banned"),
"Change the name of the group" : MessageLookupByLibrary.simpleMessage("Change the name of the group"),
"Change the server" : MessageLookupByLibrary.simpleMessage("Change the server"),
"Change your style" : MessageLookupByLibrary.simpleMessage("Change your style"),
"Changelog" : MessageLookupByLibrary.simpleMessage("Changelog"),
"Chat details" : MessageLookupByLibrary.simpleMessage("Chat details"),
@ -234,6 +235,7 @@ class MessageLookup extends MessageLookupByLibrary {
"Please enter a matrix identifier" : MessageLookupByLibrary.simpleMessage("Please enter a matrix identifier"),
"Please enter your password" : MessageLookupByLibrary.simpleMessage("Please enter your password"),
"Please enter your username" : MessageLookupByLibrary.simpleMessage("Please enter your username"),
"Public Rooms" : MessageLookupByLibrary.simpleMessage("Public Rooms"),
"Rejoin" : MessageLookupByLibrary.simpleMessage("Rejoin"),
"Remove" : MessageLookupByLibrary.simpleMessage("Remove"),
"Remove all other devices" : MessageLookupByLibrary.simpleMessage("Remove all other devices"),

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/list_items/public_room_list_item.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -8,6 +9,7 @@ import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:toast/toast.dart';
import 'package:uni_links/uni_links.dart';
import '../components/dialogs/simple_dialogs.dart';
import '../components/theme_switcher.dart';
import '../components/adaptive_page_layout.dart';
import '../components/list_items/chat_list_item.dart';
@ -51,6 +53,10 @@ class _ChatListState extends State<ChatList> {
StreamSubscription sub;
final TextEditingController searchController = TextEditingController();
SelectMode selectMode = SelectMode.normal;
Timer coolDown;
PublicRoomsResponse publicRoomsResponse;
bool loadingPublicRooms = false;
String searchServer;
Future<bool> waitForFirstSync(BuildContext context) async {
Client client = Matrix.of(context).client;
@ -64,9 +70,38 @@ class _ChatListState extends State<ChatList> {
@override
void initState() {
searchController.addListener(
() => setState(() => null),
);
searchController.addListener(() {
coolDown?.cancel();
coolDown = Timer(Duration(seconds: 1), () async {
setState(() => loadingPublicRooms = true);
final newPublicRoomsResponse =
await Matrix.of(context).tryRequestWithErrorToast(
Matrix.of(context).client.requestPublicRooms(
limit: 30,
includeAllNetworks: true,
genericSearchTerm: searchController.text,
server: searchServer,
),
);
setState(() {
loadingPublicRooms = false;
if (newPublicRoomsResponse != false) {
publicRoomsResponse = newPublicRoomsResponse;
if (searchController.text.isNotEmpty &&
searchController.text.isValidMatrixId &&
searchController.text.sigil == "#") {
publicRoomsResponse.publicRooms.add(
PublicRoomEntry(
aliases: [searchController.text],
name: searchController.text,
roomId: searchController.text),
);
}
}
});
});
setState(() => null);
});
initUniLinks();
super.initState();
}
@ -133,12 +168,38 @@ class _ChatListState extends State<ChatList> {
leading: searchMode
? IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => setState(() => searchMode = false),
onPressed: () => setState(() {
publicRoomsResponse = null;
loadingPublicRooms = false;
searchMode = false;
}),
)
: null,
automaticallyImplyLeading: false,
actions: searchMode
? null
? <Widget>[
if (loadingPublicRooms)
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Center(
child: CircularProgressIndicator(strokeWidth: 2),
),
),
IconButton(
icon: Icon(Icons.domain),
onPressed: () async {
final String newSearchServer = await SimpleDialogs(context)
.enterText(
titleText: I18n.of(context).changeTheServer,
labelText: I18n.of(context).changeTheServer,
hintText: Matrix.of(context).client.userID.domain,
prefixText: "https://");
if (newSearchServer?.isNotEmpty ?? false) {
searchServer = newSearchServer;
}
},
)
]
: <Widget>[
IconButton(
icon: Icon(Icons.search),
@ -201,8 +262,7 @@ class _ChatListState extends State<ChatList> {
foregroundColor: Colors.white,
backgroundColor: Colors.blue,
label: I18n.of(context).createNewGroup,
labelStyle:
TextStyle(fontSize: 18.0, color: Colors.black),
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewGroupView()),
(r) => r.isFirst),
@ -212,9 +272,7 @@ class _ChatListState extends State<ChatList> {
foregroundColor: Colors.white,
backgroundColor: Colors.green,
label: I18n.of(context).newPrivateChat,
labelStyle: TextStyle(
fontSize: 18.0,
color: Colors.black),
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewPrivateChatView()),
(r) => r.isFirst),
@ -231,7 +289,7 @@ class _ChatListState extends State<ChatList> {
!room.displayname
.toLowerCase()
.contains(searchController.text.toLowerCase() ?? ""));
if (rooms.isEmpty) {
if (rooms.isEmpty && (!searchMode || publicRoomsResponse == null)) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
@ -248,14 +306,27 @@ class _ChatListState extends State<ChatList> {
),
);
}
final int publicRoomsCount =
(publicRoomsResponse?.publicRooms?.length ?? 0);
final int totalCount = rooms.length + publicRoomsCount;
return ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Divider(indent: 70, height: 1),
itemCount: rooms.length,
itemBuilder: (BuildContext context, int i) => ChatListItem(
rooms[i],
activeChat: widget.activeChat == rooms[i].id,
),
i == totalCount - publicRoomsCount - 1
? Material(
elevation: 2,
child: ListTile(
title: Text("Public Rooms:"),
),
)
: Divider(indent: 70, height: 1),
itemCount: totalCount,
itemBuilder: (BuildContext context, int i) => i < rooms.length
? ChatListItem(
rooms[i],
activeChat: widget.activeChat == rooms[i].id,
)
: PublicRoomListItem(
publicRoomsResponse.publicRooms[i - rooms.length]),
);
} else {
return Center(

View File

@ -124,8 +124,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "0b5e806cdfeccae6ef49c5cfe5dfa19c1a0ff259"
resolved-ref: "0b5e806cdfeccae6ef49c5cfe5dfa19c1a0ff259"
ref: "0b0facb0e058403e3ddf79cb85f318f7acf1e593"
resolved-ref: "0b0facb0e058403e3ddf79cb85f318f7acf1e593"
url: "https://gitlab.com/famedly/famedlysdk.git"
source: git
version: "0.0.1"
@ -274,7 +274,7 @@ packages:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
version: "0.16.0"
intl_translation:
dependency: "direct main"
description:

View File

@ -27,7 +27,7 @@ dependencies:
famedlysdk:
git:
url: https://gitlab.com/famedly/famedlysdk.git
ref: 0b5e806cdfeccae6ef49c5cfe5dfa19c1a0ff259
ref: 0b0facb0e058403e3ddf79cb85f318f7acf1e593
localstorage: ^3.0.1+4
bubble: ^1.1.9+1