Files
buzz/mobile/lib/features/channels/add_members_sheet.dart
Tom Brow 5f59940544 fix(mobile): steady New DM picker rows and animate picks (#9249)
On iOS, the New DM (and Add Members) picker is a native UIKit sheet that
Flutter drives by pushing the picker's whole state on every rebuild.
Each push called `reloadData`, which rebuilt every visible row in a
fresh, unreused cell showing the initials fallback, then fetched each
avatar again over the platform channel. So whenever anything refreshed
(several refreshes land right after foregrounding), every avatar blinked
to its initial for a frame or two. Picks also jumped instantly from the
results to the chosen section.

Now the picker:

- ignores a pushed state identical to the one it shows;
- applies the rest as a diffable snapshot, so unchanged rows keep their
cells and changed rows are redrawn in place, in reused cells;
- keeps loaded avatars by avatar key for the life of the sheet, so a
redrawn row shows its avatar at once;
- animates a member's row moving between the chosen and result sections
when it's picked or removed (no animation with Reduce Motion).

Flutter's toggle handlers in both pickers now read the current selection
rather than the one captured at build time. A fast second tap could
otherwise add the same person twice, which the diffed table would
reject.

### Validation

- Native member picker test (`scripts/test-mobile-member-picker.sh`),
extended: after an avatar loads, a changed row is redrawn with that
avatar and requests nothing more. The avatar check fails on `main` and
passes here; the existing identity and accessibility checks still pass
with the diffable table.
- Added a New DM test: two toggles before Flutter rebuilds never list a
person twice. It fails without the toggle fix.
- Full mobile Flutter suite and analyzer pass in CI on the current head,
which includes `main`'s fix for the right-drag link tests.
- Simulator runs of the app (iPhone 17 Pro, iOS 27, signed in to a test
community): opened New DM, backgrounded and foregrounded the app,
searched, picked from the list and from a search, and removed a pick.
Without this change, every avatar flashed to its initial after a
foreground. With it, no avatar flashed at any point, and picks and
removals animate between sections. Recordings show real community
members, so they're shared in Buzz rather than here.

#### Full-app comparison, `main` vs this PR

The same scripted New DM sequence on a clone of the same signed-in
simulator (iPhone 17 Pro, iOS 27), first on `main` (65e864301d) and then
on this PR (330805cfc4): pick george, then glm; search "ju", pick judge,
clear the search; remove george; background and reopen the app. The list
shows only agents, no people.

Both builds end in the same state: each pick moves into the selected
section with its check mark, search narrows to one result and clearing
it restores the full list, the removed pick returns to the list, and
every avatar keeps rendering after the app reopens. Sampled at 6 frames
per second around the reopen, neither build fell back to initials in
this run. The visible difference is motion: on `main` a picked row jumps
into the selected section, and here it animates there.

Before (`main`):


https://github.com/user-attachments/assets/12d88f47-0c10-4e78-a12c-18235e068f88

After (this PR):


https://github.com/user-attachments/assets/b3d8edb1-94ad-44dd-b6b3-3db22d8311b0

### Before merge

- [x] Update onto `main` once main's link swipe tests are fixed (they
were fixed by [#9255 Solo chat
settings](https://github.com/block/buzz/pull/9255)), so Mobile Checks
can pass.

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-09 07:34:02 -08:00

461 lines
18 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:buzz/shared/theme/buzz_icons.dart';
import '../../shared/theme/theme.dart';
import '../../shared/widgets/app_list_card_item.dart';
import '../../shared/widgets/avatar_image.dart';
import '../../shared/widgets/buzz_loading_indicator.dart';
import '../../shared/identity_names/identity_names_provider.dart';
import '../../shared/profile/user_cache_provider.dart';
import '../../shared/relay/relay.dart';
import 'channel_management_provider.dart';
import 'ios_add_members_sheet.dart';
import 'mentions/mention_candidates.dart';
import 'mentions/mention_candidates_provider.dart';
import 'mentions/mention_ranking.dart';
import 'mentions/stable_mention_rows.dart';
import '../../shared/widgets/modal_presentation.dart';
bool _nativePickerOpen = false;
/// Opens the platform member picker, retaining the Android sheet presentation.
Future<bool?> showAddChannelMembersSheet({
required BuildContext context,
required String channelId,
required Set<String> existingPubkeys,
}) async {
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS) {
if (_nativePickerOpen) return null;
_nativePickerOpen = true;
try {
return await showGeneralDialog<bool>(
context: context,
barrierColor: Colors.transparent,
transitionDuration: Duration.zero,
pageBuilder: (_, _, _) => AddChannelMembersSheet(
channelId: channelId,
existingPubkeys: existingPubkeys,
nativePresentation: true,
),
);
} finally {
_nativePickerOpen = false;
}
}
final mediaQuery = MediaQuery.of(context);
return showBuzzModalBottomSheet<bool>(
context: context,
title: 'Add members',
isScrollControlled: true,
showDragHandle: true,
constraints: BoxConstraints(
maxWidth: 640,
maxHeight: mediaQuery.size.height - mediaQuery.viewPadding.top - Grid.xs,
),
builder: (_) => AddChannelMembersSheet(
channelId: channelId,
existingPubkeys: existingPubkeys,
),
);
}
/// Searchable multi-select used to add people or agents to a channel.
class AddChannelMembersSheet extends HookConsumerWidget {
const AddChannelMembersSheet({
super.key,
required this.channelId,
required this.existingPubkeys,
this.nativePresentation = false,
});
final String channelId;
final Set<String> existingPubkeys;
final bool nativePresentation;
@override
Widget build(BuildContext context, WidgetRef ref) {
final queryController = useTextEditingController();
final query = useState('');
// A new opening after each pick, as the mention chooser starts one per
// `@`: shown rows keep their places only within one query and opening.
final opening = useState(0);
final selectedUsers = useState<List<MentionCandidate>>([]);
final isSubmitting = useState(false);
final submitError = useState<String?>(null);
final actions = useMemoized(() => ref.read(channelActionsProvider));
// The same candidates, ranking, debounce and search cache as mention
// autocomplete and New DM. A channel can hold agents that do not answer
// the viewer, so those are offered too.
final search = query.value.trim();
final ranked = ref.watch(
recipientCandidatesProvider((
query: search,
historyKey: addMembersMentionHistoryKey,
unsharedAgents: true,
)),
);
final searching = ref.watch(recipientSearchPendingProvider(search));
final searchFailed = ref.watch(recipientSearchFailedProvider(search));
final normalizedExisting = existingPubkeys
.map((pubkey) => pubkey.toLowerCase())
.toSet();
final selectedPubkeys = selectedUsers.value
.map((user) => user.pubkey.toLowerCase())
.toSet();
final successfulPubkeys = useState<Set<String>>(<String>{});
final excludedPubkeys = {
...normalizedExisting,
...successfulPubkeys.value,
...selectedPubkeys,
};
final shown = useStableMentionRows(
[
for (final candidate in ranked)
if (!excludedPubkeys.contains(candidate.pubkey.toLowerCase()))
candidate,
],
query: search,
opening: opening.value,
pending: searching,
);
final availableUsers = shown.rows;
final userCache = ref.watch(userCacheProvider);
final currentPubkey = ref.watch(currentPubkeyProvider);
String? detail(MentionCandidate user) =>
recipientRowDetail(user, currentPubkey, userCache);
String? avatarUrl(MentionCandidate user) =>
user.avatarUrl ?? userCache[user.pubkey]?.avatarUrl;
// Compare every shown choice with the channel's members, so a candidate
// who shares a member's name is told apart before being added. Members
// are only compared: their names come from the roster the details page
// already loaded, so only the shown choices' profiles are fetched.
final choices = [...availableUsers, ...selectedUsers.value];
final roster =
ref.watch(channelMembersProvider(channelId)).asData?.value ??
const <ChannelMember>[];
final names = watchIdentityNames(
ref,
[...normalizedExisting, for (final user in choices) user.pubkey],
agentPubkeys: {
for (final member in roster)
if (member.isBot) member.pubkey,
for (final user in choices)
if (user.isAgent) user.pubkey,
},
fallbackNames: {
for (final member in roster) member.pubkey: ?member.displayName,
for (final user in choices) user.pubkey: user.pickerLabel,
},
shown: [for (final user in choices) user.pubkey],
);
String labelFor(MentionCandidate user) => names.labelFor(user.pubkey);
void retry() => ref.invalidate(mentionUserSearchProvider(search));
void toggleUser(MentionCandidate user) {
if (isSubmitting.value) return;
final normalized = user.pubkey.toLowerCase();
// The current selection, not this build's: a second tap can land
// before the rebuild that reflects the first.
if (selectedUsers.value.any(
(selected) => selected.pubkey.toLowerCase() == normalized,
)) {
selectedUsers.value = [
for (final selected in selectedUsers.value)
if (selected.pubkey.toLowerCase() != normalized) selected,
];
} else {
if (shown.unavailable.contains(user.pubkey)) return;
selectedUsers.value = [...selectedUsers.value, user];
ref
.read(mentionHistoryProvider.notifier)
.remember(addMembersMentionHistoryKey, user.pubkey);
}
opening.value++;
submitError.value = null;
}
Future<void> addSelectedMembers() async {
if (selectedUsers.value.isEmpty || isSubmitting.value) return;
final submittedUsers = List<MentionCandidate>.of(selectedUsers.value);
isSubmitting.value = true;
submitError.value = null;
try {
await actions.addMembers(
channelId: channelId,
pubkeys: submittedUsers.map((user) => user.pubkey).toList(),
);
if (context.mounted) Navigator.of(context).pop(true);
} on AddMembersException catch (error) {
if (!context.mounted) return;
final failedPubkeys = error.failures.keys
.map((pubkey) => pubkey.toLowerCase())
.toSet();
successfulPubkeys.value = {
...successfulPubkeys.value,
for (final user in submittedUsers)
if (!failedPubkeys.contains(user.pubkey.toLowerCase()))
user.pubkey.toLowerCase(),
};
selectedUsers.value = [
for (final user in submittedUsers)
if (failedPubkeys.contains(user.pubkey.toLowerCase())) user,
];
submitError.value = error.message;
} catch (error) {
if (!context.mounted) return;
submitError.value = error.toString();
} finally {
if (context.mounted) isSubmitting.value = false;
}
}
if (nativePresentation) {
final auth = ref.watch(mediaGetAuthServiceProvider);
final client = ref.watch(mediaHttpClientProvider);
final diskCache = ref.watch(mediaDiskCacheProvider);
final colors = Theme.of(context).colorScheme;
String avatarKey(MentionCandidate user) => jsonEncode([
user.pubkey,
avatarUrl(user),
user.initial,
user.isAgent,
colors.primaryContainer.toARGB32(),
colors.onPrimaryContainer.toARGB32(),
]);
Map<String, Object?> row(MentionCandidate user) => {
'pubkey': user.pubkey,
'name': labelFor(user),
'detail': detail(user),
'agent': user.isAgent,
'unavailable': shown.unavailable.contains(user.pubkey),
'avatarKey': avatarKey(user),
'initial': user.initial,
'avatarBackground': colors.primaryContainer.toARGB32(),
'avatarForeground': colors.onPrimaryContainer.toARGB32(),
'selected': selectedPubkeys.contains(user.pubkey.toLowerCase()),
};
return IosAddMembersSheet(
onAvatar: (pubkey, key) async {
final user = choices
.where((user) => user.pubkey == pubkey)
.firstOrNull;
if (user == null || avatarKey(user) != key) return null;
return nativeAvatarImage(
url: avatarUrl(user),
initial: user.initial,
isAgent: user.isAgent,
background: colors.primaryContainer,
foreground: colors.onPrimaryContainer,
networkImages: (url) => smallMediaImageProviders(
url: url,
auth: auth,
client: client,
diskCache: diskCache,
),
);
},
state: {
'query': query.value,
'emptyQueryHint': 'Type a name to find people or agents.',
'loading': searching,
'loadError': searchFailed,
'error': submitError.value,
'submitting': isSubmitting.value,
'selected': [for (final user in selectedUsers.value) row(user)],
'users': [for (final user in availableUsers) row(user)],
},
onQuery: (value) {
if (!isSubmitting.value) query.value = value;
},
onToggle: (pubkey) {
final user = choices
.where((user) => user.pubkey == pubkey)
.firstOrNull;
if (user != null) toggleUser(user);
},
onSubmit: () => unawaited(addSelectedMembers()),
onRetry: retry,
onClose: () => Navigator.of(context).pop(false),
);
}
return Padding(
padding: EdgeInsets.fromLTRB(
Grid.gutter,
0,
Grid.gutter,
MediaQuery.viewInsetsOf(context).bottom,
),
child: SafeArea(
top: false,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.72,
),
child: Column(
children: [
TextField(
key: const ValueKey('add-channel-members-search'),
controller: queryController,
autofocus: true,
autocorrect: false,
enableSuggestions: false,
enabled: !isSubmitting.value,
onChanged: (value) => query.value = value,
decoration: const InputDecoration(
hintText: 'Search for people or agents',
prefixIcon: Icon(BuzzIcons.search),
),
),
if (selectedUsers.value.isNotEmpty) ...[
const SizedBox(height: Grid.xxs),
Align(
alignment: Alignment.centerLeft,
child: Wrap(
spacing: Grid.half,
runSpacing: Grid.half,
children: [
for (final user in selectedUsers.value)
InputChip(
key: ValueKey(
'add-channel-member-selected-${user.pubkey}',
),
label: Text(labelFor(user)),
onDeleted: isSubmitting.value
? null
: () => toggleUser(user),
),
],
),
),
],
const SizedBox(height: Grid.xxs),
Expanded(
child: availableUsers.isEmpty && !searchFailed
? Center(
child: searching
? const BuzzLoadingIndicator(
size: 44,
semanticLabel: 'Searching people and agents',
)
: Text(
search.isEmpty
? 'Type a name to find people or agents.'
: 'No matching people or agents.',
textAlign: TextAlign.center,
),
)
: ListView.builder(
key: const ValueKey('add-channel-members-results'),
itemCount:
availableUsers.length + (searchFailed ? 1 : 0),
itemBuilder: (context, index) {
if (index == availableUsers.length) {
return ListTile(
key: const ValueKey(
'add-channel-members-search-error',
),
title: Text(
'Couldn’t search people or agents.',
style: context.textTheme.bodyMedium?.copyWith(
color: context.colors.error,
),
),
trailing: TextButton(
onPressed: retry,
child: const Text('Retry'),
),
);
}
final user = availableUsers[index];
final available = !shown.unavailable.contains(
user.pubkey,
);
final info = detail(user);
return AppListCardItem(
index: index,
itemCount: availableUsers.length,
dividerIndent: Grid.xs + 40 + Grid.xs,
child: Semantics(
key: ValueKey(
'add-channel-member-${user.pubkey}',
),
button: true,
enabled: available,
label: labelFor(user),
child: ListTile(
enabled: available,
leading: AvatarImage(
imageUrl: avatarUrl(user),
radius: 20,
backgroundColor: context.controlSurface,
fallback: Text(user.initial),
isAgent: user.isAgent,
),
title: Text(
labelFor(user),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
subtitle: info == null
? null
: Text(
info,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
trailing: Icon(
BuzzIcons.plus,
color: context.colors.onSurfaceVariant,
),
onTap: available
? () => toggleUser(user)
: null,
),
),
);
},
),
),
if (submitError.value case final error?) ...[
const SizedBox(height: Grid.xxs),
Text(
error,
style: context.textTheme.bodySmall?.copyWith(
color: context.colors.error,
),
),
],
const SizedBox(height: Grid.xxs),
SizedBox(
width: double.infinity,
child: FilledButton(
key: const ValueKey('add-channel-members-submit'),
onPressed: selectedUsers.value.isEmpty || isSubmitting.value
? null
: addSelectedMembers,
child: Text(
isSubmitting.value
? 'Adding…'
: selectedUsers.value.length == 1
? 'Add member'
: 'Add ${selectedUsers.value.length} members',
),
),
),
const SizedBox(height: Grid.xxs),
],
),
),
),
);
}
}