Skip to content

Commit

Permalink
feat: Allow choosing a custom directory for local backups (#302)
Browse files Browse the repository at this point in the history
* Allow choosing export directory for local backups

* Fix bug when user cancels file picker dialogue

* refactor: Remove flutter logic from the export service

* refactor: Null assertion logic

---------

Co-authored-by: Enrique Lozano Cebriano <[email protected]>
  • Loading branch information
victor-marino and enrique-lozano authored Jan 23, 2025
1 parent 6af81db commit 8db8b43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 49 deletions.
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.

0 comments on commit 8db8b43

Please sign in to comment.