mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
chore: fix top album and track invalid time frame operations
This commit is contained in:
parent
3bdc46da4d
commit
7927a3e404
@ -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';
|
||||||
|
@ -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,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -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,23 +66,72 @@ class StatsStreamFeesPage extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Skeletonizer.sliver(
|
SliverToBoxAdapter(
|
||||||
enabled: topTracks.isLoading && !topTracks.isLoadingNextPage,
|
child: Padding(
|
||||||
child: SliverInfiniteList(
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||||
onFetchData: () async {
|
child: Row(
|
||||||
await topTracksNotifier.fetchMore();
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
},
|
children: [
|
||||||
hasError: topTracks.hasError,
|
Text(
|
||||||
isLoading: topTracks.isLoading && !topTracks.isLoadingNextPage,
|
"Total ${usdFormatter.format(total)}",
|
||||||
hasReachedMax: topTracks.asData?.value.hasMore ?? true,
|
style: textTheme.titleLarge,
|
||||||
itemCount: artistsData.length,
|
),
|
||||||
itemBuilder: (context, index) {
|
DropdownButton<HistoryDuration>(
|
||||||
final artist = artistsData[index];
|
value: duration.value,
|
||||||
return StatsArtistItem(
|
onChanged: (value) {
|
||||||
artist: artist.artist,
|
if (value == null) return;
|
||||||
info: Text(usdFormatter.format(artist.count * 0.005)),
|
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,
|
||||||
|
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(usdFormatter.format(artist.count * 0.005)),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -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
|
||||||
|
@ -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)),
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user