chore: fix top album and track invalid time frame operations

This commit is contained in:
Kingkor Roy Tirtho 2024-07-01 13:25:28 +06:00
parent 3bdc46da4d
commit 7927a3e404
5 changed files with 103 additions and 28 deletions

View File

@ -4,7 +4,6 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
import 'package:drift/extensions/json1.dart';
import 'package:encrypt/encrypt.dart'; import 'package:encrypt/encrypt.dart';
import 'package:media_kit/media_kit.dart' hide Track; import 'package:media_kit/media_kit.dart' hide Track;
import 'package:path/path.dart'; import 'package:path/path.dart';

View File

@ -33,7 +33,7 @@ class StatsAlbumItem extends StatelessWidget {
Text("${album.albumType?.formatted}"), Text("${album.albumType?.formatted}"),
Flexible( Flexible(
child: ArtistLink( child: ArtistLink(
artists: album.artists!, artists: album.artists ?? [],
mainAxisAlignment: WrapAlignment.start, mainAxisAlignment: WrapAlignment.start,
), ),
), ),

View File

@ -20,16 +20,25 @@ class StatsStreamFeesPage extends HookConsumerWidget {
@override @override
Widget build(BuildContext context, ref) { Widget build(BuildContext context, ref) {
final ThemeData(:textTheme, :hintColor) = Theme.of(context); final ThemeData(:textTheme, :hintColor) = Theme.of(context);
final duration = useState<HistoryDuration>(HistoryDuration.days30);
final topTracks = ref.watch( final topTracks = ref.watch(
historyTopTracksProvider(HistoryDuration.allTime), historyTopTracksProvider(duration.value),
); );
final topTracksNotifier = final topTracksNotifier =
ref.watch(historyTopTracksProvider(HistoryDuration.allTime).notifier); ref.watch(historyTopTracksProvider(duration.value).notifier);
final artistsData = useMemoized( final artistsData = useMemoized(
() => topTracks.asData?.value.artists ?? [], [topTracks.asData?.value]); () => topTracks.asData?.value.artists ?? [], [topTracks.asData?.value]);
final total = useMemoized(
() => artistsData.fold<double>(
0,
(previousValue, element) => previousValue + element.count * 0.005,
),
[artistsData],
);
return Scaffold( return Scaffold(
appBar: const PageWindowTitleBar( appBar: const PageWindowTitleBar(
automaticallyImplyLeading: true, automaticallyImplyLeading: true,
@ -57,7 +66,55 @@ class StatsStreamFeesPage extends HookConsumerWidget {
), ),
), ),
), ),
Skeletonizer.sliver( SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Total ${usdFormatter.format(total)}",
style: textTheme.titleLarge,
),
DropdownButton<HistoryDuration>(
value: duration.value,
onChanged: (value) {
if (value == null) return;
duration.value = value;
},
items: const [
DropdownMenuItem(
value: HistoryDuration.days7,
child: Text("This week"),
),
DropdownMenuItem(
value: HistoryDuration.days30,
child: Text("This month"),
),
DropdownMenuItem(
value: HistoryDuration.months6,
child: Text("Last 6 months"),
),
DropdownMenuItem(
value: HistoryDuration.year,
child: Text("This year"),
),
DropdownMenuItem(
value: HistoryDuration.years2,
child: Text("Last 2 years"),
),
DropdownMenuItem(
value: HistoryDuration.allTime,
child: Text("All time"),
),
],
),
],
),
),
),
SliverSafeArea(
sliver: Skeletonizer.sliver(
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage, enabled: topTracks.isLoading && !topTracks.isLoadingNextPage,
child: SliverInfiniteList( child: SliverInfiniteList(
onFetchData: () async { onFetchData: () async {
@ -76,6 +133,7 @@ class StatsStreamFeesPage extends HookConsumerWidget {
}, },
), ),
), ),
),
], ],
), ),
); );

View File

@ -46,9 +46,10 @@ class HistoryTopAlbumsNotifier extends FamilyPaginatedAsyncNotifier<
HistoryDuration.days7 => "strftime('%s', 'now', 'weekday 0', '-7 days')", HistoryDuration.days7 => "strftime('%s', 'now', 'weekday 0', '-7 days')",
HistoryDuration.days30 => "strftime('%s', 'now', 'start of month')", HistoryDuration.days30 => "strftime('%s', 'now', 'start of month')",
HistoryDuration.months6 => HistoryDuration.months6 =>
"strftime('%s', 'start of month', '-5 months')", "strftime('%s', date('now', '-5 months', 'start of month'))",
HistoryDuration.year => "strftime('%s', 'start of year')", HistoryDuration.year => "strftime('%s', date('now', 'start of year'))",
HistoryDuration.years2 => "strftime('%s', 'start of year', '-1 year')", HistoryDuration.years2 =>
"strftime('%s', date('now', '-1 years', 'start of year'))",
}; };
return database.customSelect( return database.customSelect(
@ -59,7 +60,7 @@ class HistoryTopAlbumsNotifier extends FamilyPaginatedAsyncNotifier<
r""" r"""
json_extract(history_table.data, '$.album') as data, json_extract(history_table.data, '$.album') as data,
json_extract(history_table.data, '$.album.id') as item_id, json_extract(history_table.data, '$.album.id') as item_id,
json_extract(history_table.data, '$.album.type') as type 'album' as type
""" """
""" """
FROM history_table FROM history_table

View File

@ -62,9 +62,26 @@ class HistoryTopTracksNotifier extends FamilyPaginatedAsyncNotifier<
..where( ..where(
(tbl) => (tbl) =>
tbl.type.equalsValue(HistoryEntryType.track) & tbl.type.equalsValue(HistoryEntryType.track) &
tbl.createdAt.isBiggerOrEqualValue( tbl.createdAt.isBiggerOrEqualValue(switch (arg) {
DateTime.now().subtract(arg.duration), HistoryDuration.allTime => DateTime(1970),
), // from start of the week
HistoryDuration.days7 => DateTime.now()
.subtract(Duration(days: DateTime.now().weekday - 1)),
// from start of the month
HistoryDuration.days30 =>
DateTime.now().subtract(Duration(days: DateTime.now().day - 1)),
// from start of the 6th month
HistoryDuration.months6 => DateTime.now()
.subtract(Duration(days: DateTime.now().day - 1))
.subtract(const Duration(days: 30 * 6)),
// from start of the year
HistoryDuration.year => DateTime.now()
.subtract(Duration(days: DateTime.now().day - 1))
.subtract(const Duration(days: 30 * 12)),
HistoryDuration.years2 => DateTime.now()
.subtract(Duration(days: DateTime.now().day - 1))
.subtract(const Duration(days: 30 * 12 * 2)),
}),
); );
} }