Skip to content

Commit

Permalink
chore: request battery optimizations when downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Oct 21, 2023
1 parent 8054329 commit 9889c32
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class SettingsController {
final RxBool showUnknownFieldsInTrackInfoDialog = true.obs;
final RxBool extractFeatArtistFromTitle = true.obs;
final RxBool groupArtworksByAlbum = false.obs;
final RxBool canAskForBatteryOptimizations = true.obs;
final RxList<TagField> tagFieldsToEdit = <TagField>[
TagField.trackNumber,
TagField.year,
Expand Down Expand Up @@ -384,6 +385,7 @@ class SettingsController {
showUnknownFieldsInTrackInfoDialog.value = json['showUnknownFieldsInTrackInfoDialog'] ?? showUnknownFieldsInTrackInfoDialog.value;
extractFeatArtistFromTitle.value = json['extractFeatArtistFromTitle'] ?? extractFeatArtistFromTitle.value;
groupArtworksByAlbum.value = json['groupArtworksByAlbum'] ?? groupArtworksByAlbum.value;
canAskForBatteryOptimizations.value = json['canAskForBatteryOptimizations'] ?? canAskForBatteryOptimizations.value;

final listFromStorage = List<String>.from(json['tagFieldsToEdit'] ?? []);
tagFieldsToEdit.value = listFromStorage.isNotEmpty ? List<TagField>.from(listFromStorage.map((e) => TagField.values.getEnum(e))) : tagFieldsToEdit;
Expand Down Expand Up @@ -577,6 +579,7 @@ class SettingsController {
'showUnknownFieldsInTrackInfoDialog': showUnknownFieldsInTrackInfoDialog.value,
'extractFeatArtistFromTitle': extractFeatArtistFromTitle.value,
'groupArtworksByAlbum': groupArtworksByAlbum.value,
'canAskForBatteryOptimizations': canAskForBatteryOptimizations.value,
'tagFieldsToEdit': tagFieldsToEdit.mapped((element) => element.convertToString),
'wakelockMode': wakelockMode.value.convertToString,
'localVideoMatchingType': localVideoMatchingType.value.convertToString,
Expand Down Expand Up @@ -741,6 +744,7 @@ class SettingsController {
bool? showUnknownFieldsInTrackInfoDialog,
bool? extractFeatArtistFromTitle,
bool? groupArtworksByAlbum,
bool? canAskForBatteryOptimizations,
List<TagField>? tagFieldsToEdit,
WakelockMode? wakelockMode,
LocalVideoMatchingType? localVideoMatchingType,
Expand Down Expand Up @@ -1157,6 +1161,9 @@ class SettingsController {
if (groupArtworksByAlbum != null) {
this.groupArtworksByAlbum.value = groupArtworksByAlbum;
}
if (canAskForBatteryOptimizations != null) {
this.canAskForBatteryOptimizations.value = canAskForBatteryOptimizations;
}
if (tagFieldsToEdit != null) {
tagFieldsToEdit.loop((d, index) {
if (!this.tagFieldsToEdit.contains(d)) {
Expand Down
24 changes: 24 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ Future<bool> requestStoragePermission({bool request = true}) async {
return granted;
}

Future<bool> requestIgnoreBatteryOptimizations() async {
final granted = await Permission.ignoreBatteryOptimizations.isGranted;
if (granted) return true;
settings.save(canAskForBatteryOptimizations: true);
if (!settings.canAskForBatteryOptimizations.value) return false;

snackyy(
message: lang.IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE,
displaySeconds: 5,
top: false,
isError: true,
button: NamidaButton(
text: lang.DONT_ASK_AGAIN,
onPressed: () {
Get.closeCurrentSnackbar();
settings.save(canAskForBatteryOptimizations: false);
},
),
);
await Future.delayed(const Duration(seconds: 1));
final p = await Permission.ignoreBatteryOptimizations.request();
return p.isGranted;
}

Future<bool> requestManageStoragePermission() async {
Future<void> createDir() async => await Directory(settings.defaultBackupLocation.value).create(recursive: true);
if (kSdkVersion < 30) {
Expand Down
1 change: 1 addition & 0 deletions lib/youtube/functions/download_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ Future<void> showDownloadVideoBottomSheet({
),
onTap: () async {
if (!await requestManageStoragePermission()) return;
requestIgnoreBatteryOptimizations();
// ignore: use_build_context_synchronously
Navigator.pop(context);
final downloadedFile = await YoutubeController.inst.downloadYoutubeVideoRaw(
Expand Down

0 comments on commit 9889c32

Please sign in to comment.