Skip to content

Commit 7927a3e

Browse files
committed
chore: fix top album and track invalid time frame operations
1 parent 3bdc46d commit 7927a3e

File tree

5 files changed

+103
-28
lines changed

5 files changed

+103
-28
lines changed

lib/models/database/database.dart

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:convert';
44
import 'dart:io';
55

66
import 'package:drift/drift.dart';
7-
import 'package:drift/extensions/json1.dart';
87
import 'package:encrypt/encrypt.dart';
98
import 'package:media_kit/media_kit.dart' hide Track;
109
import 'package:path/path.dart';

lib/modules/stats/common/album_item.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class StatsAlbumItem extends StatelessWidget {
3333
Text("${album.albumType?.formatted} • "),
3434
Flexible(
3535
child: ArtistLink(
36-
artists: album.artists!,
36+
artists: album.artists ?? [],
3737
mainAxisAlignment: WrapAlignment.start,
3838
),
3939
),

lib/pages/stats/fees/fees.dart

+77-19
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ class StatsStreamFeesPage extends HookConsumerWidget {
2020
@override
2121
Widget build(BuildContext context, ref) {
2222
final ThemeData(:textTheme, :hintColor) = Theme.of(context);
23+
final duration = useState<HistoryDuration>(HistoryDuration.days30);
2324

2425
final topTracks = ref.watch(
25-
historyTopTracksProvider(HistoryDuration.allTime),
26+
historyTopTracksProvider(duration.value),
2627
);
2728
final topTracksNotifier =
28-
ref.watch(historyTopTracksProvider(HistoryDuration.allTime).notifier);
29+
ref.watch(historyTopTracksProvider(duration.value).notifier);
2930

3031
final artistsData = useMemoized(
3132
() => topTracks.asData?.value.artists ?? [], [topTracks.asData?.value]);
3233

34+
final total = useMemoized(
35+
() => artistsData.fold<double>(
36+
0,
37+
(previousValue, element) => previousValue + element.count * 0.005,
38+
),
39+
[artistsData],
40+
);
41+
3342
return Scaffold(
3443
appBar: const PageWindowTitleBar(
3544
automaticallyImplyLeading: true,
@@ -57,23 +66,72 @@ class StatsStreamFeesPage extends HookConsumerWidget {
5766
),
5867
),
5968
),
60-
Skeletonizer.sliver(
61-
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage,
62-
child: SliverInfiniteList(
63-
onFetchData: () async {
64-
await topTracksNotifier.fetchMore();
65-
},
66-
hasError: topTracks.hasError,
67-
isLoading: topTracks.isLoading && !topTracks.isLoadingNextPage,
68-
hasReachedMax: topTracks.asData?.value.hasMore ?? true,
69-
itemCount: artistsData.length,
70-
itemBuilder: (context, index) {
71-
final artist = artistsData[index];
72-
return StatsArtistItem(
73-
artist: artist.artist,
74-
info: Text(usdFormatter.format(artist.count * 0.005)),
75-
);
76-
},
69+
SliverToBoxAdapter(
70+
child: Padding(
71+
padding: const EdgeInsets.symmetric(horizontal: 16),
72+
child: Row(
73+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
74+
children: [
75+
Text(
76+
"Total ${usdFormatter.format(total)}",
77+
style: textTheme.titleLarge,
78+
),
79+
DropdownButton<HistoryDuration>(
80+
value: duration.value,
81+
onChanged: (value) {
82+
if (value == null) return;
83+
duration.value = value;
84+
},
85+
items: const [
86+
DropdownMenuItem(
87+
value: HistoryDuration.days7,
88+
child: Text("This week"),
89+
),
90+
DropdownMenuItem(
91+
value: HistoryDuration.days30,
92+
child: Text("This month"),
93+
),
94+
DropdownMenuItem(
95+
value: HistoryDuration.months6,
96+
child: Text("Last 6 months"),
97+
),
98+
DropdownMenuItem(
99+
value: HistoryDuration.year,
100+
child: Text("This year"),
101+
),
102+
DropdownMenuItem(
103+
value: HistoryDuration.years2,
104+
child: Text("Last 2 years"),
105+
),
106+
DropdownMenuItem(
107+
value: HistoryDuration.allTime,
108+
child: Text("All time"),
109+
),
110+
],
111+
),
112+
],
113+
),
114+
),
115+
),
116+
SliverSafeArea(
117+
sliver: Skeletonizer.sliver(
118+
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage,
119+
child: SliverInfiniteList(
120+
onFetchData: () async {
121+
await topTracksNotifier.fetchMore();
122+
},
123+
hasError: topTracks.hasError,
124+
isLoading: topTracks.isLoading && !topTracks.isLoadingNextPage,
125+
hasReachedMax: topTracks.asData?.value.hasMore ?? true,
126+
itemCount: artistsData.length,
127+
itemBuilder: (context, index) {
128+
final artist = artistsData[index];
129+
return StatsArtistItem(
130+
artist: artist.artist,
131+
info: Text(usdFormatter.format(artist.count * 0.005)),
132+
);
133+
},
134+
),
77135
),
78136
),
79137
],

lib/provider/history/top/albums.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ class HistoryTopAlbumsNotifier extends FamilyPaginatedAsyncNotifier<
4646
HistoryDuration.days7 => "strftime('%s', 'now', 'weekday 0', '-7 days')",
4747
HistoryDuration.days30 => "strftime('%s', 'now', 'start of month')",
4848
HistoryDuration.months6 =>
49-
"strftime('%s', 'start of month', '-5 months')",
50-
HistoryDuration.year => "strftime('%s', 'start of year')",
51-
HistoryDuration.years2 => "strftime('%s', 'start of year', '-1 year')",
49+
"strftime('%s', date('now', '-5 months', 'start of month'))",
50+
HistoryDuration.year => "strftime('%s', date('now', 'start of year'))",
51+
HistoryDuration.years2 =>
52+
"strftime('%s', date('now', '-1 years', 'start of year'))",
5253
};
5354

5455
return database.customSelect(
@@ -59,7 +60,7 @@ class HistoryTopAlbumsNotifier extends FamilyPaginatedAsyncNotifier<
5960
r"""
6061
json_extract(history_table.data, '$.album') as data,
6162
json_extract(history_table.data, '$.album.id') as item_id,
62-
json_extract(history_table.data, '$.album.type') as type
63+
'album' as type
6364
"""
6465
"""
6566
FROM history_table

lib/provider/history/top/tracks.dart

+20-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,26 @@ class HistoryTopTracksNotifier extends FamilyPaginatedAsyncNotifier<
6262
..where(
6363
(tbl) =>
6464
tbl.type.equalsValue(HistoryEntryType.track) &
65-
tbl.createdAt.isBiggerOrEqualValue(
66-
DateTime.now().subtract(arg.duration),
67-
),
65+
tbl.createdAt.isBiggerOrEqualValue(switch (arg) {
66+
HistoryDuration.allTime => DateTime(1970),
67+
// from start of the week
68+
HistoryDuration.days7 => DateTime.now()
69+
.subtract(Duration(days: DateTime.now().weekday - 1)),
70+
// from start of the month
71+
HistoryDuration.days30 =>
72+
DateTime.now().subtract(Duration(days: DateTime.now().day - 1)),
73+
// from start of the 6th month
74+
HistoryDuration.months6 => DateTime.now()
75+
.subtract(Duration(days: DateTime.now().day - 1))
76+
.subtract(const Duration(days: 30 * 6)),
77+
// from start of the year
78+
HistoryDuration.year => DateTime.now()
79+
.subtract(Duration(days: DateTime.now().day - 1))
80+
.subtract(const Duration(days: 30 * 12)),
81+
HistoryDuration.years2 => DateTime.now()
82+
.subtract(Duration(days: DateTime.now().day - 1))
83+
.subtract(const Duration(days: 30 * 12 * 2)),
84+
}),
6885
);
6986
}
7087

0 commit comments

Comments
 (0)