Skip to content

Commit

Permalink
feat(mpris): MPRIS metadata are now updated in realtime
Browse files Browse the repository at this point in the history
refactor(download-button): use download provider and queue instead of custom logic
  • Loading branch information
KRTirtho committed Sep 9, 2022
1 parent cae9993 commit d9addcd
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 235 deletions.
2 changes: 1 addition & 1 deletion lib/components/Home/Home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:spotube/components/Home/SpotubeNavigationBar.dart';
import 'package:spotube/components/LoaderShimmers/ShimmerCategories.dart';
import 'package:spotube/components/Lyrics/SyncedLyrics.dart';
import 'package:spotube/components/Search/Search.dart';
import 'package:spotube/components/Shared/DownloadTrackButton.dart';
import 'package:spotube/components/Shared/ReplaceDownloadedFileDialog.dart';
import 'package:spotube/components/Shared/PageWindowTitleBar.dart';
import 'package:spotube/components/Player/Player.dart';
import 'package:spotube/components/Library/UserLibrary.dart';
Expand Down
45 changes: 41 additions & 4 deletions lib/components/Player/PlayerActions.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/components/Library/UserLocalTracks.dart';
import 'package:spotube/components/Player/PlayerQueue.dart';
import 'package:spotube/components/Shared/DownloadTrackButton.dart';
import 'package:spotube/components/Shared/HeartButton.dart';
import 'package:spotube/hooks/useForceUpdate.dart';
import 'package:spotube/models/Logger.dart';
import 'package:spotube/provider/Auth.dart';
import 'package:spotube/provider/Downloader.dart';
import 'package:spotube/provider/Playback.dart';
import 'package:spotube/provider/SpotifyDI.dart';
import 'package:spotube/provider/SpotifyRequests.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

class PlayerActions extends HookConsumerWidget {
final MainAxisAlignment mainAxisAlignment;
Expand All @@ -27,7 +30,25 @@ class PlayerActions extends HookConsumerWidget {
final SpotifyApi spotifyApi = ref.watch(spotifyProvider);
final Playback playback = ref.watch(playbackProvider);
final Auth auth = ref.watch(authProvider);
final downloader = ref.watch(downloaderProvider);
final update = useForceUpdate();
final isInQueue =
downloader.inQueue.any((element) => element.id == playback.track?.id);
final localTracks = ref.watch(localTracksProvider).value;

final isDownloaded = useMemoized(() {
return localTracks?.any(
(element) =>
element.name == playback.track?.name &&
element.album?.name == playback.track?.album?.name &&
TypeConversionUtils.artists_X_String<Artist>(
element.artists ?? []) ==
TypeConversionUtils.artists_X_String<Artist>(
playback.track?.artists ?? []),
) ==
true;
}, [localTracks, playback.track]);

return Row(
mainAxisAlignment: mainAxisAlignment,
children: [
Expand Down Expand Up @@ -56,9 +77,25 @@ class PlayerActions extends HookConsumerWidget {
: null,
),
if (!kIsWeb)
DownloadTrackButton(
track: playback.track,
),
if (isInQueue)
const SizedBox(
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
),
height: 20,
width: 20,
)
else
IconButton(
icon: Icon(
isDownloaded
? Icons.download_done_rounded
: Icons.download_rounded,
),
onPressed: playback.track != null
? () => downloader.addToQueue(playback.track!)
: null,
),
if (auth.isLoggedIn)
FutureBuilder<bool>(
future: playback.track?.id != null
Expand Down
228 changes: 0 additions & 228 deletions lib/components/Shared/DownloadTrackButton.dart

This file was deleted.

31 changes: 31 additions & 0 deletions lib/components/Shared/ReplaceDownloadedFileDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:spotify/spotify.dart';

class ReplaceDownloadedFileDialog extends StatelessWidget {
final Track track;
const ReplaceDownloadedFileDialog({required this.track, Key? key})
: super(key: key);

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("Track ${track.name} Already Exists"),
content:
const Text("Do you want to replace the already downloaded track?"),
actions: [
TextButton(
child: const Text("No"),
onPressed: () {
Navigator.pop(context, false);
},
),
TextButton(
child: const Text("Yes"),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:go_router/go_router.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/components/Shared/DownloadTrackButton.dart';
import 'package:spotube/components/Shared/ReplaceDownloadedFileDialog.dart';
import 'package:spotube/entities/CacheTrack.dart';
import 'package:spotube/models/GoRouteDeclarations.dart';
import 'package:spotube/models/LocalStorageKeys.dart';
Expand Down
4 changes: 4 additions & 0 deletions lib/provider/Playback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class Playback extends PersistedChangeNotifier {
await player.setVolume(volume);
}

addListener(() {
_linuxAudioService?.player.updateProperties(this);
});

_subscriptions.addAll([
player.onPlayerStateChanged.listen(
(state) async {
Expand Down
Loading

0 comments on commit d9addcd

Please sign in to comment.