From e401e77d58c06bd782ba02c5b7ef3e9d723b7405 Mon Sep 17 00:00:00 2001 From: Sheikh Haziq Date: Wed, 30 Oct 2024 10:39:11 +0530 Subject: [PATCH] added delete all option in downloaded screen --- lib/app_config.dart | 2 +- lib/screens/saved_screen/download_screen.dart | 24 +++++++++++++++++++ .../audio_and_playback_screen_data.dart | 2 +- lib/services/download_manager.dart | 14 ++++++----- lib/services/file_storage.dart | 21 ++++++++-------- pubspec.lock | 12 +++++----- pubspec.yaml | 6 ++--- 7 files changed, 54 insertions(+), 27 deletions(-) diff --git a/lib/app_config.dart b/lib/app_config.dart index 969b40c..cd5c4e6 100644 --- a/lib/app_config.dart +++ b/lib/app_config.dart @@ -1,4 +1,4 @@ -AppConfig appConfig = AppConfig(version: 30, codeName: '2.0.5'); +AppConfig appConfig = AppConfig(version: 31, codeName: '2.0.6'); class AppConfig { int version; diff --git a/lib/screens/saved_screen/download_screen.dart b/lib/screens/saved_screen/download_screen.dart index a46cd55..a0828fe 100644 --- a/lib/screens/saved_screen/download_screen.dart +++ b/lib/screens/saved_screen/download_screen.dart @@ -7,6 +7,8 @@ import 'package:flutter_swipe_action_cell/core/cell.dart'; import 'package:get_it/get_it.dart'; import 'package:gyawun/screens/saved_screen/downloading_screen.dart'; import 'package:gyawun/utils/extensions.dart'; +import 'package:gyawun/utils/pprint.dart'; +import 'package:hive_flutter/hive_flutter.dart'; import '../../generated/l10n.dart'; import '../../services/bottom_message.dart'; @@ -25,6 +27,26 @@ class DownloadScreen extends StatelessWidget { title: Text(S.of(context).Downloads), centerTitle: true, actions: [ + AdaptiveButton(child: Icon(AdaptiveIcons.delete), onPressed: ()async{ + bool shouldDelete = await Modals.showConfirmBottomModal(context, message: 'Are you sure you want to delete all downloaded songs.',isDanger: true,doneText: S.of(context).Yes,cancelText: S.of(context).No); + + if(shouldDelete){ + Modals.showCenterLoadingModal(context); + List songs = Hive.box('DOWNLOADS').values.toList(); + for (var song in songs) { + String path = song['path']; + await Hive.box('DOWNLOADS').delete(song['videoId']); + try{ + File(path).delete(); + }catch(e){ + pprint(e); + } + } + Navigator.pop(context); + } + + }), + const SizedBox(width: 8), AdaptiveButton(child: Icon(AdaptiveIcons.download), onPressed: (){ Navigator.push(context, (MaterialPageRoute(builder: (context) =>const DownloadingScreen()))); }) @@ -42,6 +64,8 @@ class DownloadScreen extends StatelessWidget { .where((song) => ['DOWNLOADED', 'DELETED'].contains(song['status'])) .toList(); + songs.sort((a, b) => (a['timestamp']??0).compareTo(b['timestamp']??0)); + return Column( children: [ ...songs.indexed.map((indexedSong) { diff --git a/lib/screens/settings_screen/playback/audio_and_playback_screen_data.dart b/lib/screens/settings_screen/playback/audio_and_playback_screen_data.dart index 379c871..c17e861 100644 --- a/lib/screens/settings_screen/playback/audio_and_playback_screen_data.dart +++ b/lib/screens/settings_screen/playback/audio_and_playback_screen_data.dart @@ -86,7 +86,7 @@ List audioandplaybackScreenData(BuildContext context) => [ icon: CupertinoIcons.folder, subtitle: (context) { return ValueListenableBuilder(valueListenable: Hive.box('SETTINGS').listenable(keys:['APP_FOLDER']), builder:(context, value, child) { - return Text(value.get('APP_FOLDER',defaultValue:'/storage/emulated/0/Download')); + return Text(value.get('APP_FOLDER',defaultValue:'/storage/emulated/0/Download'),style:const TextStyle(fontSize: 10),); },); }, trailing: (context) { diff --git a/lib/services/download_manager.dart b/lib/services/download_manager.dart index 7b1db91..7a6b200 100644 --- a/lib/services/download_manager.dart +++ b/lib/services/download_manager.dart @@ -43,6 +43,7 @@ class DownloadManager { quality: GetIt.I().downloadQuality.name.toLowerCase()); int start = 0; int end = audioSource.size.totalBytes; + Stream> stream = AudioStreamClient().getAudioStream(audioSource,start:start,end: end ); int total = audioSource.size.totalBytes; List received = []; @@ -51,7 +52,6 @@ class DownloadManager { 'status': 'PROCESSING', 'progress': 0, }); - stream.listen( (data) async { received.addAll(data); @@ -63,13 +63,15 @@ class DownloadManager { }, onDone: () async { if (received.length == total) { - File? file = await GetIt.I().saveMusic(received, song); + File? file = await GetIt.I().saveMusic(received, song,extension: audioSource.container.name); + print(file); if (file != null) { await _box.put(song['videoId'], { ...song, 'status': 'DOWNLOADED', 'progress': 100, 'path': file.path, + 'timestamp':DateTime.now().millisecondsSinceEpoch }); } else { await _box.delete(song['videoId']); @@ -79,6 +81,7 @@ class DownloadManager { }, onError: (err) async { await _box.delete(song['videoId']); + print(err); _downloadNext(); // Trigger next download }, ); @@ -120,12 +123,11 @@ class DownloadManager { StreamManifest manifest = await ytExplode.videos.streamsClient.getManifest(videoId); List streamInfos = manifest.audioOnly .sortByBitrate() - .where((stream) => stream.container == StreamContainer.mp4) + .reversed .toList(); - int qualityIndex = (quality == 'low') ? 0 : streamInfos.length - 1; - return streamInfos[qualityIndex]; + return quality == 'low' ? streamInfos.first:streamInfos.last; } catch (e) { - rethrow; // Allows for further handling in `downloadSong` + rethrow; } } } diff --git a/lib/services/file_storage.dart b/lib/services/file_storage.dart index 7c81b75..ebc4a85 100644 --- a/lib/services/file_storage.dart +++ b/lib/services/file_storage.dart @@ -89,7 +89,7 @@ class FileStorage { } } - Future saveMusic(List data, Map song) async { + Future saveMusic(List data, Map song,{extension='m4a'}) async { String fileName = song['title']; final RegExp avoid = RegExp(r'[\.\\\*\:\(\)\"\?#/;\|]'); fileName = fileName.replaceAll(avoid, '').replaceAll("'", ''); @@ -97,10 +97,10 @@ class FileStorage { if (!(await requestPermissions())) return null; Directory directory = await _getDirectory(storagePaths.musicPath); - File file = File(path.join(directory.path, '$fileName.m4a')); + File file = File(path.join(directory.path, '$fileName.$extension')); int number = 1; while (file.existsSync()) { - file = File((path.join(directory.path, '$fileName($number).m4a'))); + file = File((path.join(directory.path, '$fileName($number).$extension'))); number++; } try { @@ -108,8 +108,10 @@ class FileStorage { await file.delete(); } await file.writeAsBytes(data, flush: true); - - Response res = await get( + + + try{ + Response res = await get( Uri.parse(getEnhancedImage(song['thumbnails'].first['url']))); Tag tag = Tag( title: song['title'], @@ -122,11 +124,10 @@ class FileStorage { pictureType: PictureType.coverFront, ) ]); - pprint(tag.title); - pprint(tag.trackArtist); - pprint(tag.album); - await AudioTags.write(file.path, tag); - + await AudioTags.write(file.path, tag); + }catch(e){ + await file.writeAsBytes(data,flush: true); + } return file; } catch (e) { return null; diff --git a/pubspec.lock b/pubspec.lock index d752252..a6691ad 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -593,10 +593,10 @@ packages: dependency: "direct main" description: name: just_audio - sha256: d8e8aaf417d33e345299c17f6457f72bd4ba0c549dc34607abb5183a354edc4d + sha256: b41646a8241688f1d99c2e69c4da2bb26aa4b3a99795f6ff205c2a165e033fda url: "https://pub.dev" source: hosted - version: "0.9.40" + version: "0.9.41" just_audio_background: dependency: "direct main" description: @@ -609,10 +609,10 @@ packages: dependency: "direct main" description: name: just_audio_media_kit - sha256: "7f57d317fafa04cb3e70b924e8f632ffb7eca7a97a369e1e44738ed89fbd5da1" + sha256: "9f3517213dfc7bbaf6980656feb66c35600f114c7efc0b5b3f4476cd5c18b45e" url: "https://pub.dev" source: hosted - version: "2.0.5" + version: "2.0.6" just_audio_platform_interface: dependency: transitive description: @@ -713,10 +713,10 @@ packages: dependency: transitive description: name: media_kit - sha256: "3289062540e3b8b9746e5c50d95bd78a9289826b7227e253dff806d002b9e67a" + sha256: "1f1deee148533d75129a6f38251ff8388e33ee05fc2d20a6a80e57d6051b7b62" url: "https://pub.dev" source: hosted - version: "1.1.10+1" + version: "1.1.11" media_kit_libs_windows_audio: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 4e2421f..561d3c0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: gyawun description: "A new Flutter project." publish_to: 'none' -version: 2.0.5+30 +version: 2.0.6+31 environment: sdk: '>=3.4.1 <4.0.0' @@ -29,7 +29,7 @@ dependencies: provider: ^6.1.2 flutter_staggered_grid_view: ^0.7.0 flutter_swipe_action_cell: ^3.1.3 - just_audio: ^0.9.40 + just_audio: ^0.9.41 just_audio_background: ^0.0.1-beta.13 audio_video_progress_bar: ^2.0.3 sliding_up_panel: ^2.0.0+1 @@ -50,7 +50,7 @@ dependencies: flutter_expandable_fab: ^2.1.0 flutter_lyric: ^2.0.4+6 media_kit_libs_windows_audio: any - just_audio_media_kit: ^2.0.5 + just_audio_media_kit: ^2.0.6 media_kit_native_event_loop: ^1.0.8 flutter_acrylic: ^1.1.4 fluent_ui: ^4.9.2