Skip to content

Commit

Permalink
feat: heart button animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingkor Roy Tirtho committed Apr 28, 2023
1 parent af278d8 commit 8432dc6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
57 changes: 39 additions & 18 deletions lib/components/shared/heart_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import 'package:spotify/spotify.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/hooks/use_palette_color.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/services/mutations/mutations.dart';
Expand All @@ -13,7 +12,7 @@ import 'package:spotube/services/queries/queries.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:tuple/tuple.dart';

class HeartButton extends ConsumerWidget {
class HeartButton extends HookConsumerWidget {
final bool isLiked;
final void Function()? onPressed;
final IconData? icon;
Expand All @@ -36,9 +35,24 @@ class HeartButton extends ConsumerWidget {

return IconButton(
tooltip: tooltip,
icon: Icon(
icon ?? (!isLiked ? SpotubeIcons.heart : SpotubeIcons.heartFilled),
color: isLiked ? Colors.pink : color,
icon: AnimatedSwitcher(
switchInCurve: Curves.fastOutSlowIn,
switchOutCurve: Curves.fastOutSlowIn,
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
return ScaleTransition(
scale: animation,
child: child,
);
},
child: Icon(
icon ??
(isLiked
? Icons.favorite_rounded
: Icons.favorite_outline_rounded),
key: ValueKey(isLiked),
color: color ?? (isLiked ? color ?? Colors.red : null),
),
),
onPressed: onPressed,
);
Expand All @@ -53,29 +67,36 @@ Tuple3<bool, Mutation<bool, dynamic, bool>, Query<User, dynamic>>
useQueries.playlist.tracksOfQuery(ref, "user-liked-tracks");

final isLiked =
savedTracks.data?.map((track) => track.id).contains(track.id) ?? false;
savedTracks.data?.any((element) => element.id == track.id) ?? false;

final mounted = useIsMounted();

final toggleTrackLike = useMutations.track.toggleFavorite(
ref,
track.id!,
onMutate: (variables) {
return variables;
onMutate: (isLiked) {
savedTracks.setData(
[
if (isLiked == true)
...?savedTracks.data?.where((element) => element.id != track.id)
else
...?savedTracks.data?..add(track)
],
);
return isLiked;
},
onData: (data, recoveryData) async {
await savedTracks.refresh();
},
onError: (payload, isLiked) {
if (!mounted()) return;

savedTracks.setData(
isLiked == true
? [...(savedTracks.data ?? []), track]
: savedTracks.data
?.where(
(element) => element.id != track.id,
)
.toList() ??
[],
);
savedTracks.setData([
if (isLiked != true)
...?savedTracks.data?.where((element) => element.id != track.id)
else
...?savedTracks.data?..add(track),
]);
},
);

Expand Down
4 changes: 3 additions & 1 deletion lib/components/shared/track_table/track_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ class TrackTile extends HookConsumerWidget {
color: Colors.pink,
)
: const Icon(SpotubeIcons.heart),
title: const Text("Save as favorite"),
title: Text(
"${toggler.item1 ? "Remove from" : "Save as "} favorite",
),
),
),
if (auth != null)
Expand Down

0 comments on commit 8432dc6

Please sign in to comment.