Files
buzz/mobile/lib/shared/widgets/buzz_toast.dart
klopez4212 14e5ea7cc4 Mobile: refine section filters and ordering in Messages (#9796)
## Summary

Give Messages sections a common options sheet for sorting, filtering and
ordering. Active sections let people choose a time window. Channel and
DM sections can reveal filtered rows in place with **Show more** and
collapse them with **Show fewer**. Reordering saves a device-local order
for the current account and community.

Failed activity lookups keep affected channels visible. The recovery
toast offers Retry and dismisses when activity loads. On iOS the sheet
dims the Messages header along with the list.

### Related work

#9707 explores a count-based disclosure. This change uses the section’s
unread or activity filter.

### iOS snapshots

| Section options with an active filter | Reorder sections |
| --- | --- |
| ![Section options with a 30-day activity
window](https://github.com/user-attachments/assets/4b0fd306-f7ba-4466-aeca-18627bc05c57)
| ![Reorder sheet with draggable Solo, project, Team and Direct messages
rows](https://github.com/user-attachments/assets/3a3f1429-6334-4c8e-a535-630ae860376c)
|

### Rename a section

Tap the emoji beside the name to open the picker. Set saves both fields,
including an emoji-only change.

![Rename section with a new emoji
selected](https://github.com/user-attachments/assets/840bf926-f29f-435e-87de-2470d32ac64c)

### Activity recovery and section order

Unfiled Solo chats support local **Show more** and **Show fewer**,
including offline. Failed activity lookups retry automatically up to
three times with jittered backoff while Messages is visible and
connected. **Retry** stays available after that budget is exhausted. The
notice dismisses on recovery.

Reorder supports drag handles and keyboard arrow keys. Saving preserves
the position of an empty Starred section and checks for a newer order.
Each signing identity has its own saved order per relay.

### Testing

Exercised filtering, reorder and dismissal in the iOS simulator. The
preview was also tested on iPhone Air. At `6135e314f3`, Flutter analysis
and the full pre-push mobile checks passed (5,613 tests passed, 71
skipped). Widget regressions cover offline Solo disclosure, retry
exhaustion, route visibility, keyboard reorder, large text, and editor
cancellation.

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
2026-11-20 17:20:08 +00:00

338 lines
10 KiB
Dart

import 'ios_composition_layer.dart';
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import '../theme/theme.dart';
import '../toast_haptic.dart';
import 'toast_capsule_content.dart';
export '../toast_haptic.dart' show ToastKind;
part 'buzz_toast_island.dart';
part 'reconnect_capsule.dart';
final _toastCoordinators = Expando<_ToastCoordinator>();
_ToastCoordinator _toastCoordinator(BuildContext context) {
final overlay = Overlay.of(context, rootOverlay: true);
return _toastCoordinators[overlay] ??= _ToastCoordinator();
}
class _ToastCoordinator extends ValueNotifier<bool> {
_ToastCoordinator() : super(false);
_ToastHandle? active;
}
/// Shows brief feedback above routes and modal sheets.
/// A new notice replaces the current one instead of building a stale queue.
/// Supported portrait iPhones morph from the Dynamic Island automatically.
void showBuzzToast(
BuildContext context,
String message, {
ToastKind kind = ToastKind.generic,
VoidCallback? onRetry,
Rect? islandOrigin,
double islandSpeed = 0.75,
}) {
final overlay = Overlay.of(context, rootOverlay: true);
final coordinator = _toastCoordinator(context);
coordinator.active?.remove();
late final _ToastHandle handle;
final entry = OverlayEntry(
builder: (_) => IosCompositionLayer(
child: _ToastBubble(
message: message,
islandOrigin: islandOrigin,
islandSpeed: islandSpeed,
onDismiss: handle.remove,
onRetry: onRetry == null
? null
: () {
handle.remove();
if (context.mounted) onRetry();
},
),
),
);
handle = _ToastHandle(entry, () {
if (identical(coordinator.active, handle)) {
coordinator.active = null;
coordinator.value = false;
}
});
coordinator.active = handle;
coordinator.value = true;
overlay.insert(entry);
unawaited(toastHaptic(kind));
}
class _ToastHandle {
_ToastHandle(this.entry, this.onRemove);
final OverlayEntry entry;
final VoidCallback onRemove;
bool removed = false;
void remove() {
if (removed) return;
removed = true;
entry.remove();
entry.dispose();
onRemove();
}
}
class _ToastBubble extends HookWidget {
const _ToastBubble({
required this.message,
required this.onDismiss,
this.onRetry,
this.islandOrigin,
this.islandSpeed = 0.75,
this.visible = true,
this.persistent = false,
this.actionLabel = 'Retry',
this.destructiveAction = false,
this.secondaryAction,
this.inline = false,
});
final String message;
final VoidCallback? onRetry;
final VoidCallback onDismiss;
final Rect? islandOrigin;
final double islandSpeed;
final bool visible;
final bool persistent;
final String actionLabel;
final bool destructiveAction;
final ({String label, VoidCallback onPressed})? secondaryAction;
final bool inline;
@override
Widget build(BuildContext context) {
// Tall cards need fixed corners and the standard entrance so enlarged
// text is not clipped by a capsule or Dynamic Island surface.
final stacked =
secondaryAction != null ||
(onRetry != null &&
MediaQuery.textScalerOf(context).scale(14) > 14 * 1.3);
final origin = !stacked && !inline
? islandOrigin ?? _toastIslandOrigin(context)
: null;
final reduceMotion = MediaQuery.disableAnimationsOf(context);
final accessible = MediaQuery.accessibleNavigationOf(context);
final program = useFuture(
useMemoized(
() => origin != null && !reduceMotion ? _loadIslandProgram() : null,
[origin != null, reduceMotion],
),
);
final shader = useMemoized(() => program.data?.fragmentShader(), [
program.data,
]);
useEffect(
() =>
() => shader?.dispose(),
[shader],
);
final waitingForIsland =
origin != null &&
!reduceMotion &&
program.connectionState != ConnectionState.done;
final island = reduceMotion || shader == null ? null : origin;
final speed = island == null ? 1.0 : islandSpeed.clamp(1 / 3, 1.0);
final entranceMs = (280 / speed).round();
final controller = useAnimationController(
duration: Duration(milliseconds: entranceMs),
reverseDuration: Duration(milliseconds: (240 / speed).round()),
);
useEffect(
() {
if (waitingForIsland) return null;
if (persistent) {
if (reduceMotion) {
controller.value = visible ? 1 : 0;
if (!visible) onDismiss();
} else if (visible) {
controller.forward();
} else {
// An interrupted outro must not remove a notice that became
// relevant again. Reverse also completes if already dismissed.
controller.reverse().whenCompleteOrCancel(() {
if (context.mounted && controller.isDismissed) onDismiss();
});
}
return null;
}
if (reduceMotion) {
controller.value = 1;
} else {
controller.forward();
}
final timer = accessible && onRetry != null
? null
: Timer(
Duration(
milliseconds:
(accessible || onRetry != null ? 6000 : 3000) +
(reduceMotion ? 0 : entranceMs),
),
() async {
if (!reduceMotion) {
try {
await controller.reverse().orCancel;
} on TickerCanceled {
return;
}
}
onDismiss();
},
);
return () => timer?.cancel();
},
[
controller,
reduceMotion,
accessible,
persistent,
visible,
waitingForIsland,
],
);
final value = useAnimation(controller);
if (waitingForIsland || (persistent && value == 0)) {
return const SizedBox.shrink();
}
final exiting = controller.status == AnimationStatus.reverse;
// The exit collapses immediately while upward travel accelerates later.
// Reversing the entry width curve leaves the text fully exposed too long.
final progress = exiting
? Curves.easeOutQuad.transform(value)
: const Cubic(0.2, 1.1, 0.4, 1).transform(value);
final reveal = exiting
? Curves.easeInCubic.transform(value)
: Curves.easeOutCubic.transform(value);
final contentOpacity = exiting
? value * value
: island == null
? 1.0
: const Interval(0.25, 0.8).transform(value);
final top = MediaQuery.viewPaddingOf(context).top + Grid.xxs;
// Follow the system preference even when the current route overrides Theme.
final dark = MediaQuery.platformBrightnessOf(context) == Brightness.dark;
final background = island != null
? Colors.black
: dark
? darkColorScheme.surfaceContainer
: lightColorScheme.inverseSurface;
final foreground = dark
? darkColorScheme.onSurface
: lightColorScheme.onInverseSurface;
final bubble = IgnorePointer(
ignoring: onRetry == null || exiting,
child: Align(
alignment: Alignment.topCenter,
heightFactor: inline ? 1 : null,
child: FractionalTranslation(
translation: Offset(0, -(island == null ? 1 : 0.5) * (1 - progress)),
child: Transform.translate(
offset: Offset(
0,
((island?.center.dy ?? 0) - top) * (1 - progress),
),
child: Semantics(
liveRegion: true,
onDismiss: persistent ? null : onDismiss,
// Lay text out once at its full width. Only the capsule's
// painted bounds expand, revealing rather than scaling text.
child: CustomPaint(
painter: island == null || shader == null
? null
: _ToastIslandSurface(
shader: shader,
reveal: reveal,
pixelRatio: MediaQuery.devicePixelRatioOf(context),
island: island,
top: top,
progress: progress,
),
child: PhysicalShape(
clipper: _ToastRevealClipper(
reveal,
radius: stacked ? Radii.navigationCapsule : null,
),
clipBehavior: Clip.antiAlias,
color: island != null && shader != null
? Colors.transparent
: background,
elevation: island != null && shader != null ? 0 : 4,
shadowColor: Colors.black.withValues(alpha: 0.18),
child: Opacity(
opacity: contentOpacity,
child: ToastCapsuleContent(
message: message,
foreground: foreground,
actionLabel: actionLabel,
destructiveAction: destructiveAction,
secondaryAction: secondaryAction,
onAction: onRetry,
stackActions: stacked,
),
),
),
),
),
),
),
),
);
if (inline) {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: Grid.gutter,
vertical: Grid.xxs,
),
child: bubble,
);
}
return Positioned(
top: top,
left: Grid.gutter,
right: Grid.gutter,
child: bubble,
);
}
}
class _ToastRevealClipper extends CustomClipper<Path> {
const _ToastRevealClipper(this.progress, {this.radius});
final double progress;
final double? radius;
@override
Path getClip(Size size) {
final collapsedWidth = size.height.clamp(0.0, size.width);
final width = collapsedWidth + (size.width - collapsedWidth) * progress;
return RoundedSuperellipseBorder(
borderRadius: BorderRadius.circular(radius ?? size.height / 2),
).getOuterPath(
Rect.fromCenter(
center: size.center(Offset.zero),
width: width,
height: size.height,
),
);
}
@override
bool shouldReclip(_ToastRevealClipper oldClipper) =>
oldClipper.progress != progress || oldClipper.radius != radius;
}