-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add friend activity in home screen
- Loading branch information
Showing
9 changed files
with
327 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:skeletonizer/skeletonizer.dart'; | ||
import 'package:spotube/collections/fake.dart'; | ||
import 'package:spotube/components/home/sections/friends/friend_item.dart'; | ||
import 'package:spotube/hooks/utils/use_breakpoint_value.dart'; | ||
import 'package:spotube/models/spotify_friends.dart'; | ||
import 'package:spotube/services/queries/queries.dart'; | ||
|
||
class HomePageFriendsSection extends HookConsumerWidget { | ||
const HomePageFriendsSection({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context, ref) { | ||
final friendsQuery = useQueries.user.friendActivity(ref); | ||
final friends = friendsQuery.data?.friends ?? FakeData.friends.friends; | ||
|
||
final groupCount = useBreakpointValue( | ||
sm: 3, | ||
xs: 2, | ||
md: 4, | ||
lg: 5, | ||
xl: 6, | ||
xxl: 7, | ||
); | ||
|
||
final friendGroup = friends.fold<List<List<SpotifyFriendActivity>>>( | ||
[], | ||
(previousValue, element) { | ||
if (previousValue.isEmpty) { | ||
return [ | ||
[element] | ||
]; | ||
} | ||
|
||
final lastGroup = previousValue.last; | ||
if (lastGroup.length < groupCount) { | ||
return [ | ||
...previousValue.sublist(0, previousValue.length - 1), | ||
[...lastGroup, element] | ||
]; | ||
} | ||
|
||
return [ | ||
...previousValue, | ||
[element] | ||
]; | ||
}, | ||
); | ||
|
||
if (friendsQuery.hasData && | ||
friendsQuery.data?.friends.isEmpty == true && | ||
!friendsQuery.isLoading) { | ||
return const SliverToBoxAdapter( | ||
child: SizedBox.shrink(), | ||
); | ||
} | ||
|
||
return Skeletonizer.sliver( | ||
enabled: friendsQuery.isLoading, | ||
child: SliverMainAxisGroup( | ||
slivers: [ | ||
SliverToBoxAdapter( | ||
child: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Text( | ||
'Friends', | ||
style: Theme.of(context).textTheme.titleMedium, | ||
), | ||
), | ||
), | ||
SliverToBoxAdapter( | ||
child: SingleChildScrollView( | ||
scrollDirection: Axis.horizontal, | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
for (final group in friendGroup) | ||
Row( | ||
children: [ | ||
for (final friend in group) | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: FriendItem(friend: friend), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import 'package:fl_query_hooks/fl_query_hooks.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:gap/gap.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:spotify/spotify.dart'; | ||
import 'package:spotube/collections/spotube_icons.dart'; | ||
import 'package:spotube/components/shared/image/universal_image.dart'; | ||
import 'package:spotube/models/spotify_friends.dart'; | ||
import 'package:spotube/provider/spotify_provider.dart'; | ||
|
||
class FriendItem extends HookConsumerWidget { | ||
final SpotifyFriendActivity friend; | ||
const FriendItem({ | ||
Key? key, | ||
required this.friend, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context, ref) { | ||
final ThemeData( | ||
textTheme: textTheme, | ||
colorScheme: colorScheme, | ||
) = Theme.of(context); | ||
|
||
final queryClient = useQueryClient(); | ||
final spotify = ref.watch(spotifyProvider); | ||
|
||
return Container( | ||
padding: const EdgeInsets.all(8), | ||
decoration: BoxDecoration( | ||
color: colorScheme.surfaceVariant.withOpacity(0.3), | ||
borderRadius: BorderRadius.circular(15), | ||
), | ||
constraints: const BoxConstraints( | ||
minWidth: 300, | ||
), | ||
height: 80, | ||
child: Row( | ||
children: [ | ||
CircleAvatar( | ||
backgroundImage: UniversalImage.imageProvider( | ||
friend.user.imageUrl, | ||
), | ||
), | ||
const Gap(8), | ||
Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Text( | ||
friend.user.name, | ||
style: textTheme.bodyLarge, | ||
), | ||
RichText( | ||
text: TextSpan( | ||
style: textTheme.bodySmall, | ||
children: [ | ||
TextSpan( | ||
text: friend.track.name, | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () { | ||
context.push("/track/${friend.track.id}"); | ||
}, | ||
), | ||
const TextSpan(text: " • "), | ||
const WidgetSpan( | ||
child: Icon( | ||
SpotubeIcons.artist, | ||
size: 12, | ||
), | ||
), | ||
TextSpan( | ||
text: " ${friend.track.artist.name}", | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () { | ||
context.push( | ||
"/artist/${friend.track.artist.id}", | ||
); | ||
}, | ||
), | ||
const TextSpan(text: "\n"), | ||
TextSpan( | ||
text: friend.track.context.name, | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () async { | ||
context.push( | ||
"/${friend.track.context.path}", | ||
extra: !friend.track.context.path | ||
.startsWith("album") | ||
? null | ||
: await queryClient.fetchQuery<Album, dynamic>( | ||
"album/${friend.track.album.id}", | ||
() => spotify.albums.get( | ||
friend.track.album.id, | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
const TextSpan(text: " • "), | ||
const WidgetSpan( | ||
child: Icon( | ||
SpotubeIcons.album, | ||
size: 12, | ||
), | ||
), | ||
TextSpan( | ||
text: " ${friend.track.album.name}", | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () async { | ||
final album = | ||
await queryClient.fetchQuery<Album, dynamic>( | ||
"album/${friend.track.album.id}", | ||
() => spotify.albums.get( | ||
friend.track.album.id, | ||
), | ||
); | ||
if (context.mounted) { | ||
context.push( | ||
"/album/${friend.track.album.id}", | ||
extra: album, | ||
); | ||
} | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.