Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle selecting files and folders for patch options correctly #2144

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ManagerAPI {
bool releaseBuild = false;
bool suggestedAppVersionSelected = true;
bool isDynamicThemeAvailable = false;
bool isScopedStorageAvailable = false;
int sdkVersion = 0;
String storedPatchesFile = '/selected-patches.json';
String keystoreFile =
'/sdcard/Android/data/app.revanced.manager.flutter/files/revanced-manager.keystore';
Expand All @@ -55,8 +57,11 @@ class ManagerAPI {
Future<void> initialize() async {
_prefs = await SharedPreferences.getInstance();
isRooted = await _rootAPI.isRooted();
isDynamicThemeAvailable =
(await getSdkVersion()) >= 31; // ANDROID_12_SDK_VERSION = 31
if (sdkVersion == 0) {
sdkVersion = await getSdkVersion();
}
isDynamicThemeAvailable = sdkVersion >= 31; // ANDROID_12_SDK_VERSION = 31
isScopedStorageAvailable = sdkVersion >= 30; // ANDROID_11_SDK_VERSION = 30
storedPatchesFile =
(await getApplicationDocumentsDirectory()).path + storedPatchesFile;
if (kReleaseMode) {
Expand Down
66 changes: 47 additions & 19 deletions lib/ui/widgets/patchesSelectorView/patch_options_fields.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/gen/strings.g.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/ui/views/patch_options/patch_options_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_card.dart';

Expand Down Expand Up @@ -398,6 +401,7 @@ class TextFieldForPatchOption extends StatefulWidget {
}

class _TextFieldForPatchOptionState extends State<TextFieldForPatchOption> {
final ManagerAPI _managerAPI = locator<ManagerAPI>();
final TextEditingController controller = TextEditingController();
String? selectedKey;
String? defaultValue;
Expand Down Expand Up @@ -524,29 +528,53 @@ class _TextFieldForPatchOptionState extends State<TextFieldForPatchOption> {
];
},
onSelected: (String selection) async {
switch (selection) {
case 'file':
final String? result = await FlutterFileDialog.pickFile();
if (result != null) {
controller.text = result;
widget.onChanged(controller.text);
// manageExternalStorage permission is required for file/folder selection
// otherwise, the app will not complain, but the patches will error out
// the same way as if the user selected an empty file/folder.
// Android 11 and above requires the manageExternalStorage permission
final Map<String, dynamic> availableActions = {
Francesco146 marked this conversation as resolved.
Show resolved Hide resolved
'file': () async {
if (_managerAPI.isScopedStorageAvailable) {
final permission =
await Permission.manageExternalStorage.request();
if (!permission.isGranted) {
return;
}
}
final FilePickerResult? result =
await FilePicker.platform.pickFiles();
if (result == null) {
return;
}
controller.text = result.files.single.path!;
widget.onChanged(controller.text);
},
'folder': () async {
if (_managerAPI.isScopedStorageAvailable) {
final permission =
await Permission.manageExternalStorage.request();
if (!permission.isGranted) {
return;
}
}
break;
case 'folder':
final DirectoryLocation? result =
await FlutterFileDialog.pickDirectory();
if (result != null) {
controller.text = result.toString();
widget.onChanged(controller.text);
final String? result =
await FilePicker.platform.getDirectoryPath();
if (result == null) {
return;
}
break;
case 'remove':
controller.text = result;
widget.onChanged(controller.text);
},
'remove': () {
widget.removeValue!();
break;
case 'null':
},
'null': () {
controller.text = '';
widget.onChanged(null);
break;
},
};
if (availableActions.containsKey(selection)) {
await availableActions[selection]!();
}
},
),
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
dynamic_color: ^1.7.0
dynamic_themes: ^1.1.0
expandable: ^5.0.1
file_picker: ^8.0.5
Francesco146 marked this conversation as resolved.
Show resolved Hide resolved
flutter:
sdk: flutter
flutter_background:
Expand Down