feat: Light implementation of spaces drawer

This commit is contained in:
Christian Pauly 2021-08-01 07:45:34 +02:00
parent 0c0602a0a7
commit d3f87aeaad
3 changed files with 335 additions and 268 deletions

View File

@ -44,6 +44,9 @@ void main() async {
KeyVerificationMethod.emoji, KeyVerificationMethod.emoji,
}, },
importantStateEvents: <String>{ importantStateEvents: <String>{
EventTypes.spaceChild,
EventTypes.spaceParent,
EventTypes.RoomCreate,
'im.ponies.room_emotes', // we want emotes to work properly 'im.ponies.room_emotes', // we want emotes to work properly
}, },
databaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, databaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder,

View File

@ -40,6 +40,17 @@ class ChatListController extends State<ChatList> {
StreamSubscription _intentUriStreamSubscription; StreamSubscription _intentUriStreamSubscription;
String _activeSpaceId;
String get activeSpaceId => _activeSpaceId;
void setActiveSpaceId(BuildContext context, String spaceId) {
Scaffold.of(context).openEndDrawer();
setState(() => _activeSpaceId = spaceId);
}
List<Room> get spaces =>
Matrix.of(context).client.rooms.where((r) => r.isSpace).toList();
final selectedRoomIds = <String>{}; final selectedRoomIds = <String>{};
Future<bool> crossSigningCachedFuture; Future<bool> crossSigningCachedFuture;
bool crossSigningCached; bool crossSigningCached;

View File

@ -1,3 +1,4 @@
import 'package:fluffychat/widgets/avatar.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:fluffychat/pages/chat_list.dart'; import 'package:fluffychat/pages/chat_list.dart';
import 'package:fluffychat/widgets/connection_status_header.dart'; import 'package:fluffychat/widgets/connection_status_header.dart';
@ -23,285 +24,337 @@ class ChatListView extends StatelessWidget {
builder: (_, __) { builder: (_, __) {
final selectMode = controller.selectMode; final selectMode = controller.selectMode;
return VWidgetGuard( return VWidgetGuard(
onSystemPop: (redirector) async { onSystemPop: (redirector) async {
final selMode = controller.selectMode; final selMode = controller.selectMode;
if (selMode != SelectMode.normal) controller.cancelAction(); if (selMode != SelectMode.normal) controller.cancelAction();
if (selMode == SelectMode.select) redirector.stopRedirection(); if (selMode == SelectMode.select) redirector.stopRedirection();
}, },
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
leading: selectMode == SelectMode.normal leading: selectMode == SelectMode.normal
? null ? controller.spaces.isEmpty
: IconButton( ? null
tooltip: L10n.of(context).cancel, : Builder(
icon: Icon(Icons.close_outlined), builder: (context) => IconButton(
onPressed: controller.cancelAction, icon: Icon(Icons.group_work_outlined),
), onPressed: Scaffold.of(context).openDrawer,
centerTitle: false, ))
actions: selectMode == SelectMode.share : IconButton(
? null tooltip: L10n.of(context).cancel,
: selectMode == SelectMode.select icon: Icon(Icons.close_outlined),
? [ onPressed: controller.cancelAction,
if (controller.selectedRoomIds.length == 1) ),
IconButton( centerTitle: false,
tooltip: L10n.of(context).toggleUnread, titleSpacing: controller.spaces.isEmpty ? null : 0,
icon: Icon(Matrix.of(context) actions: selectMode == SelectMode.share
.client ? null
.getRoomById( : selectMode == SelectMode.select
controller.selectedRoomIds.single) ? [
.isUnread if (controller.selectedRoomIds.length == 1)
? Icons.mark_chat_read_outlined
: Icons.mark_chat_unread_outlined),
onPressed: controller.toggleUnread,
),
if (controller.selectedRoomIds.length == 1)
IconButton(
tooltip: L10n.of(context).toggleFavorite,
icon: Icon(Icons.push_pin_outlined),
onPressed: controller.toggleFavouriteRoom,
),
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,
),
IconButton( IconButton(
icon: Icon(Icons.archive_outlined), tooltip: L10n.of(context).toggleUnread,
tooltip: L10n.of(context).archive, icon: Icon(Matrix.of(context)
onPressed: controller.archiveAction, .client
.getRoomById(
controller.selectedRoomIds.single)
.isUnread
? Icons.mark_chat_read_outlined
: Icons.mark_chat_unread_outlined),
onPressed: controller.toggleUnread,
), ),
] if (controller.selectedRoomIds.length == 1)
: [
IconButton( IconButton(
icon: Icon(Icons.search_outlined), tooltip: L10n.of(context).toggleFavorite,
tooltip: L10n.of(context).search, icon: Icon(Icons.push_pin_outlined),
onPressed: () => onPressed: controller.toggleFavouriteRoom,
VRouter.of(context).to('/search'),
), ),
PopupMenuButton<PopupMenuAction>( if (controller.selectedRoomIds.length == 1)
onSelected: controller.onPopupMenuSelect, IconButton(
itemBuilder: (_) => [ icon: Icon(Matrix.of(context)
PopupMenuItem( .client
value: PopupMenuAction.setStatus, .getRoomById(controller
child: Row( .selectedRoomIds.single)
mainAxisSize: MainAxisSize.min, .pushRuleState ==
children: [ PushRuleState.notify
Icon(Icons.edit_outlined), ? Icons.notifications_off_outlined
SizedBox(width: 12), : Icons.notifications_outlined),
Text(L10n.of(context).setStatus), tooltip: L10n.of(context).toggleMuted,
], onPressed: controller.toggleMuted,
),
),
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),
],
),
),
],
), ),
], IconButton(
title: Text(selectMode == SelectMode.share icon: Icon(Icons.archive_outlined),
? L10n.of(context).share tooltip: L10n.of(context).archive,
: selectMode == SelectMode.select onPressed: controller.archiveAction,
? L10n.of(context).numberSelected( ),
controller.selectedRoomIds.length.toString()) ]
: AppConfig.applicationName), : [
), IconButton(
body: Column(children: [ icon: Icon(Icons.search_outlined),
ConnectionStatusHeader(), tooltip: L10n.of(context).search,
Expanded( onPressed: () =>
child: StreamBuilder( VRouter.of(context).to('/search'),
stream: Matrix.of(context) ),
.client PopupMenuButton<PopupMenuAction>(
.onSync onSelected: controller.onPopupMenuSelect,
.stream itemBuilder: (_) => [
.where((s) => s.hasRoomUpdate) PopupMenuItem(
.rateLimit(Duration(seconds: 1)), value: PopupMenuAction.setStatus,
builder: (context, snapshot) { child: Row(
return FutureBuilder<void>(
future: controller.waitForFirstSync(),
builder: (BuildContext context, snapshot) {
if (Matrix.of(context).client.prevBatch != null) {
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;
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: Theme.of(context)
.secondaryHeaderColor,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Theme.of(
context)
.scaffoldBackgroundColor,
foregroundColor:
Theme.of(context)
.colorScheme
.secondaryVariant,
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,
);
},
);
} else {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Image.asset( Icon(Icons.edit_outlined),
'assets/private_chat_wallpaper.png', SizedBox(width: 12),
width: 100, Text(L10n.of(context).setStatus),
),
Text(
L10n.of(context)
.yourChatsAreBeingSynced,
textAlign: TextAlign.center,
),
], ],
), ),
),
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),
],
),
),
],
),
],
title: Text(selectMode == SelectMode.share
? L10n.of(context).share
: selectMode == SelectMode.select
? L10n.of(context).numberSelected(
controller.selectedRoomIds.length.toString())
: controller.activeSpaceId == null
? AppConfig.applicationName
: Matrix.of(context)
.client
.getRoomById(controller.activeSpaceId)
.displayname),
),
body: Column(children: [
ConnectionStatusHeader(),
Expanded(
child: StreamBuilder(
stream: Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.hasRoomUpdate)
.rateLimit(Duration(seconds: 1)),
builder: (context, snapshot) {
return FutureBuilder<void>(
future: controller.waitForFirstSync(),
builder: (BuildContext context, snapshot) {
if (Matrix.of(context).client.prevBatch != null) {
final rooms = List<Room>.from(
Matrix.of(context).client.rooms)
.where((r) => !r.isSpace)
.toList();
if (controller.activeSpaceId != null) {
rooms.removeWhere((room) => !room.spaceParents
.any((parent) =>
parent.roomId ==
controller.activeSpaceId));
}
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;
); return ListView.builder(
}), itemCount: totalCount,
), itemBuilder: (BuildContext context, int i) {
]), if (i == 0) {
floatingActionButton: selectMode == SelectMode.normal return FutureBuilder(
? FloatingActionButton( future:
heroTag: 'main_fab', controller.crossSigningCachedFuture,
onPressed: () => builder: (context, snapshot) {
VRouter.of(context).to('/newprivatechat'), final needsBootstrap =
child: Icon(CupertinoIcons.chat_bubble), Matrix.of(context)
) .client
: null, .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: Theme.of(context)
.secondaryHeaderColor,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Theme.of(
context)
.scaffoldBackgroundColor,
foregroundColor:
Theme.of(context)
.colorScheme
.secondaryVariant,
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,
);
},
);
} else {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
'assets/private_chat_wallpaper.png',
width: 100,
),
Text(
L10n.of(context).yourChatsAreBeingSynced,
textAlign: TextAlign.center,
),
],
),
);
}
},
);
}),
),
]),
floatingActionButton: selectMode == SelectMode.normal
? FloatingActionButton(
heroTag: 'main_fab',
onPressed: () =>
VRouter.of(context).to('/newprivatechat'),
child: Icon(CupertinoIcons.chat_bubble),
)
: null,
drawer: controller.spaces.isEmpty
? null
: Drawer(
child: SafeArea(
child: ListView.builder(
itemCount: controller.spaces.length + 1,
itemBuilder: (context, i) {
if (i == 0) {
return ListTile(
leading: CircleAvatar(
foregroundColor:
Theme.of(context).primaryColor,
backgroundColor:
Theme.of(context).secondaryHeaderColor,
radius: Avatar.defaultSize / 2,
child: Icon(Icons.home_outlined),
),
title: Text(L10n.of(context).chats),
onTap: () =>
controller.setActiveSpaceId(context, null),
);
}
final space = controller.spaces[i - 1];
return ListTile(
leading: Avatar(space.avatar, space.displayname),
title: Text(space.displayname),
onTap: () => controller.setActiveSpaceId(
context, space.id),
);
},
),
),
),
),
);
}); });
} }
} }