fluffychat/lib/views/chat_list.dart

343 lines
12 KiB
Dart
Raw Normal View History

2020-01-02 15:10:21 +01:00
import 'dart:async';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-02-22 20:05:04 +01:00
import 'package:fluffychat/components/list_items/public_room_list_item.dart';
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2020-02-10 14:14:55 +01:00
import 'package:flutter/services.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
2020-02-10 14:14:55 +01:00
import 'package:toast/toast.dart';
import 'package:uni_links/uni_links.dart';
2020-01-01 19:10:13 +01:00
2020-02-22 20:05:04 +01:00
import '../components/dialogs/simple_dialogs.dart';
2020-02-16 20:11:39 +01:00
import '../components/theme_switcher.dart';
import '../components/adaptive_page_layout.dart';
import '../components/list_items/chat_list_item.dart';
import '../components/matrix.dart';
import '../i18n/i18n.dart';
import '../utils/app_route.dart';
import '../utils/url_launcher.dart';
import 'archive.dart';
import 'new_group.dart';
import 'new_private_chat.dart';
import 'settings.dart';
enum SelectMode { normal, share }
2020-01-17 11:37:02 +01:00
2020-01-01 19:10:13 +01:00
class ChatListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.FIRST,
firstScaffold: ChatList(),
secondScaffold: Scaffold(
body: Center(
2020-01-03 13:51:18 +01:00
child: Image.asset("assets/logo.png", width: 100, height: 100),
2020-01-01 19:10:13 +01:00
),
),
);
}
}
class ChatList extends StatefulWidget {
final String activeChat;
const ChatList({this.activeChat, Key key}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
_ChatListState createState() => _ChatListState();
}
class _ChatListState extends State<ChatList> {
2020-01-02 22:55:12 +01:00
bool searchMode = false;
2020-01-02 15:10:21 +01:00
StreamSubscription sub;
2020-01-02 22:55:12 +01:00
final TextEditingController searchController = TextEditingController();
2020-01-17 11:37:02 +01:00
SelectMode selectMode = SelectMode.normal;
2020-02-22 20:05:04 +01:00
Timer coolDown;
PublicRoomsResponse publicRoomsResponse;
bool loadingPublicRooms = false;
String searchServer;
2020-01-01 19:10:13 +01:00
2020-01-02 15:10:21 +01:00
Future<bool> waitForFirstSync(BuildContext context) async {
2020-01-01 19:10:13 +01:00
Client client = Matrix.of(context).client;
2020-01-02 22:31:39 +01:00
if (client.prevBatch?.isEmpty ?? true) {
2020-01-02 15:10:21 +01:00
await client.onFirstSync.stream.first;
2020-01-02 22:31:39 +01:00
}
sub ??= client.onSync.stream
.listen((s) => mounted ? setState(() => null) : null);
2020-01-02 15:10:21 +01:00
return true;
2020-01-01 19:10:13 +01:00
}
2020-01-02 22:55:12 +01:00
@override
void initState() {
2020-02-22 20:05:04 +01:00
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(
2020-02-23 09:52:43 +01:00
aliases: [searchController.text],
name: searchController.text,
roomId: searchController.text,
client: Matrix.of(context).client,
),
2020-02-22 20:05:04 +01:00
);
}
}
});
});
setState(() => null);
});
2020-02-10 14:14:55 +01:00
initUniLinks();
2020-01-02 22:55:12 +01:00
super.initState();
}
2020-01-19 19:28:12 +01:00
StreamSubscription _intentDataStreamSubscription;
2020-02-10 14:14:55 +01:00
StreamSubscription _onUniLinksub;
2020-01-19 19:28:12 +01:00
2020-02-10 14:14:55 +01:00
Future<void> initUniLinks() async {
if (kIsWeb) return;
_onUniLinksub ??= getLinksStream().listen(
(String initialLink) {
try {
if (initialLink?.isEmpty ?? true) return;
if (initialLink.startsWith("https://matrix.to/#/")) {
UrlLauncher(context, initialLink).openMatrixToUrl();
}
} on PlatformException {
debugPrint("initUniLinks failed during platform exception");
}
},
onError: (error) => Toast.show(
I18n.of(context).oopsSomethingWentWrong + " " + error.toString(),
context,
2020-02-10 14:14:55 +01:00
duration: 5),
);
2020-01-19 19:28:12 +01:00
}
2020-01-01 19:10:13 +01:00
@override
void dispose() {
2020-01-02 15:10:21 +01:00
sub?.cancel();
2020-01-02 22:55:12 +01:00
searchController.removeListener(
() => setState(() => null),
);
2020-01-19 19:28:12 +01:00
_intentDataStreamSubscription?.cancel();
2020-02-10 14:14:55 +01:00
_onUniLinksub?.cancel();
2020-01-01 19:10:13 +01:00
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-01-17 11:37:02 +01:00
if (Matrix.of(context).shareContent != null) {
selectMode = SelectMode.share;
} else if (selectMode == SelectMode.share) {
setState(() => selectMode = SelectMode.normal);
}
2020-01-01 19:10:13 +01:00
return Scaffold(
appBar: AppBar(
2020-01-02 22:55:12 +01:00
title: searchMode
? TextField(
autofocus: true,
autocorrect: false,
controller: searchController,
decoration: InputDecoration(
border: InputBorder.none,
2020-01-20 13:46:39 +01:00
hintText: I18n.of(context).searchForAChat,
2020-01-02 22:55:12 +01:00
),
)
: Text(
2020-01-20 13:46:39 +01:00
selectMode == SelectMode.share
? I18n.of(context).share
: I18n.of(context).fluffychat,
2020-01-01 19:10:13 +01:00
),
2020-01-02 22:55:12 +01:00
leading: searchMode
? IconButton(
icon: Icon(Icons.arrow_back),
2020-02-22 20:05:04 +01:00
onPressed: () => setState(() {
publicRoomsResponse = null;
loadingPublicRooms = false;
searchMode = false;
}),
2020-01-02 22:55:12 +01:00
)
: null,
2020-01-03 12:37:16 +01:00
automaticallyImplyLeading: false,
2020-01-02 22:55:12 +01:00
actions: searchMode
2020-02-22 20:05:04 +01:00
? <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;
}
},
)
]
2020-01-02 22:55:12 +01:00
: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () => setState(() => searchMode = true),
),
2020-01-17 11:37:02 +01:00
if (selectMode == SelectMode.share)
IconButton(
icon: Icon(Icons.close),
onPressed: () {
Matrix.of(context).shareContent = null;
setState(() => selectMode = SelectMode.normal);
},
),
if (selectMode == SelectMode.normal)
PopupMenuButton(
onSelected: (String choice) {
switch (choice) {
case "settings":
Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
SettingsView(),
),
(r) => r.isFirst,
);
break;
case "archive":
Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
Archive(),
),
(r) => r.isFirst,
);
break;
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
2020-01-20 13:46:39 +01:00
PopupMenuItem<String>(
2020-01-17 11:37:02 +01:00
value: "archive",
2020-01-20 13:46:39 +01:00
child: Text(I18n.of(context).archive),
2020-01-17 11:37:02 +01:00
),
2020-01-20 13:46:39 +01:00
PopupMenuItem<String>(
2020-01-17 11:37:02 +01:00
value: "settings",
2020-01-20 13:46:39 +01:00
child: Text(I18n.of(context).settings),
2020-01-17 11:37:02 +01:00
),
],
),
2020-01-02 22:55:12 +01:00
],
2020-01-01 19:10:13 +01:00
),
floatingActionButton: SpeedDial(
child: Icon(Icons.add),
overlayColor: blackWhiteColor(context),
2020-02-16 20:13:55 +01:00
foregroundColor: Colors.white,
2020-01-03 11:57:00 +01:00
backgroundColor: Theme.of(context).primaryColor,
2020-01-01 19:10:13 +01:00
children: [
SpeedDialChild(
child: Icon(Icons.people_outline),
2020-02-16 20:13:55 +01:00
foregroundColor: Colors.white,
2020-01-01 19:10:13 +01:00
backgroundColor: Colors.blue,
2020-01-20 13:46:39 +01:00
label: I18n.of(context).createNewGroup,
2020-02-22 20:05:04 +01:00
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
2020-01-27 10:14:38 +01:00
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewGroupView()),
(r) => r.isFirst),
2020-01-01 19:10:13 +01:00
),
SpeedDialChild(
2020-01-02 22:55:12 +01:00
child: Icon(Icons.person_add),
2020-02-16 20:13:55 +01:00
foregroundColor: Colors.white,
2020-01-01 19:10:13 +01:00
backgroundColor: Colors.green,
2020-01-20 13:46:39 +01:00
label: I18n.of(context).newPrivateChat,
2020-02-22 20:05:04 +01:00
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
2020-01-27 10:14:38 +01:00
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewPrivateChatView()),
(r) => r.isFirst),
2020-01-01 19:10:13 +01:00
),
],
),
2020-01-02 15:10:21 +01:00
body: FutureBuilder<bool>(
future: waitForFirstSync(context),
2020-01-01 19:10:13 +01:00
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
2020-01-02 22:55:12 +01:00
List<Room> rooms = List<Room>.from(Matrix.of(context).client.rooms);
rooms.removeWhere((Room room) =>
searchMode &&
!room.displayname
.toLowerCase()
.contains(searchController.text.toLowerCase() ?? ""));
2020-02-22 20:05:04 +01:00
if (rooms.isEmpty && (!searchMode || publicRoomsResponse == null)) {
2020-01-02 22:55:12 +01:00
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
2020-01-27 11:25:16 +01:00
searchMode ? Icons.search : Icons.chat_bubble_outline,
2020-01-02 22:55:12 +01:00
size: 80,
color: Colors.grey,
),
Text(searchMode
2020-01-20 13:46:39 +01:00
? I18n.of(context).noRoomsFound
: I18n.of(context).startYourFirstChat),
2020-01-02 22:55:12 +01:00
],
),
);
}
2020-02-22 20:05:04 +01:00
final int publicRoomsCount =
(publicRoomsResponse?.publicRooms?.length ?? 0);
final int totalCount = rooms.length + publicRoomsCount;
2020-01-03 14:47:42 +01:00
return ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
2020-02-22 20:05:04 +01:00
i == totalCount - publicRoomsCount - 1
? Material(
elevation: 2,
child: ListTile(
2020-02-23 08:49:58 +01:00
title: Text(I18n.of(context).publicRooms),
2020-02-22 20:05:04 +01:00
),
)
: 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]),
2020-01-01 19:10:13 +01:00
);
2020-01-02 22:31:39 +01:00
} else {
2020-01-01 19:10:13 +01:00
return Center(
child: CircularProgressIndicator(),
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
},
),
);
}
}