mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
On iOS, an animated agent avatar (a preset character with a transparent
video) blinks out for one to three frames right after navigation
settles. Opening a Solo chat blanks the header avatar; going back to
Messages blanks the bestie bubble.
The cause is the hand-off from the still poster to the video. While a
route animates, `PresetAgentAvatar` shows the poster; once it settles it
replaced the poster with a native `AVPlayerLayer`, which draws nothing
until its first video frame arrives. The avatar picker and the
create-agent page already covered this by passing a preloaded poster;
the DM and Solo header and the bestie bubble pass none, so they went
straight to the bare player.
Now every iOS caller gets that hand-off, using its own poster when it
passes none: the poster stays up, below the player, until playback
advances in that showing, then fades out over 160 ms. The poster sits
below the native layer rather than over it, because a Flutter overlay
above a platform view also costs a blank frame when the view first
appears. Each showing starts covered again and waits for playback to
advance from where the player actually is, so coming back to a chat from
a page above it doesn't blink either. The poster is keyed by its URL, so
when the avatar changes to another character (for example a synced
bestie change) it never shows the previous character's poster. Android
is unchanged; it already waits for a presented frame.
### Validation
Measured on an iPhone 17 Pro simulator, recording at 60 fps and checking
the avatar region frame by frame, opening a Solo chat with an animated
agent and going back:
| | Header blank on open | Bubble blank on back |
| --- | --- | --- |
| main, 3 runs | 1 frame every run | 2 to 3 frames every run |
| this PR, 3 runs | none | none |
Before (left) and after (right), slowed 4x: the Solo header on open,
then the bestie bubble on back.
<video
src="https://github.com/user-attachments/assets/4ddf16c0-bb8d-4d77-b53e-84f83ff45e98"
width="480"></video>
Added test: [iOS keeps the poster over a new player until playback
advances](2b172d1b68/mobile/test/shared/widgets/preset_agent_avatar_test.dart),
covering a first showing, a return to the route, and a change of
character while the video is showing.
### Regressions
- No extra work: the poster image was already loaded for the transition,
and the video plays exactly as before.
- During the 160 ms fade, poster and video overlap. They show the same
character, so the fade reads as a single image, as it already did in the
avatar picker.
- If the video never starts playing, the poster just stays up, which is
what callers showed before the video was ready anyway.
buzz-review-completed
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
219 lines
7.8 KiB
Dart
219 lines
7.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
import '../animated_avatar.dart';
|
|
import 'avatar_image.dart';
|
|
import 'avatar_shape.dart';
|
|
import 'android_transparent_avatar.dart';
|
|
|
|
/// Plays the transparent video paired with a bundled character poster.
|
|
class PresetAgentAvatar extends HookWidget {
|
|
const PresetAgentAvatar({
|
|
super.key,
|
|
required this.imageUrl,
|
|
required this.size,
|
|
required this.fallback,
|
|
this.posterPlaceholder,
|
|
});
|
|
|
|
final String imageUrl;
|
|
final double size;
|
|
final Widget fallback;
|
|
|
|
/// A preloaded poster for spatial transitions in the character picker.
|
|
final Widget? posterPlaceholder;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final poster = parseAnimatedAvatarUrl(imageUrl)?.posterUrl ?? imageUrl;
|
|
final uri = Uri.tryParse(poster);
|
|
final canPlay =
|
|
RegExp(
|
|
r'^/avatars/[A-Za-z0-9_-]+/poster/[A-Za-z0-9_-]+/[A-Za-z0-9_-]+\.png$',
|
|
).hasMatch(uri?.path ?? '') &&
|
|
uri?.host == 'dwwgwmfqqjotj.cloudfront.net' &&
|
|
!MediaQuery.disableAnimationsOf(context);
|
|
final isIos = defaultTargetPlatform == TargetPlatform.iOS;
|
|
final isAndroid = defaultTargetPlatform == TargetPlatform.android;
|
|
final route = ModalRoute.of(context);
|
|
useListenable(isIos ? route?.animation : null);
|
|
useListenable(isIos ? route?.secondaryAnimation : null);
|
|
// UIKit video layers cannot reliably follow Flutter's Hero clipping and
|
|
// route opacity. Keep them out of the composition until navigation settles.
|
|
final routeSettled =
|
|
(route?.animation == null ||
|
|
route!.animation!.status == AnimationStatus.completed) &&
|
|
(route?.secondaryAnimation == null ||
|
|
route!.secondaryAnimation!.status == AnimationStatus.dismissed);
|
|
final showNativeVideo =
|
|
!isIos ||
|
|
(routeSettled &&
|
|
route?.isCurrent != false &&
|
|
TickerMode.valuesOf(context).enabled);
|
|
final videoUrl = canPlay
|
|
? uri!.replace(
|
|
path: uri.path
|
|
.replaceFirst('/poster/', isIos ? '/hevc/' : '/webm/')
|
|
.replaceFirst(RegExp(r'\.png$'), isIos ? '.mp4' : '.webm'),
|
|
)
|
|
: null;
|
|
final controller = useMemoized(
|
|
() => videoUrl == null || isAndroid
|
|
? null
|
|
: VideoPlayerController.networkUrl(
|
|
videoUrl,
|
|
// Let AVPlayerLayer composite HEVC alpha directly on iOS.
|
|
viewType: isIos
|
|
? VideoViewType.platformView
|
|
: VideoViewType.textureView,
|
|
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
|
|
),
|
|
[videoUrl, isAndroid],
|
|
);
|
|
final ready = useState<VideoPlayerController?>(null);
|
|
final videoShown =
|
|
showNativeVideo && controller != null && ready.value == controller;
|
|
// A fresh AVPlayerLayer draws nothing until its first frame arrives, so
|
|
// keep the poster under it until playback advances in this showing. The
|
|
// baseline is read from the player, and the controller's last polled
|
|
// position is kept beside it: while paused that value lags the player,
|
|
// so it differs from the baseline until a poll in this showing.
|
|
final shownFrom = useState<(Duration, Duration)?>(null);
|
|
useEffect(() {
|
|
shownFrom.value = null;
|
|
if (!videoShown) return null;
|
|
var active = true;
|
|
unawaited(
|
|
controller.position
|
|
.then((position) {
|
|
if (active) {
|
|
shownFrom.value = (
|
|
position ?? Duration.zero,
|
|
controller.value.position,
|
|
);
|
|
}
|
|
})
|
|
.catchError((Object _) {}),
|
|
);
|
|
return () => active = false;
|
|
}, [videoShown, controller]);
|
|
final presented = useState(false);
|
|
useListenable(isIos && videoShown && !presented.value ? controller : null);
|
|
useEffect(() {
|
|
if (!videoShown) {
|
|
presented.value = false;
|
|
} else if (shownFrom.value case (final fresh, final polled)
|
|
when controller.value.position != fresh &&
|
|
controller.value.position != polled) {
|
|
presented.value = true;
|
|
}
|
|
return null;
|
|
}, [videoShown, controller, shownFrom.value, controller?.value.position]);
|
|
final lifecycle = useAppLifecycleState();
|
|
final playing =
|
|
showNativeVideo &&
|
|
TickerMode.valuesOf(context).enabled &&
|
|
(lifecycle == null || lifecycle == AppLifecycleState.resumed);
|
|
useEffect(() {
|
|
if (controller == null) return null;
|
|
var active = true;
|
|
Future<void> initialize() async {
|
|
try {
|
|
await controller.initialize();
|
|
if (!active) return;
|
|
await controller.setVolume(0);
|
|
await controller.setLooping(true);
|
|
if (!active) return;
|
|
ready.value = controller;
|
|
} catch (_) {
|
|
// Keep the poster if the video is unavailable or unsupported.
|
|
}
|
|
}
|
|
|
|
unawaited(initialize());
|
|
return () {
|
|
active = false;
|
|
unawaited(controller.dispose().catchError((Object _) {}));
|
|
};
|
|
}, [controller]);
|
|
useEffect(() {
|
|
if (controller != null && ready.value == controller) {
|
|
unawaited(
|
|
(playing ? controller.play() : controller.pause()).catchError((
|
|
Object _,
|
|
) {
|
|
if (context.mounted && ready.value == controller) {
|
|
ready.value = null;
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
return null;
|
|
}, [controller, ready.value, playing]);
|
|
|
|
// Keyed by URL so a new character never shows the previous one's poster
|
|
// while its own loads.
|
|
final posterImage = KeyedSubtree(
|
|
key: ValueKey(poster),
|
|
child:
|
|
posterPlaceholder ??
|
|
AvatarImageContent(
|
|
imageUrl: poster,
|
|
preferThumbnail: true,
|
|
fallback: fallback,
|
|
),
|
|
);
|
|
return SizedBox.square(
|
|
dimension: size,
|
|
child: AvatarClip(
|
|
isAgent: true,
|
|
child: Transform.scale(
|
|
scale: animatedAvatarArtworkScale,
|
|
child: isAndroid && videoUrl != null
|
|
? AndroidTransparentAvatar(
|
|
key: ValueKey(videoUrl),
|
|
url: videoUrl,
|
|
playing: playing,
|
|
fallback: posterImage,
|
|
)
|
|
: !isIos
|
|
? videoShown
|
|
? VideoPlayer(controller)
|
|
: posterImage
|
|
: Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
// The poster stays below the native layer, in Flutter's
|
|
// base layer: an overlay above it costs a blank frame.
|
|
// It stays mounted under the video so the next showing
|
|
// covers at once instead of reloading it.
|
|
IgnorePointer(
|
|
key: const ValueKey('preset-poster'),
|
|
child: AnimatedOpacity(
|
|
opacity: videoShown && presented.value ? 0 : 1,
|
|
// Cover a hidden player at once; fade only onto
|
|
// video.
|
|
duration: videoShown && presented.value
|
|
? const Duration(milliseconds: 160)
|
|
: Duration.zero,
|
|
child: posterImage,
|
|
),
|
|
),
|
|
if (videoShown)
|
|
Opacity(
|
|
key: const ValueKey('preset-video'),
|
|
opacity: presented.value ? 1 : 0,
|
|
child: VideoPlayer(controller),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|