-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stats): add lazy loading support
- Loading branch information
Showing
14 changed files
with
610 additions
and
319 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
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 |
---|---|---|
@@ -1,29 +1,47 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:skeletonizer/skeletonizer.dart'; | ||
import 'package:spotube/collections/formatters.dart'; | ||
import 'package:spotube/modules/stats/common/artist_item.dart'; | ||
import 'package:spotube/provider/history/top.dart'; | ||
import 'package:spotube/provider/history/top/tracks.dart'; | ||
import 'package:spotube/provider/spotify/spotify.dart'; | ||
import 'package:very_good_infinite_list/very_good_infinite_list.dart'; | ||
|
||
class TopArtists extends HookConsumerWidget { | ||
const TopArtists({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, ref) { | ||
final historyDuration = ref.watch(playbackHistoryTopDurationProvider); | ||
final artists = ref.watch(playbackHistoryTopProvider(historyDuration) | ||
.select((value) => value.whenData((s) => s.artists))); | ||
final topTracks = ref.watch( | ||
historyTopTracksProvider(historyDuration), | ||
); | ||
final topTracksNotifier = | ||
ref.watch(historyTopTracksProvider(historyDuration).notifier); | ||
|
||
final artistsData = artists.asData?.value ?? []; | ||
final artistsData = useMemoized( | ||
() => topTracks.asData?.value.artists ?? [], [topTracks.asData?.value]); | ||
|
||
return SliverList.builder( | ||
itemCount: artistsData.length, | ||
itemBuilder: (context, index) { | ||
final artist = artistsData[index]; | ||
return StatsArtistItem( | ||
artist: artist.artist, | ||
info: Text("${compactNumberFormatter.format(artist.count)} plays"), | ||
); | ||
}, | ||
return Skeletonizer.sliver( | ||
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage, | ||
child: SliverInfiniteList( | ||
onFetchData: () async { | ||
await topTracksNotifier.fetchMore(); | ||
}, | ||
hasError: topTracks.hasError, | ||
isLoading: topTracks.isLoading && !topTracks.isLoadingNextPage, | ||
hasReachedMax: topTracks.asData?.value.hasMore ?? true, | ||
itemCount: artistsData.length, | ||
itemBuilder: (context, index) { | ||
final artist = artistsData[index]; | ||
return StatsArtistItem( | ||
artist: artist.artist, | ||
info: Text("${compactNumberFormatter.format(artist.count)} plays"), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,33 +1,47 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:skeletonizer/skeletonizer.dart'; | ||
import 'package:spotube/collections/formatters.dart'; | ||
import 'package:spotube/modules/stats/common/track_item.dart'; | ||
import 'package:spotube/provider/history/top.dart'; | ||
import 'package:spotube/provider/history/top/tracks.dart'; | ||
import 'package:spotube/provider/spotify/spotify.dart'; | ||
import 'package:very_good_infinite_list/very_good_infinite_list.dart'; | ||
|
||
class TopTracks extends HookConsumerWidget { | ||
const TopTracks({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, ref) { | ||
final historyDuration = ref.watch(playbackHistoryTopDurationProvider); | ||
final tracks = ref.watch( | ||
playbackHistoryTopProvider(historyDuration) | ||
.select((value) => value.whenData((s) => s.tracks)), | ||
final topTracks = ref.watch( | ||
historyTopTracksProvider(historyDuration), | ||
); | ||
final topTracksNotifier = | ||
ref.watch(historyTopTracksProvider(historyDuration).notifier); | ||
|
||
final tracksData = tracks.asData?.value ?? []; | ||
final tracksData = topTracks.asData?.value.items ?? []; | ||
|
||
return SliverList.builder( | ||
itemCount: tracksData.length, | ||
itemBuilder: (context, index) { | ||
final track = tracksData[index]; | ||
return StatsTrackItem( | ||
track: track.track, | ||
info: Text( | ||
"${compactNumberFormatter.format(track.count)} plays", | ||
), | ||
); | ||
}, | ||
return Skeletonizer.sliver( | ||
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage, | ||
child: SliverInfiniteList( | ||
onFetchData: () async { | ||
await topTracksNotifier.fetchMore(); | ||
}, | ||
hasError: topTracks.hasError, | ||
isLoading: topTracks.isLoading && !topTracks.isLoadingNextPage, | ||
hasReachedMax: topTracks.asData?.value.hasMore ?? true, | ||
itemCount: tracksData.length, | ||
itemBuilder: (context, index) { | ||
final track = tracksData[index]; | ||
return StatsTrackItem( | ||
track: track.track, | ||
info: Text( | ||
"${compactNumberFormatter.format(track.count)} plays", | ||
), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} |
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.