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

Allow choosing a custom directory for local backups #302

Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 15 additions & 5 deletions lib/app/settings/export_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:monekin/core/database/services/transaction/transaction_service.dart';
import 'package:monekin/core/presentation/widgets/dates/outlinedButtonStacked.dart';
import 'package:monekin/core/presentation/widgets/persistent_footer_button.dart';
import 'package:monekin/core/presentation/widgets/transaction_filter/transaction_filters.dart';
import 'package:monekin/core/utils/logger.dart';
import 'package:monekin/i18n/translations.g.dart';

import '../../core/database/backup/backup_database_service.dart';
Expand Down Expand Up @@ -69,19 +69,29 @@ class _ExportDataPageState extends State<ExportDataPage> {
child: Text(t.backup.export.title),
onPressed: () async {
final messeger = ScaffoldMessenger.of(context);
String? path = await FilePicker.platform.getDirectoryPath();

if (path == null) {
// TODO: Maybe we should also add a snackbar here
return;
}

if (selectedExportFormat == _ExportFormats.db) {
await BackupDatabaseService()
.downloadDatabaseFile(context)
.exportDatabaseFile(path)
.then((value) {
Logger.printDebug('EEEEEEEEEEE');
messeger.showSnackBar(SnackBar(
content: Text(t.backup.export.success(x: path)),
));
}).catchError((err) {
Logger.printDebug(err);
messeger.showSnackBar(SnackBar(
content: Text('$err'),
));
});
} else {
await BackupDatabaseService()
.exportSpreadsheet(
context,
path,
await TransactionService.instance
.getTransactions(filters: filters)
.first)
Expand Down
30 changes: 10 additions & 20 deletions lib/core/database/backup/backup_database_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,31 @@ import 'dart:io';

import 'package:csv/csv.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:monekin/core/database/app_db.dart';
import 'package:monekin/core/database/services/app-data/app_data_service.dart';
import 'package:monekin/core/models/transaction/transaction.dart';
import 'package:monekin/core/utils/get_download_path.dart';
import 'package:monekin/core/utils/logger.dart';
import 'package:path/path.dart' as path;

class BackupDatabaseService {
AppDB db = AppDB.instance;

Future<void> downloadDatabaseFile(BuildContext context) async {
final messeger = ScaffoldMessenger.of(context);

Future<void> exportDatabaseFile(String exportPath) async {
List<int> dbFileInBytes = await File(await db.databasePath).readAsBytes();

String downloadPath = await getDownloadPath();
downloadPath = path.join(
downloadPath,
exportPath = path.join(
exportPath,
"monekin-${DateFormat('yyyyMMdd-Hms').format(DateTime.now())}.db",
);

File downloadFile = File(downloadPath);
File downloadFile = File(exportPath);

await downloadFile.writeAsBytes(dbFileInBytes);

messeger.showSnackBar(SnackBar(
content: Text('Base de datos descargada con exito en $downloadPath'),
));
}

Future<String> exportSpreadsheet(
BuildContext context,
String exportPath,
List<MoneyTransaction> data, {
String format = 'csv',
String separator = ',',
Expand Down Expand Up @@ -94,15 +85,14 @@ class BackupDatabaseService {
}
}

String downloadPath = await getDownloadPath();
downloadPath =
'${downloadPath}Transactions-${DateFormat('yyyyMMdd-Hms').format(DateTime.now())}.csv';
exportPath =
'${exportPath}Transactions-${DateFormat('yyyyMMdd-Hms').format(DateTime.now())}.csv';

File downloadFile = File(downloadPath);
File exportFile = File(exportPath);

await downloadFile.writeAsString(csvData);
await exportFile.writeAsString(csvData);

return downloadPath;
return exportPath;
}

Future<bool> importDatabase() async {
Expand Down
24 changes: 0 additions & 24 deletions lib/core/utils/get_download_path.dart

This file was deleted.