This commit is contained in:
Imran Remtulla
2023-09-10 23:11:06 -04:00
parent 118e05a0fa
commit 2aea1d2631
3 changed files with 86 additions and 63 deletions

View File

@@ -28,8 +28,8 @@ class _ImportExportPageState extends State<ImportExportPage> {
@override
Widget build(BuildContext context) {
SourceProvider sourceProvider = SourceProvider();
var appsProvider = context.read<AppsProvider>();
var settingsProvider = context.read<SettingsProvider>();
var appsProvider = context.watch<AppsProvider>();
var settingsProvider = context.watch<SettingsProvider>();
var outlineButtonStyle = ButtonStyle(
shape: MaterialStateProperty.all(
@@ -102,10 +102,12 @@ class _ImportExportPageState extends State<ImportExportPage> {
});
}
runObtainiumExport() {
runObtainiumExport() async {
HapticFeedback.selectionClick();
appsProvider
.exportApps(pickOnly: settingsProvider.exportDir == null)
.exportApps(
pickOnly: (await settingsProvider.getExportDir()) == null,
sp: settingsProvider)
.then((String? result) {
if (result != null) {
showError(tr('exportedTo', args: [result]), context);
@@ -305,56 +307,69 @@ class _ImportExportPageState extends State<ImportExportPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: TextButton(
style: outlineButtonStyle,
onPressed: appsProvider.apps.isEmpty ||
importInProgress
? null
: runObtainiumExport,
child: Text(tr(
settingsProvider.exportDir != null
? 'obtainiumExport'
: 'pickExportDirKeepLastN')))),
const SizedBox(
width: 16,
),
Expanded(
child: TextButton(
style: outlineButtonStyle,
onPressed: importInProgress
? null
: runObtainiumImport,
child: Text(tr('obtainiumImport'))))
],
),
if (settingsProvider.exportDir != null)
Column(
children: [
const SizedBox(height: 16),
GeneratedForm(
items: [
[
GeneratedFormSwitch(
'autoExportOnChanges',
label: tr('autoExportOnChanges'),
defaultValue:
settingsProvider.autoExportOnChanges,
)
]
FutureBuilder(
future: settingsProvider.getExportDir(),
builder: (context, snapshot) {
return Column(
children: [
Row(
children: [
Expanded(
child: TextButton(
style: outlineButtonStyle,
onPressed: appsProvider.apps.isEmpty ||
importInProgress
? null
: runObtainiumExport,
child: Text(tr(snapshot.data != null
? 'obtainiumExport'
: 'pickExportDir')),
)),
const SizedBox(
width: 16,
),
Expanded(
child: TextButton(
style: outlineButtonStyle,
onPressed: importInProgress
? null
: runObtainiumImport,
child: Text(tr('obtainiumImport'))))
],
onValueChanges: (value, valid, isBuilding) {
if (valid && !isBuilding) {
if (value['autoExportOnChanges'] != null) {
settingsProvider.autoExportOnChanges =
value['autoExportOnChanges'] == true;
}
}
}),
],
),
),
if (snapshot.data != null)
Column(
children: [
const SizedBox(height: 16),
GeneratedForm(
items: [
[
GeneratedFormSwitch(
'autoExportOnChanges',
label: tr('autoExportOnChanges'),
defaultValue: settingsProvider
.autoExportOnChanges,
)
]
],
onValueChanges:
(value, valid, isBuilding) {
if (valid && !isBuilding) {
if (value['autoExportOnChanges'] !=
null) {
settingsProvider
.autoExportOnChanges = value[
'autoExportOnChanges'] ==
true;
}
}
}),
],
),
],
);
},
),
if (importInProgress)
const Column(
children: [

View File

@@ -1095,8 +1095,10 @@ class AppsProvider with ChangeNotifier {
return updateAppIds;
}
Future<String?> exportApps({bool pickOnly = false, isAuto = false}) async {
var exportDir = settingsProvider.exportDir;
Future<String?> exportApps(
{bool pickOnly = false, isAuto = false, SettingsProvider? sp}) async {
SettingsProvider settingsProvider = sp ?? this.settingsProvider;
var exportDir = await settingsProvider.getExportDir();
if (isAuto) {
if (exportDir == null) {
logs.add('Skipping auto-export as dir is not set.');
@@ -1112,13 +1114,12 @@ class AppsProvider with ChangeNotifier {
logs.add('Previous auto-export deleted.');
}
}
exportDir = settingsProvider.exportDir;
if (exportDir == null || pickOnly) {
await settingsProvider.pickExportDirKeepLastN();
exportDir = settingsProvider.exportDir;
await settingsProvider.pickExportDir();
exportDir = await settingsProvider.getExportDir();
}
if (exportDir == null) {
throw ObtainiumError(tr('unexpectedError'));
return null;
}
String? returnPath;
if (!pickOnly) {

View File

@@ -362,18 +362,25 @@ class SettingsProvider with ChangeNotifier {
notifyListeners();
}
Uri? get exportDir {
Future<Uri?> getExportDir() async {
var uriString = prefs?.getString('exportDir');
if (uriString != null) {
return Uri.parse(uriString);
Uri? uri = Uri.parse(uriString);
if (!(await saf.canRead(uri) ?? false) ||
!(await saf.canWrite(uri) ?? false)) {
uri = null;
prefs?.remove('exportDir');
notifyListeners();
}
return uri;
} else {
return null;
}
}
Future<void> pickExportDirKeepLastN({bool remove = false}) async {
Future<void> pickExportDir({bool remove = false}) async {
var existingSAFPerms = (await saf.persistedUriPermissions()) ?? [];
var currentOneWayDataSyncDir = exportDir;
var currentOneWayDataSyncDir = await getExportDir();
Uri? newOneWayDataSyncDir;
if (!remove) {
newOneWayDataSyncDir = (await saf.openDocumentTree());