refactor: Chat list body code

This commit is contained in:
Christian Pauly 2022-09-10 13:32:46 +02:00
parent a4ee523849
commit 518c688c58

View File

@ -1,5 +1,3 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
@ -18,49 +16,48 @@ import '../../utils/stream_extension.dart';
import '../../widgets/connection_status_header.dart';
import '../../widgets/matrix.dart';
class ChatListViewBody extends StatefulWidget {
class ChatListViewBody extends StatelessWidget {
final ChatListController controller;
const ChatListViewBody(this.controller, {Key? key}) : super(key: key);
@override
State<ChatListViewBody> createState() => _ChatListViewBodyState();
}
class _ChatListViewBodyState extends State<ChatListViewBody> {
// the matrix sync stream
late StreamSubscription _subscription;
@override
void initState() {
_subscription = Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.hasRoomUpdate)
.rateLimit(const Duration(seconds: 1))
.listen((d) => setState(() {}));
super.initState();
}
@override
Widget build(BuildContext context) {
final roomSearchResult = widget.controller.roomSearchResult;
final userSearchResult = widget.controller.userSearchResult;
Widget child;
if (widget.controller.waitForFirstSync &&
Matrix.of(context).client.prevBatch != null &&
widget.controller.activeFilter != ActiveFilter.spaces) {
final rooms = widget.controller.filteredRooms;
final roomSearchResult = controller.roomSearchResult;
final userSearchResult = controller.userSearchResult;
final client = Matrix.of(context).client;
return PageTransitionSwitcher(
transitionBuilder: (
Widget child,
Animation<double> primaryAnimation,
Animation<double> secondaryAnimation,
) {
return SharedAxisTransition(
animation: primaryAnimation,
secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.vertical,
fillColor: Theme.of(context).scaffoldBackgroundColor,
child: child,
);
},
child: StreamBuilder(
stream: client.onSync.stream
.where((s) => s.hasRoomUpdate)
.rateLimit(const Duration(seconds: 1)),
builder: (context, _) {
if (controller.waitForFirstSync &&
client.prevBatch != null &&
controller.activeFilter != ActiveFilter.spaces) {
final rooms = controller.filteredRooms;
final displayStoriesHeader = {
ActiveFilter.allChats,
ActiveFilter.messages,
}.contains(widget.controller.activeFilter);
child = ListView.builder(
key: ValueKey(Matrix.of(context).client.userID.toString() +
widget.controller.activeFilter.toString()),
controller: widget.controller.scrollController,
}.contains(controller.activeFilter);
return ListView.builder(
key: ValueKey(client.userID.toString() +
controller.activeFilter.toString()),
controller: controller.scrollController,
// add +1 space below in order to properly scroll below the spaces bar
itemCount: rooms.length + 1,
itemBuilder: (BuildContext context, int i) {
@ -90,8 +87,8 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
onPressed: () => showModalBottomSheet(
context: context,
builder: (c) => PublicRoomBottomSheet(
roomAlias:
roomSearchResult.chunk[i].canonicalAlias ??
roomAlias: roomSearchResult
.chunk[i].canonicalAlias ??
roomSearchResult.chunk[i].roomId,
outerContext: context,
chunk: roomSearchResult.chunk[i],
@ -115,8 +112,10 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
scrollDirection: Axis.horizontal,
itemCount: userSearchResult.results.length,
itemBuilder: (context, i) => _SearchItem(
title: userSearchResult.results[i].displayName ??
userSearchResult.results[i].userId.localpart ??
title:
userSearchResult.results[i].displayName ??
userSearchResult
.results[i].userId.localpart ??
L10n.of(context)!.unknownDevice,
avatar: userSearchResult.results[i].avatarUrl,
onPressed: () => showModalBottomSheet(
@ -130,18 +129,18 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
),
),
],
if (widget.controller.isSearchMode)
if (controller.isSearchMode)
SearchTitle(
title: L10n.of(context)!.stories,
icon: const Icon(Icons.camera_alt_outlined),
),
if (displayStoriesHeader)
StoriesHeader(
filter: widget.controller.searchController.text,
filter: controller.searchController.text,
),
const ConnectionStatusHeader(),
AnimatedContainer(
height: widget.controller.isTorBrowser ? 64 : 0,
height: controller.isTorBrowser ? 64 : 0,
duration: const Duration(milliseconds: 300),
clipBehavior: Clip.hardEdge,
curve: Curves.bounceInOut,
@ -151,18 +150,20 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
child: ListTile(
leading: const Icon(Icons.vpn_key),
title: Text(L10n.of(context)!.dehydrateTor),
subtitle: Text(L10n.of(context)!.dehydrateTorLong),
trailing: const Icon(Icons.chevron_right_outlined),
onTap: widget.controller.dehydrate,
subtitle:
Text(L10n.of(context)!.dehydrateTorLong),
trailing:
const Icon(Icons.chevron_right_outlined),
onTap: controller.dehydrate,
),
),
),
if (widget.controller.isSearchMode)
if (controller.isSearchMode)
SearchTitle(
title: L10n.of(context)!.chats,
icon: const Icon(Icons.chat_outlined),
),
if (rooms.isEmpty && !widget.controller.isSearchMode)
if (rooms.isEmpty && !controller.isSearchMode)
Column(
key: const ValueKey(null),
mainAxisAlignment: MainAxisAlignment.center,
@ -191,33 +192,34 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
}
i--;
if (!rooms[i].displayname.toLowerCase().contains(
widget.controller.searchController.text.toLowerCase())) {
controller.searchController.text.toLowerCase())) {
return Container();
}
return ChatListItem(
rooms[i],
selected: widget.controller.selectedRoomIds.contains(rooms[i].id),
onTap: widget.controller.selectMode == SelectMode.select
? () => widget.controller.toggleSelection(rooms[i].id)
selected: controller.selectedRoomIds.contains(rooms[i].id),
onTap: controller.selectMode == SelectMode.select
? () => controller.toggleSelection(rooms[i].id)
: null,
onLongPress: () => widget.controller.toggleSelection(rooms[i].id),
activeChat: widget.controller.activeChat == rooms[i].id,
onLongPress: () => controller.toggleSelection(rooms[i].id),
activeChat: controller.activeChat == rooms[i].id,
);
},
);
} else if (widget.controller.activeFilter == ActiveFilter.spaces) {
child = SpaceView(
widget.controller,
scrollController: widget.controller.scrollController,
key: Key(widget.controller.activeSpaceId ?? 'Spaces'),
}
if (controller.activeFilter == ActiveFilter.spaces) {
return SpaceView(
controller,
scrollController: controller.scrollController,
key: Key(controller.activeSpaceId ?? 'Spaces'),
);
} else {
}
const dummyChatCount = 5;
final titleColor =
Theme.of(context).textTheme.bodyText1!.color!.withAlpha(100);
final subtitleColor =
Theme.of(context).textTheme.bodyText1!.color!.withAlpha(50);
child = ListView.builder(
return ListView.builder(
key: const Key('dummychats'),
itemCount: dummyChatCount,
itemBuilder: (context, i) => Opacity(
@ -272,35 +274,8 @@ class _ChatListViewBodyState extends State<ChatListViewBody> {
),
),
);
}
return PageTransitionSwitcher(
transitionBuilder: (
Widget child,
Animation<double> primaryAnimation,
Animation<double> secondaryAnimation,
) {
return SharedAxisTransition(
animation: primaryAnimation,
secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.vertical,
fillColor: Theme.of(context).scaffoldBackgroundColor,
child: child,
}),
);
},
child: child,
);
}
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
@override
void didUpdateWidget(covariant ChatListViewBody oldWidget) {
setState(() {});
super.didUpdateWidget(oldWidget);
}
}