Skip to content

Commit

Permalink
improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Jan 27, 2023
1 parent 332480b commit 7534c82
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
30 changes: 12 additions & 18 deletions lib/API/musify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:musify/services/data_manager.dart';
import 'package:musify/services/ext_storage.dart';
import 'package:musify/services/lyrics_service.dart';
import 'package:on_audio_query/on_audio_query.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

final yt = YoutubeExplode();
Expand Down Expand Up @@ -260,29 +259,24 @@ Future getSongDetails(dynamic songIndex, dynamic songId) async {
}

Future<List<SongModel>> getDownloadedSongs() async {
var localSongs = <SongModel>[];
if (await ExtStorageProvider.requestPermission(Permission.storage)) {
localSongs = await _audioQuery.querySongs(
path: await ExtStorageProvider.getExtStorage(dirName: 'Music'),
);
}
final localSongs = await _audioQuery.querySongs(
path: await ExtStorageProvider.getExtStorage(dirName: 'Music'),
);

return localSongs;
}

Future<List<SongModel>> getLocalSongs() async {
var localSongs = <SongModel>[];
if (await ExtStorageProvider.requestPermission(Permission.storage)) {
final allSongs = await _audioQuery.querySongs();
localSongs = allSongs
.where(
(song) =>
song.isAlarm == false &&
song.isNotification == false &&
song.isRingtone == false,
)
.toList();
}
final allSongs = await _audioQuery.querySongs();
localSongs = allSongs
.where(
(song) =>
song.isAlarm == false &&
song.isNotification == false &&
song.isRingtone == false,
)
.toList();

return localSongs;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:get_it/get_it.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:musify/services/audio_handler.dart';
import 'package:musify/services/audio_manager.dart';
import 'package:musify/services/download_manager.dart';
import 'package:musify/style/appColors.dart';
import 'package:musify/style/appTheme.dart';
import 'package:musify/ui/morePage.dart';
Expand Down Expand Up @@ -244,6 +245,8 @@ Future<void> initialisation() async {
print(e);
}
}

await checkAudioPerms();
}

@pragma('vm:entry-point')
Expand Down
44 changes: 26 additions & 18 deletions lib/services/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,7 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

Future<void> downloadSong(BuildContext context, dynamic song) async {
if (await Permission.audio.status.isDenied) {
await Permission.audio.request();
if (await Permission.audio.status.isPermanentlyDenied) {
await openAppSettings();
}
}

if (await Permission.storage.status.isDenied) {
await [
Permission.storage,
Permission.accessMediaLocation,
Permission.mediaLibrary,
].request();

if (await Permission.storage.status.isPermanentlyDenied) {
await openAppSettings();
}
}
await checkAudioPerms();

final filename = song['more_info']['singers'] +
' - ' +
Expand Down Expand Up @@ -83,3 +66,28 @@ Future<void> downloadFileFromYT(
await fileStream.flush();
await fileStream.close();
}

Future<void> checkAudioPerms() async {
if (await Permission.storage.status.isDenied) {
await Permission.storage.request();

if (await Permission.storage.status.isPermanentlyDenied) {
await openAppSettings();
}
}

if (await Permission.accessMediaLocation.status.isDenied) {
await Permission.accessMediaLocation.request();
}

if (await Permission.manageExternalStorage.status.isDenied) {
await Permission.manageExternalStorage.request();
}

if (await Permission.audio.status.isDenied) {
await Permission.audio.request();
if (await Permission.audio.status.isPermanentlyDenied) {
await openAppSettings();
}
}
}

0 comments on commit 7534c82

Please sign in to comment.