refactor: ChatListView and enforce bootstrap

This commit is contained in:
Christian Pauly 2021-08-01 15:21:02 +02:00
parent e995ff0eaf
commit 648e690a0d
2 changed files with 98 additions and 142 deletions

View File

@ -154,6 +154,18 @@ class ChatListController extends State<ChatList> {
@override
void initState() {
_initReceiveSharingIntent();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (!Matrix.of(context).client.encryptionEnabled) return;
final crossSigning = await crossSigningCachedFuture;
final needsBootstrap =
Matrix.of(context).client.encryption?.crossSigning?.enabled ==
false ||
crossSigning == false;
final isUnknownSession = Matrix.of(context).client.isUnknownSession;
if (needsBootstrap || isUnknownSession) {
firstRunBootstrapAction();
}
});
super.initState();
}

View File

@ -180,148 +180,7 @@ class ChatListView extends StatelessWidget {
),
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) {
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,
children: [
Image.asset(
'assets/private_chat_wallpaper.png',
width: 100,
),
Text(
L10n.of(context).yourChatsAreBeingSynced,
textAlign: TextAlign.center,
),
],
),
);
}
},
);
}),
),
Expanded(child: _ChatListViewBody(controller)),
]),
floatingActionButton: selectMode == SelectMode.normal
? FloatingActionButton(
@ -375,6 +234,91 @@ class ChatListView extends StatelessWidget {
}
}
class _ChatListViewBody extends StatelessWidget {
final ChatListController controller;
const _ChatListViewBody(this.controller, {Key key}) : super(key: key);
@override
Widget build(BuildContext context) => 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;
return ListView.builder(
itemCount: totalCount,
itemBuilder: (BuildContext context, int i) {
return ChatListItem(
rooms[i],
selected: controller.selectedRoomIds.contains(rooms[i].id),
onTap: controller.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,
),
],
),
);
}
},
);
});
}
enum ChatListPopupMenuItemActions {
createGroup,
createSpace,