fluffychat/lib/pages/views/chat_list_view.dart

308 lines
16 KiB
Dart
Raw Normal View History

import 'package:matrix/matrix.dart';
2021-05-22 08:57:49 +02:00
import 'package:fluffychat/pages/chat_list.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/connection_status_header.dart';
import 'package:fluffychat/widgets/list_items/chat_list_item.dart';
2021-04-14 14:09:46 +02:00
import 'package:flutter/cupertino.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-05-22 08:53:52 +02:00
import '../../widgets/matrix.dart';
2021-04-14 14:09:46 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-05-22 09:13:47 +02:00
class ChatListView extends StatelessWidget {
2021-04-14 14:09:46 +02:00
final ChatListController controller;
2021-05-22 09:13:47 +02:00
const ChatListView(this.controller, {Key key}) : super(key: key);
2021-04-14 14:09:46 +02:00
@override
Widget build(BuildContext context) {
return StreamBuilder<Object>(
stream: Matrix.of(context).onShareContentChanged.stream,
builder: (_, __) {
final selectMode = controller.selectMode;
return VWidgetGuard(
2021-05-26 20:50:15 +02:00
onSystemPop: (redirector) async {
final selMode = controller.selectMode;
if (selMode != SelectMode.normal) controller.cancelAction();
if (selMode == SelectMode.select) redirector.stopRedirection();
2021-05-26 20:50:15 +02:00
},
child: Scaffold(
appBar: AppBar(
leading: selectMode == SelectMode.normal
? null
: IconButton(
tooltip: L10n.of(context).cancel,
icon: Icon(Icons.close_outlined),
onPressed: controller.cancelAction,
),
centerTitle: false,
actions: selectMode == SelectMode.share
? null
: selectMode == SelectMode.select
? [
if (controller.selectedRoomIds.length == 1)
IconButton(
tooltip: L10n.of(context).toggleUnread,
icon: Icon(Matrix.of(context)
2021-04-14 14:09:46 +02:00
.client
.getRoomById(
controller.selectedRoomIds.single)
2021-05-26 20:50:15 +02:00
.isUnread
? Icons.mark_chat_read_outlined
: Icons.mark_chat_unread_outlined),
onPressed: controller.toggleUnread,
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
if (controller.selectedRoomIds.length == 1)
IconButton(
tooltip: L10n.of(context).toggleFavorite,
icon: Icon(Icons.push_pin_outlined),
onPressed: controller.toggleFavouriteRoom,
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
if (controller.selectedRoomIds.length == 1)
IconButton(
icon: Icon(Matrix.of(context)
.client
.getRoomById(controller
.selectedRoomIds.single)
.pushRuleState ==
PushRuleState.notify
? Icons.notifications_off_outlined
: Icons.notifications_outlined),
tooltip: L10n.of(context).toggleMuted,
onPressed: controller.toggleMuted,
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
IconButton(
icon: Icon(Icons.archive_outlined),
tooltip: L10n.of(context).archive,
onPressed: controller.archiveAction,
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
]
: [
IconButton(
icon: Icon(Icons.search_outlined),
tooltip: L10n.of(context).search,
onPressed: () =>
VRouter.of(context).push('/search'),
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
PopupMenuButton<PopupMenuAction>(
onSelected: controller.onPopupMenuSelect,
itemBuilder: (_) => [
PopupMenuItem(
value: PopupMenuAction.setStatus,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.edit_outlined),
SizedBox(width: 12),
Text(L10n.of(context).setStatus),
],
),
2021-04-14 14:09:46 +02:00
),
2021-05-26 20:50:15 +02:00
PopupMenuItem(
value: PopupMenuAction.newGroup,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.group_add_outlined),
SizedBox(width: 12),
Text(L10n.of(context).createNewGroup),
],
),
),
PopupMenuItem(
value: PopupMenuAction.invite,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.share_outlined),
SizedBox(width: 12),
Text(L10n.of(context).inviteContact),
],
),
),
PopupMenuItem(
value: PopupMenuAction.archive,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.archive_outlined),
SizedBox(width: 12),
Text(L10n.of(context).archive),
],
),
),
PopupMenuItem(
value: PopupMenuAction.settings,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.settings_outlined),
SizedBox(width: 12),
Text(L10n.of(context).settings),
],
2021-04-14 14:09:46 +02:00
),
),
],
),
2021-05-26 20:50:15 +02:00
],
title: Text(selectMode == SelectMode.share
? L10n.of(context).share
: selectMode == SelectMode.select
? L10n.of(context).numberSelected(
controller.selectedRoomIds.length.toString())
: AppConfig.applicationName),
),
body: Column(children: [
ConnectionStatusHeader(),
Expanded(
child: StreamBuilder(
stream: Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.hasRoomUpdate),
builder: (context, snapshot) {
return FutureBuilder<void>(
future: controller.waitForFirstSync(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
final rooms = List<Room>.from(
Matrix.of(context).client.rooms);
rooms.removeWhere(
(room) => room.lastEvent == null);
if (rooms.isEmpty) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.maps_ugc_outlined,
size: 80,
color: Colors.grey,
),
Center(
child: Text(
L10n.of(context).startYourFirstChat,
textAlign: TextAlign.start,
style: TextStyle(
color: Colors.grey,
fontSize: 16,
),
),
),
],
);
}
final totalCount = rooms.length + 1;
2021-05-26 20:50:15 +02:00
return ListView.builder(
itemCount: totalCount,
itemBuilder: (BuildContext context, int i) {
if (i == 0) {
return FutureBuilder(
future: controller
.crossSigningCachedFuture,
builder: (context, snapshot) {
final needsBootstrap =
Matrix.of(context)
.client
.encryption
?.crossSigning
?.enabled ==
false ||
snapshot.data == false;
final isUnknownSession =
Matrix.of(context)
.client
.isUnknownSession;
final displayHeader =
needsBootstrap ||
isUnknownSession;
if (!displayHeader ||
controller
.hideChatBackupBanner) {
return Container();
}
return Material(
color: Colors.orange,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
child: Icon(Icons.cloud),
),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: controller
.hideChatBackupBannerAction,
),
title: Text(L10n.of(context)
.chatBackup),
subtitle: Text(L10n.of(context)
.enableChatBackup),
onTap: controller
.firstRunBootstrapAction,
),
);
});
}
i--;
return ChatListItem(
rooms[i],
selected: controller.selectedRoomIds
.contains(rooms[i].id),
onTap: selectMode == SelectMode.select
? () => controller
.toggleSelection(rooms[i].id)
: null,
onLongPress: () => controller
.toggleSelection(rooms[i].id),
activeChat:
controller.activeChat == rooms[i].id,
);
},
2021-05-26 20:50:15 +02:00
);
} else {
return Center(
2021-06-20 12:57:46 +02:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
'assets/private_chat_wallpaper.png',
width: 100,
),
Text(
L10n.of(context)
.yourChatsAreBeingSynced,
textAlign: TextAlign.center,
),
],
),
2021-05-26 20:50:15 +02:00
);
}
},
);
}),
),
]),
floatingActionButton: selectMode == SelectMode.normal
2021-05-30 15:37:39 +02:00
? FloatingActionButton(
2021-05-30 15:43:03 +02:00
heroTag: 'main_fab',
onPressed: () =>
VRouter.of(context).push('/newprivatechat'),
child: Icon(CupertinoIcons.chat_bubble),
)
2021-05-26 20:50:15 +02:00
: null,
));
2021-04-14 14:09:46 +02:00
});
}
}
enum ChatListPopupMenuItemActions {
createGroup,
discover,
setStatus,
inviteContact,
settings,
}