Skip to content

Commit

Permalink
added delete all option in downloaded screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikh Haziq committed Oct 30, 2024
1 parent ff38f73 commit e401e77
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/app_config.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
24 changes: 24 additions & 0 deletions lib/screens/saved_screen/download_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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())));
})
Expand All @@ -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<Widget>((indexedSong) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ List<SettingItem> 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) {
Expand Down
14 changes: 8 additions & 6 deletions lib/services/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DownloadManager {
quality: GetIt.I<SettingsManager>().downloadQuality.name.toLowerCase());
int start = 0;
int end = audioSource.size.totalBytes;

Stream<List<int>> stream = AudioStreamClient().getAudioStream(audioSource,start:start,end: end );
int total = audioSource.size.totalBytes;
List<int> received = [];
Expand All @@ -51,7 +52,6 @@ class DownloadManager {
'status': 'PROCESSING',
'progress': 0,
});

stream.listen(
(data) async {
received.addAll(data);
Expand All @@ -63,13 +63,15 @@ class DownloadManager {
},
onDone: () async {
if (received.length == total) {
File? file = await GetIt.I<FileStorage>().saveMusic(received, song);
File? file = await GetIt.I<FileStorage>().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']);
Expand All @@ -79,6 +81,7 @@ class DownloadManager {
},
onError: (err) async {
await _box.delete(song['videoId']);
print(err);
_downloadNext(); // Trigger next download
},
);
Expand Down Expand Up @@ -120,12 +123,11 @@ class DownloadManager {
StreamManifest manifest = await ytExplode.videos.streamsClient.getManifest(videoId);
List<AudioOnlyStreamInfo> 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;
}
}
}
21 changes: 11 additions & 10 deletions lib/services/file_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,29 @@ class FileStorage {
}
}

Future<File?> saveMusic(List<int> data, Map song) async {
Future<File?> saveMusic(List<int> data, Map song,{extension='m4a'}) async {
String fileName = song['title'];
final RegExp avoid = RegExp(r'[\.\\\*\:\(\)\"\?#/;\|]');
fileName = fileName.replaceAll(avoid, '').replaceAll("'", '');
//fileName = Uri.decodeFull(fileName);
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 {
if (await file.exists()) {
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'],
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e401e77

Please sign in to comment.