Perf round 4: lazy chat sessions list
Chat sections are slivers: a lazy, keyed SliverList under the same heading, with SliverAnimatedExtent reproducing the clipped AnimatedSize for open, close and arriving rows. The first Chat tab visit no longer builds every row (150 chats: worst build frame 116 ms to 19 ms).
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import '../../../../ui/layout/collapsible_list_section.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'session_row_memo.dart';
|
||||
import 'session_list_items.dart';
|
||||
import '../../../../ui/adaptive/floating_bar_layout.dart';
|
||||
|
||||
class CupertinoSessionList extends StatelessWidget {
|
||||
@@ -44,79 +42,52 @@ class CupertinoSessionList extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
// Starts below a floating top bar (its height arrives as media top
|
||||
// padding) and ends clear of a floating bottom bar.
|
||||
padding: withFloatingBarClearance(
|
||||
context,
|
||||
EdgeInsets.only(top: MediaQuery.paddingOf(context).top, bottom: 24),
|
||||
),
|
||||
children: [
|
||||
for (final section in sections) ..._section(section),
|
||||
if (emptyMessage != null)
|
||||
ChatCupertinoMessage(
|
||||
message: emptyMessage!,
|
||||
actionLabel: onClearSearch == null ? null : 'Clear search',
|
||||
onAction: onClearSearch,
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
SliverPadding(
|
||||
// Starts below a floating top bar (its height arrives as media top
|
||||
// padding) and ends clear of a floating bottom bar.
|
||||
padding: withFloatingBarClearance(
|
||||
context,
|
||||
EdgeInsets.only(top: MediaQuery.paddingOf(context).top, bottom: 24),
|
||||
),
|
||||
sliver: SliverMainAxisGroup(
|
||||
slivers: [
|
||||
for (final section in sections) _section(section),
|
||||
if (emptyMessage != null)
|
||||
SliverToBoxAdapter(
|
||||
child: ChatCupertinoMessage(
|
||||
message: emptyMessage!,
|
||||
actionLabel: onClearSearch == null ? null : 'Clear search',
|
||||
onAction: onClearSearch,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _section(ChatSessionSection section) {
|
||||
final groups = groupChatSessions(section.sessions);
|
||||
// Closed chats start folded away; the live ones start open.
|
||||
return [
|
||||
CollapsibleListSection(
|
||||
key: ValueKey('chat-section-${section.title}'),
|
||||
title: section.title,
|
||||
count: section.sessions.length,
|
||||
initiallyOpen: section.title != 'Closed',
|
||||
children: [
|
||||
for (final group in groups) ...[
|
||||
if (groups.length > 1)
|
||||
_DirectoryHeader(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
),
|
||||
for (var i = 0; i < group.sessions.length; i++)
|
||||
..._row(
|
||||
group.sessions[i],
|
||||
divider: i < group.sessions.length - 1,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/// The row plus its refusal, the server's words in their own full-width
|
||||
/// row under it.
|
||||
List<Widget> _row(JsonMap session, {required bool divider}) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
final error = errorFor(id);
|
||||
return [
|
||||
memoChatSessionRow(
|
||||
session: session,
|
||||
selected: id == activeSessionId,
|
||||
duplicateTitles: duplicateTitles,
|
||||
unseen: isUnseen(session),
|
||||
// A refusal below the row separates it instead.
|
||||
divider: divider && error == null,
|
||||
onOpen: onOpen,
|
||||
onStar: onStar,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onHide: onHide,
|
||||
),
|
||||
if (error != null)
|
||||
ChatRowError(
|
||||
message: error,
|
||||
messageKey: Key('chat-row-error-$id'),
|
||||
onDismiss: onDismissError == null ? null : () => onDismissError!(id),
|
||||
),
|
||||
];
|
||||
}
|
||||
Widget _section(ChatSessionSection section) => chatSessionSection(
|
||||
section: section,
|
||||
directoryHeader: _DirectoryHeader.new,
|
||||
row: (session, {required divider}) => chatSessionRowItem(
|
||||
session,
|
||||
divider: divider,
|
||||
activeSessionId: activeSessionId,
|
||||
duplicateTitles: duplicateTitles,
|
||||
isUnseen: isUnseen,
|
||||
onOpen: onOpen,
|
||||
onStar: onStar,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onHide: onHide,
|
||||
),
|
||||
errorFor: errorFor,
|
||||
onDismissError: onDismissError,
|
||||
);
|
||||
}
|
||||
|
||||
/// A directory's name above its chats, like a grouped table's header.
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../../../ui/layout/collapsible_list_section.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'session_row_memo.dart';
|
||||
|
||||
/// One entry of a chat section's lazy list: a directory header, a row or a
|
||||
/// row's refusal.
|
||||
typedef SessionListItem = ({Key key, WidgetBuilder build});
|
||||
|
||||
/// A chat section as a sliver: its heading and its rows, built only when on
|
||||
/// screen. Material and Cupertino supply their own directory header.
|
||||
Widget chatSessionSection({
|
||||
required ChatSessionSection section,
|
||||
required Widget Function(String directory) directoryHeader,
|
||||
required SessionListItem Function(JsonMap session, {required bool divider})
|
||||
row,
|
||||
required String? Function(String sessionId) errorFor,
|
||||
required ValueChanged<String>? onDismissError,
|
||||
}) {
|
||||
final groups = groupChatSessions(section.sessions);
|
||||
final items = <SessionListItem>[
|
||||
for (final group in groups) ...[
|
||||
if (groups.length > 1)
|
||||
(
|
||||
key: ValueKey('chat-dir-${group.directory}'),
|
||||
build: (_) => directoryHeader(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
),
|
||||
),
|
||||
// Inset dividers between the rows of a group; the group header owns
|
||||
// the boundary between directories.
|
||||
for (var i = 0; i < group.sessions.length; i++)
|
||||
...() {
|
||||
final session = group.sessions[i];
|
||||
final id = '${session['id'] ?? ''}';
|
||||
final error = errorFor(id);
|
||||
return [
|
||||
// A refusal below the row separates it instead.
|
||||
row(
|
||||
session,
|
||||
divider: i < group.sessions.length - 1 && error == null,
|
||||
),
|
||||
// The refusal at full width under its row (a long sentence in a
|
||||
// subtitle made the row taller than the viewport at 320px with
|
||||
// 2x text).
|
||||
if (error != null)
|
||||
(
|
||||
key: ValueKey('chat-error-$id'),
|
||||
build: (_) => ChatRowError(
|
||||
message: error,
|
||||
messageKey: Key('chat-row-error-$id'),
|
||||
onDismiss: onDismissError == null
|
||||
? null
|
||||
: () => onDismissError(id),
|
||||
),
|
||||
),
|
||||
];
|
||||
}(),
|
||||
],
|
||||
];
|
||||
// Closed chats start folded away; the live ones start open.
|
||||
return SliverCollapsibleListSection(
|
||||
key: ValueKey('chat-section-${section.title}'),
|
||||
title: section.title,
|
||||
count: section.sessions.length,
|
||||
initiallyOpen: section.title != 'Closed',
|
||||
itemCount: items.length,
|
||||
itemKey: (index) => items[index].key,
|
||||
itemBuilder: (context, index) => items[index].build(context),
|
||||
);
|
||||
}
|
||||
|
||||
/// A chat row as a list entry, memoised (see [memoChatSessionRow]).
|
||||
SessionListItem chatSessionRowItem(
|
||||
JsonMap session, {
|
||||
required bool divider,
|
||||
required String? activeSessionId,
|
||||
required Set<String> duplicateTitles,
|
||||
required bool Function(JsonMap) isUnseen,
|
||||
required ValueChanged<JsonMap> onOpen,
|
||||
required ValueChanged<JsonMap> onStar,
|
||||
required ValueChanged<JsonMap> onRename,
|
||||
required ValueChanged<JsonMap> onClose,
|
||||
required ValueChanged<JsonMap> onReopen,
|
||||
required ValueChanged<JsonMap> onHide,
|
||||
}) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
return (
|
||||
key: ValueKey('chat-item-$id'),
|
||||
build: (_) => memoChatSessionRow(
|
||||
session: session,
|
||||
selected: id == activeSessionId,
|
||||
duplicateTitles: duplicateTitles,
|
||||
unseen: isUnseen(session),
|
||||
divider: divider,
|
||||
onOpen: onOpen,
|
||||
onStar: onStar,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onHide: onHide,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
import '../../../../ui/layout/collapsible_list_section.dart';
|
||||
import '../../../../ui/layout/section_heading.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'session_row_memo.dart';
|
||||
import 'session_list_items.dart';
|
||||
|
||||
class MaterialSessionList extends StatelessWidget {
|
||||
const MaterialSessionList({
|
||||
@@ -44,65 +41,59 @@ class MaterialSessionList extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
children: [
|
||||
for (final section in sections) _section(context, section),
|
||||
if (emptyMessage != null)
|
||||
ListTile(
|
||||
title: Text(emptyMessage!),
|
||||
trailing: onClearSearch == null
|
||||
? null
|
||||
: TextButton(
|
||||
onPressed: onClearSearch,
|
||||
child: const Text('Clear search'),
|
||||
// Clears the media padding above and below (a floating top bar's height
|
||||
// arrives as top padding) and leaves only the side padding to the rows,
|
||||
// as a ListView does.
|
||||
final media = MediaQuery.of(context);
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
SliverPadding(
|
||||
padding: media.padding.copyWith(left: 0, right: 0),
|
||||
sliver: MediaQuery(
|
||||
data: media.copyWith(
|
||||
padding: media.padding.copyWith(top: 0, bottom: 0),
|
||||
),
|
||||
child: SliverMainAxisGroup(
|
||||
slivers: [
|
||||
for (final section in sections) _section(context, section),
|
||||
if (emptyMessage != null)
|
||||
SliverToBoxAdapter(
|
||||
child: ListTile(
|
||||
title: Text(emptyMessage!),
|
||||
trailing: onClearSearch == null
|
||||
? null
|
||||
: TextButton(
|
||||
onPressed: onClearSearch,
|
||||
child: const Text('Clear search'),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _section(BuildContext context, ChatSessionSection section) {
|
||||
final groups = groupChatSessions(section.sessions);
|
||||
final heading = SectionHeadingStyle.of(context);
|
||||
// Closed chats start folded away; the live ones start open.
|
||||
return CollapsibleListSection(
|
||||
key: ValueKey('chat-section-${section.title}'),
|
||||
title: section.title,
|
||||
count: section.sessions.length,
|
||||
initiallyOpen: section.title != 'Closed',
|
||||
children: [
|
||||
for (final group in groups) ...[
|
||||
if (groups.length > 1)
|
||||
ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
style: heading.title.copyWith(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
// Inset dividers between the rows of a group; the group header
|
||||
// owns the boundary between directories.
|
||||
for (var i = 0; i < group.sessions.length; i++)
|
||||
..._row(group.sessions[i], divider: i < group.sessions.length - 1),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// The row plus its refusal at full width (a long sentence in a subtitle
|
||||
/// made the row taller than the viewport at 320px with 2x text).
|
||||
List<Widget> _row(JsonMap session, {required bool divider}) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
final error = errorFor(id);
|
||||
return [
|
||||
memoChatSessionRow(
|
||||
session: session,
|
||||
selected: id == activeSessionId,
|
||||
return chatSessionSection(
|
||||
section: section,
|
||||
directoryHeader: (directory) => ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(
|
||||
directory,
|
||||
style: heading.title.copyWith(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
row: (session, {required divider}) => chatSessionRowItem(
|
||||
session,
|
||||
divider: divider,
|
||||
activeSessionId: activeSessionId,
|
||||
duplicateTitles: duplicateTitles,
|
||||
unseen: isUnseen(session),
|
||||
// A refusal below the row separates it instead.
|
||||
divider: divider && error == null,
|
||||
isUnseen: isUnseen,
|
||||
onOpen: onOpen,
|
||||
onStar: onStar,
|
||||
onRename: onRename,
|
||||
@@ -110,12 +101,8 @@ class MaterialSessionList extends StatelessWidget {
|
||||
onReopen: onReopen,
|
||||
onHide: onHide,
|
||||
),
|
||||
if (error != null)
|
||||
ChatRowError(
|
||||
message: error,
|
||||
messageKey: Key('chat-row-error-$id'),
|
||||
onDismiss: onDismissError == null ? null : () => onDismissError!(id),
|
||||
),
|
||||
];
|
||||
errorFor: errorFor,
|
||||
onDismissError: onDismissError,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user