From 88d7ce55a59f673d60cd9e85ab062bcb1b7dcbc3 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Thu, 13 Oct 2022 19:21:12 +0600 Subject: [PATCH] feat(downloader): replace /skip all choice for downloaded tracks --- .../Shared/ReplaceDownloadedFileDialog.dart | 41 +++++++++++++++++-- lib/provider/Downloader.dart | 5 ++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/components/Shared/ReplaceDownloadedFileDialog.dart b/lib/components/Shared/ReplaceDownloadedFileDialog.dart index d9427411e..6b6591b76 100644 --- a/lib/components/Shared/ReplaceDownloadedFileDialog.dart +++ b/lib/components/Shared/ReplaceDownloadedFileDialog.dart @@ -1,17 +1,52 @@ import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:spotify/spotify.dart'; -class ReplaceDownloadedFileDialog extends StatelessWidget { +final replaceDownloadedFileState = StateProvider((ref) => null); + +class ReplaceDownloadedFileDialog extends ConsumerWidget { final Track track; const ReplaceDownloadedFileDialog({required this.track, Key? key}) : super(key: key); @override - Widget build(BuildContext context) { + Widget build(BuildContext context, ref) { + final groupValue = ref.watch(replaceDownloadedFileState); + return AlertDialog( title: Text("Track ${track.name} Already Exists"), - content: + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ const Text("Do you want to replace the already downloaded track?"), + RadioListTile( + dense: true, + contentPadding: EdgeInsets.zero, + activeColor: Theme.of(context).primaryColor, + value: true, + groupValue: groupValue, + onChanged: (value) { + if (value != null) { + ref.read(replaceDownloadedFileState.state).state = value; + } + }, + title: const Text("Replace all downloaded tracks"), + ), + RadioListTile( + dense: true, + contentPadding: EdgeInsets.zero, + activeColor: Theme.of(context).primaryColor, + value: false, + groupValue: groupValue, + onChanged: (value) { + if (value != null) { + ref.read(replaceDownloadedFileState.state).state = value; + } + }, + title: const Text("Skip downloading all downloaded tracks"), + ), + ], + ), actions: [ TextButton( child: const Text("No"), diff --git a/lib/provider/Downloader.dart b/lib/provider/Downloader.dart index 5eb6f5c2d..7c03fe99f 100644 --- a/lib/provider/Downloader.dart +++ b/lib/provider/Downloader.dart @@ -8,6 +8,7 @@ import 'package:metadata_god/metadata_god.dart'; import 'package:queue/queue.dart'; import 'package:path/path.dart' as path; import 'package:spotify/spotify.dart' hide Image; +import 'package:spotube/components/Shared/ReplaceDownloadedFileDialog.dart'; import 'package:spotube/models/Logger.dart'; import 'package:spotube/models/SpotubeTrack.dart'; import 'package:spotube/provider/Playback.dart'; @@ -63,8 +64,10 @@ class Downloader with ChangeNotifier { final filename = '$cleanTitle.m4a'; final file = File(path.join(downloadPath, filename)); try { + final replaceFileGlobal = ref.read(replaceDownloadedFileState); logger.v("[addToQueue] Download starting for ${file.path}"); - if (file.existsSync() && await onFileExists?.call(track) != true) { + if (file.existsSync() && + (replaceFileGlobal ?? await onFileExists?.call(track)) != true) { return; } file.createSync(recursive: true);