π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Replace favorite Rive animation
ashilknfa7bb65250a8
diff --git a/mobile/apps/photos/assets/favorite_icon.riv b/mobile/apps/photos/assets/favorite_icon.rivdeleted file mode 100644index 6bb1147a69..0000000000Binary files a/mobile/apps/photos/assets/favorite_icon.riv and /dev/null differdiff --git a/mobile/apps/photos/changes/flutter-favorite-animation.md b/mobile/apps/photos/changes/flutter-favorite-animation.mdnew file mode 100644index 0000000000..b1d331e462--- /dev/null+++ b/mobile/apps/photos/changes/flutter-favorite-animation.md@@ -0,0 +1 @@+- Replaced the favorite animation with Ente's star icon.diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/favorite_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/favorite_widget.dartindex f49cf18d58..3ad169c9b0 100644--- a/mobile/apps/photos/lib/ui/viewer/file_details/favorite_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/file_details/favorite_widget.dart@@ -1,13 +1,15 @@import "dart:async";+import "dart:math" as math;+import "package:ente_icons/ente_icons.dart";import "package:ente_strings/ente_strings.dart";import "package:flutter/material.dart";import "package:flutter/services.dart";import "package:logging/logging.dart";import "package:photos/models/file/file.dart";import "package:photos/services/favorites_service.dart";+import "package:photos/theme/ente_theme.dart";import "package:photos/ui/notification/toast.dart";-import "package:rive/rive.dart" as rive;class FavoriteWidget extends StatefulWidget {final EnteFile file;@@ -25,188 +27,317 @@ class FavoriteWidget extends StatefulWidget {State<StatefulWidget> createState() => _FavoriteWidgetState();}-class _FavoriteWidgetState extends State<FavoriteWidget> {- late Logger _logger;- bool _isLoading = false;+class _FavoriteWidgetState extends State<FavoriteWidget>+ with SingleTickerProviderStateMixin {+ static const double _defaultIconSize = 20;++ late final Logger _logger;+ late final AnimationController _favoriteAnimationController;+ late final Animation<double> _favoriteScaleAnimation;++ bool _isUpdating = false;bool? _isFavorite;- late final rive.FileLoader _riveFileLoader;- rive.StateMachine? _stateMachine;- bool _hasSetInitialState = false;+ int _fileVersion = 0;@overridevoid initState() {super.initState();_logger = Logger("_FavoriteWidgetState");- _riveFileLoader = rive.FileLoader.fromAsset(- "assets/favorite_icon.riv",- riveFactory: rive.Factory.rive,+ _favoriteAnimationController = AnimationController(+ duration: const Duration(milliseconds: 960),+ vsync: this,);+ _favoriteScaleAnimation = TweenSequence<double>([+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 1,+ end: 0.72,+ ).chain(CurveTween(curve: Curves.easeInCubic)),+ weight: 12,+ ),+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 0.72,+ end: 1.32,+ ).chain(CurveTween(curve: Curves.easeOutCubic)),+ weight: 25,+ ),+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 1.32,+ end: 0.86,+ ).chain(CurveTween(curve: Curves.easeInOutCubic)),+ weight: 20,+ ),+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 0.86,+ end: 1.16,+ ).chain(CurveTween(curve: Curves.easeOutCubic)),+ weight: 18,+ ),+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 1.16,+ end: 0.96,+ ).chain(CurveTween(curve: Curves.easeInOutCubic)),+ weight: 13,+ ),+ TweenSequenceItem(+ tween: Tween<double>(+ begin: 0.96,+ end: 1,+ ).chain(CurveTween(curve: Curves.easeOutCubic)),+ weight: 12,+ ),+ ]).animate(_favoriteAnimationController);_initializeFavoriteState();}Future<void> _initializeFavoriteState() async {+ final int fileVersion = _fileVersion;final isFavorite = await FavoritesService.instance.isFavorite(widget.file);- if (mounted) {- setState(() {- _isFavorite = isFavorite;- });- _updateAnimationState();- }+ if (!mounted || fileVersion != _fileVersion) return;+ setState(() {+ _isFavorite = isFavorite;+ });}@overridevoid didUpdateWidget(covariant FavoriteWidget oldWidget) {super.didUpdateWidget(oldWidget);if (oldWidget.file.uploadedFileID != widget.file.uploadedFileID) {- _hasSetInitialState = false;+ _fileVersion++;+ _isUpdating = false;+ _isFavorite = null;+ _favoriteAnimationController.reset();_initializeFavoriteState();}}@overridevoid dispose() {- _riveFileLoader.dispose();+ _favoriteAnimationController.dispose();super.dispose();}- void _handleRiveLoaded(rive.RiveLoaded loaded) {- if (!mounted) return;-- _stateMachine = loaded.controller.stateMachine;-- if (_hasSetInitialState) {- // Re-sync animation state after rebuild (e.g., when loading state clears)- _updateAnimationState();- } else {- WidgetsBinding.instance.addPostFrameCallback((_) {- if (!mounted || _stateMachine == null || _isFavorite == null) return;- _setInitialAnimationState();- });- }- }-- void _setInitialAnimationState() {- if (_isFavorite == null || _hasSetInitialState) return;- _hasSetInitialState = true;- _updateAnimationState();- }-- void _updateAnimationState() {- if (_isFavorite == null || _stateMachine == null) return;- if (_isFavorite!) {- _stateMachine!.trigger("Filled")?.fire();- } else {- _stateMachine!.trigger("Stroke")?.fire();- }- }-Future<void> _onTap() async {- if (_isLoading || _isFavorite == null || _stateMachine == null) return;-- final bool currentlyFavorite = _isFavorite!;- final bool newFavoriteState = !currentlyFavorite;+ if (_isUpdating || _isFavorite == null) return;- if (widget.file.uploadedFileID == null) {- setState(() {- _isLoading = true;- });- }+ final int fileVersion = _fileVersion;+ final file = widget.file.copyWith();+ final bool previousFavoriteState = _isFavorite!;+ final bool newFavoriteState = !previousFavoriteState;- bool hasError = false;+ setState(() {+ _isUpdating = true;+ _isFavorite = newFavoriteState;+ });if (newFavoriteState) {- // Adding to favorites - play animation- _stateMachine?.trigger("Animation to filled")?.fire();-- try {- await FavoritesService.instance.addToFavorites(- context,- widget.file.copyWith(),- );- _stateMachine?.trigger("Filled")?.fire();- } catch (e, s) {- _logger.severe("Failed to add file to favorites", e, s);- hasError = true;- if (!mounted) return;- showToast(context, context.strings.sorryCouldNotAddToFavorites);- _stateMachine?.trigger("Stroke")?.fire();- }+ unawaited(_favoriteAnimationController.forward(from: 0));} else {- // Removing from favorites - go directly to stroke- _stateMachine?.trigger("Stroke")?.fire();+ _favoriteAnimationController.reset();+ }- try {- await FavoritesService.instance.removeFromFavorites(- context,- widget.file.copyWith(),- );- } catch (e, s) {- _logger.severe("Failed to remove file from favorites", e, s);- hasError = true;- if (!mounted) return;- showToast(context, context.strings.sorryCouldNotRemoveFromFavorites);- _stateMachine?.trigger("Filled")?.fire();+ try {+ if (newFavoriteState) {+ await FavoritesService.instance.addToFavorites(context, file);+ } else {+ await FavoritesService.instance.removeFromFavorites(context, file);}+ } catch (e, s) {+ _logger.severe(+ newFavoriteState+ ? "Failed to add file to favorites"+ : "Failed to remove file from favorites",+ e,+ s,+ );+ if (!mounted || fileVersion != _fileVersion) return;+ _favoriteAnimationController.reset();+ setState(() {+ _isUpdating = false;+ _isFavorite = previousFavoriteState;+ });+ showToast(+ context,+ newFavoriteState+ ? context.strings.sorryCouldNotAddToFavorites+ : context.strings.sorryCouldNotRemoveFromFavorites,+ );+ return;}- if (!mounted) return;+ if (!mounted || fileVersion != _fileVersion) return;setState(() {- _isLoading = false;- if (!hasError) {- _isFavorite = newFavoriteState;- }+ _isUpdating = false;});- if (newFavoriteState && !hasError) {+ if (newFavoriteState) {unawaited(HapticFeedback.mediumImpact());}}@overrideWidget build(BuildContext context) {- // Show blank while initial state is being fetched- if (_isFavorite == null || _isLoading) {- final placeholderSize = widget.tapTargetSize ?? 22;+ final double placeholderSize = widget.tapTargetSize ?? 22;+ if (_isFavorite == null) {return SizedBox.square(dimension: placeholderSize);}- final riveWidget = rive.RiveWidgetBuilder(- fileLoader: _riveFileLoader,- stateMachineSelector: const rive.StateMachineNamed("State Machine 1"),- onLoaded: _handleRiveLoaded,- builder: (BuildContext context, rive.RiveState state) {- if (state is rive.RiveLoaded) {- return rive.RiveWidget(- controller: state.controller,- fit: rive.Fit.contain,+ final double iconSize =+ widget.iconSize ?? widget.tapTargetSize ?? _defaultIconSize;+ final colorScheme = getEnteColorScheme(context);+ final animatedIcon = SizedBox.square(+ dimension: iconSize,+ child: AnimatedBuilder(+ animation: _favoriteAnimationController,+ builder: (context, child) {+ final bool hasFavoriteAnimationStarted =+ _isFavorite! &&+ (_favoriteAnimationController.isAnimating ||+ _favoriteAnimationController.isCompleted);+ final double colorFadeProgress =+ ((_favoriteAnimationController.value - 0.72) / 0.28).clamp(+ 0.0,+ 1.0,+ );+ final Color iconColor = hasFavoriteAnimationStarted+ ? Color.lerp(+ colorScheme.primary400,+ Colors.white,+ Curves.easeInOutCubic.transform(colorFadeProgress),+ )!+ : Colors.white;+ return Stack(+ alignment: Alignment.center,+ children: [+ Positioned.fill(+ child: CustomPaint(+ painter: _FavoriteBurstPainter(+ progress: _favoriteAnimationController.value,+ primaryColor: colorScheme.primary400,+ secondaryColor: colorScheme.primary700,+ ),+ ),+ ),+ Transform.scale(+ scale: _favoriteScaleAnimation.value,+ child: IconTheme(+ data: IconThemeData(color: iconColor),+ child: child!,+ ),+ ),+ ],);- }- if (state is rive.RiveFailed) {- _logger.warning("Failed to load Rive file: ${state.error}");- }- return const SizedBox.shrink();- },+ },+ child: AnimatedSwitcher(+ duration: const Duration(milliseconds: 120),+ switchInCurve: Curves.easeOut,+ switchOutCurve: Curves.easeIn,+ child: Icon(+ _isFavorite! ? EnteIcons.favoriteFilled : EnteIcons.favoriteStroke,+ key: ValueKey(_isFavorite),+ size: iconSize,+ ),+ ),+ ),);- return GestureDetector(- onTap: _onTap,- behavior: HitTestBehavior.opaque,- child: widget.iconSize != null || widget.tapTargetSize != null- ? SizedBox.square(- dimension: widget.tapTargetSize ?? widget.iconSize!,- child: Center(- child: SizedBox.square(- dimension: widget.iconSize ?? widget.tapTargetSize!,- child: riveWidget,- ),+ return Semantics(+ button: true,+ toggled: _isFavorite!,+ label: _isFavorite!+ ? context.strings.removeFromFavorite+ : context.strings.favorite,+ child: GestureDetector(+ onTap: _onTap,+ behavior: HitTestBehavior.opaque,+ child: widget.iconSize != null || widget.tapTargetSize != null+ ? SizedBox.square(+ dimension: widget.tapTargetSize ?? widget.iconSize!,+ child: Center(child: animatedIcon),+ )+ : Container(+ padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 4),+ width: 34,+ height: 30,+ child: Center(child: animatedIcon),),- )- : Container(- padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 4),- width: 34,- height: 30,- child: riveWidget,- ),+ ),);}}++class _FavoriteBurstPainter extends CustomPainter {+ static const double _diagonalDirection = 0.7071067811865476;+ static const List<Offset> _particleDirections = [+ Offset(0, -1),+ Offset(_diagonalDirection, -_diagonalDirection),+ Offset(1, 0),+ Offset(_diagonalDirection, _diagonalDirection),+ Offset(0, 1),+ Offset(-_diagonalDirection, _diagonalDirection),+ Offset(-1, 0),+ Offset(-_diagonalDirection, -_diagonalDirection),+ ];++ final double progress;+ final Color primaryColor;+ final Color secondaryColor;++ const _FavoriteBurstPainter({+ required this.progress,+ required this.primaryColor,+ required this.secondaryColor,+ });++ @override+ void paint(Canvas canvas, Size size) {+ final double burstProgress = ((progress - 0.12) / 0.7).clamp(0.0, 1.0);+ if (burstProgress <= 0 || burstProgress >= 1) return;++ final center = size.center(Offset.zero);+ final double shortestSide = size.shortestSide;+ final double easedProgress = Curves.easeOutCubic.transform(burstProgress);+ final double opacity = math.sin(burstProgress * math.pi);+ final double startRadius = shortestSide * 0.3;+ final double endRadius = shortestSide * 0.48;+ final double radius =+ startRadius + ((endRadius - startRadius) * easedProgress);+ final double particleRadius = shortestSide * 0.035;+ final double halfLineLength = shortestSide * 0.045;+ final paint = Paint()+ ..strokeCap = StrokeCap.round+ ..strokeWidth = shortestSide * 0.06;++ for (int index = 0; index < _particleDirections.length; index++) {+ final direction = _particleDirections[index];+ final particleCenter = center + (direction * radius);+ final Color particleColor = switch (index % 3) {+ 0 => primaryColor,+ 1 => Colors.white,+ _ => secondaryColor,+ };+ paint.color = particleColor.withValues(alpha: opacity);++ if (index.isEven) {+ canvas.drawCircle(particleCenter, particleRadius, paint);+ } else {+ canvas.drawLine(+ particleCenter - (direction * halfLineLength),+ particleCenter + (direction * halfLineLength),+ paint,+ );+ }+ }+ }++ @override+ bool shouldRepaint(covariant _FavoriteBurstPainter oldDelegate) {+ return progress != oldDelegate.progress ||+ primaryColor != oldDelegate.primaryColor ||+ secondaryColor != oldDelegate.secondaryColor;+ }+}