From 925fa86271fa10ff77b8137ba8d09b8067d0e819 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 25 Jun 2023 17:03:36 +0600 Subject: [PATCH] feat: download button on each track --- .../shared/themed_button_tab_bar.dart | 6 ++---- .../shared/track_table/track_options.dart | 20 +++++++++++++++---- lib/pages/home/home.dart | 4 ++-- lib/pages/library/library.dart | 20 ++++++++++++++----- lib/pages/lyrics/lyrics.dart | 4 ++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/lib/components/shared/themed_button_tab_bar.dart b/lib/components/shared/themed_button_tab_bar.dart index ee107088f..d38c3a196 100644 --- a/lib/components/shared/themed_button_tab_bar.dart +++ b/lib/components/shared/themed_button_tab_bar.dart @@ -6,7 +6,7 @@ import 'package:spotube/hooks/use_brightness_value.dart'; import 'package:spotube/utils/platform.dart'; class ThemedButtonsTabBar extends HookWidget implements PreferredSizeWidget { - final List tabs; + final List tabs; const ThemedButtonsTabBar({Key? key, required this.tabs}) : super(key: key); @override @@ -48,9 +48,7 @@ class ThemedButtonsTabBar extends HookWidget implements PreferredSizeWidget { unselectedLabelStyle: theme.textTheme.labelLarge?.copyWith( color: theme.colorScheme.primary, ), - tabs: tabs.map((tab) { - return Tab(text: " $tab "); - }).toList(), + tabs: tabs, ), ); } diff --git a/lib/components/shared/track_table/track_options.dart b/lib/components/shared/track_table/track_options.dart index e3b5cee25..13c1e5d56 100644 --- a/lib/components/shared/track_table/track_options.dart +++ b/lib/components/shared/track_table/track_options.dart @@ -16,6 +16,7 @@ import 'package:spotube/extensions/context.dart'; import 'package:spotube/models/local_track.dart'; import 'package:spotube/provider/authentication_provider.dart'; import 'package:spotube/provider/blacklist_provider.dart'; +import 'package:spotube/provider/download_manager_provider.dart'; import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart'; import 'package:spotube/services/mutations/mutations.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; @@ -31,6 +32,7 @@ enum TrackOptionValue { playNext, favorite, details, + download, } class TrackOptions extends HookConsumerWidget { @@ -75,7 +77,8 @@ class TrackOptions extends HookConsumerWidget { final playback = ref.watch(ProxyPlaylistNotifier.notifier); final scaffoldMessenger = ScaffoldMessenger.of(context); final auth = ref.watch(AuthenticationNotifier.provider); - + ref.watch(downloadManagerProvider); + final downloadManager = ref.watch(downloadManagerProvider.notifier); final blacklist = ref.watch(BlackListNotifier.provider); final favorites = useTrackToggleLike(track, ref); @@ -171,6 +174,9 @@ class TrackOptions extends HookConsumerWidget { builder: (context) => TrackDetailsDialog(track: track), ); break; + case TrackOptionValue.download: + await downloadManager.enqueue(track); + break; } }, icon: const Icon(SpotubeIcons.moreHorizontal), @@ -256,12 +262,18 @@ class TrackOptions extends HookConsumerWidget { value: TrackOptionValue.removeFromPlaylist, leading: (removeTrack.isMutating || !removeTrack.hasData) && removingTrack.value == track.uri - ? const Center( - child: CircularProgressIndicator(), - ) + ? const CircularProgressIndicator() : const Icon(SpotubeIcons.removeFilled), title: Text(context.l10n.remove_from_playlist), ), + PopSheetEntry( + value: TrackOptionValue.download, + enabled: downloadManager.activeItem?.id != track.id!, + leading: downloadManager.activeItem?.id == track.id! + ? const CircularProgressIndicator() + : const Icon(SpotubeIcons.download), + title: Text(context.l10n.download_track), + ), PopSheetEntry( value: TrackOptionValue.blacklist, leading: const Icon(SpotubeIcons.playlistRemove), diff --git a/lib/pages/home/home.dart b/lib/pages/home/home.dart index 6d663cf1d..34f136b6c 100644 --- a/lib/pages/home/home.dart +++ b/lib/pages/home/home.dart @@ -19,8 +19,8 @@ class HomePage extends HookConsumerWidget { leadingWidth: double.infinity, leading: ThemedButtonsTabBar( tabs: [ - context.l10n.personalized, - context.l10n.genre, + Tab(text: " ${context.l10n.personalized} "), + Tab(text: " ${context.l10n.genre} "), ], ), ), diff --git a/lib/pages/library/library.dart b/lib/pages/library/library.dart index 2fbcef0fc..15434f19b 100644 --- a/lib/pages/library/library.dart +++ b/lib/pages/library/library.dart @@ -9,11 +9,15 @@ import 'package:spotube/components/library/user_downloads.dart'; import 'package:spotube/components/library/user_playlists.dart'; import 'package:spotube/components/shared/themed_button_tab_bar.dart'; import 'package:spotube/extensions/context.dart'; +import 'package:spotube/provider/download_manager_provider.dart'; class LibraryPage extends HookConsumerWidget { const LibraryPage({Key? key}) : super(key: key); @override Widget build(BuildContext context, ref) { + final downloadingCount = + ref.watch(downloadManagerProvider.select((s) => s.length)); + return DefaultTabController( length: 5, child: SafeArea( @@ -23,11 +27,17 @@ class LibraryPage extends HookConsumerWidget { centerTitle: true, leading: ThemedButtonsTabBar( tabs: [ - context.l10n.playlists, - context.l10n.tracks, - context.l10n.downloads, - context.l10n.artists, - context.l10n.albums, + Tab(text: " ${context.l10n.playlists} "), + Tab(text: " ${context.l10n.tracks} "), + Tab( + child: Badge( + isLabelVisible: downloadingCount > 0, + label: Text(downloadingCount.toString()), + child: Text(" ${context.l10n.downloads} "), + ), + ), + Tab(text: " ${context.l10n.artists} "), + Tab(text: " ${context.l10n.albums} "), ], ), leadingWidth: double.infinity, diff --git a/lib/pages/lyrics/lyrics.dart b/lib/pages/lyrics/lyrics.dart index d4c29449d..0e62f3b76 100644 --- a/lib/pages/lyrics/lyrics.dart +++ b/lib/pages/lyrics/lyrics.dart @@ -46,8 +46,8 @@ class LyricsPage extends HookConsumerWidget { final tabbar = ThemedButtonsTabBar( tabs: [ - context.l10n.synced, - context.l10n.plain, + Tab(text: " ${context.l10n.synced} "), + Tab(text: " ${context.l10n.plain} "), ], );