Skip to content

Commit

Permalink
Support backup database on iOS (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped authored Jan 6, 2025
1 parent 1af78d6 commit a3d747f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
44 changes: 28 additions & 16 deletions lib/core/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:path/path.dart';
import '../common/constants.dart';
import 'package:logging/logging.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

// Initialize the logger
final Logger _logger = Logger('DatabaseLogger');
Expand Down Expand Up @@ -577,7 +578,8 @@ class DatabaseHelper {
if (Platform.isAndroid) {
backupPath = '/storage/emulated/0/Download/backup_database.db';
} else if (Platform.isIOS) {
throw Exception('Unsupported platform');
final directory = await getApplicationDocumentsDirectory();
backupPath = '${directory.path}/backup_database.db';
} else {
throw Exception('Unsupported platform');
}
Expand All @@ -596,21 +598,31 @@ class DatabaseHelper {

Future<void> restoreDatabase(BuildContext context) async {
try {
// Use file picker to select the backup file
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.any,
);

if (result == null) {
print('No file selected');
await _showDialog(context, 'Restore Failed', 'No file selected');
return;
}
String file_path;
if (Platform.isIOS) {
final directory = await getApplicationDocumentsDirectory();
file_path = '${directory.path}/backup_database.db';
} else if (Platform.isAndroid) {
file_path = '/storage/emulated/0/Download/backup_database.db';

// Use file picker to select the backup file
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.any,
);

String selectedFilePath = result.files.single.path!;
File selectedFile = File(selectedFilePath);
if (result == null) {
print('No file selected');
await _showDialog(context, 'Restore Failed', 'No file selected');
return;
}

file_path = result.files.single.path!;
} else {
throw Exception('Unsupported platform');
}

// Check if the backup file exists
File selectedFile = File(file_path);
if (!await selectedFile.exists()) {
print('File does not exist');
await _showDialog(context, 'Restore Failed', 'File does not exist');
Expand All @@ -621,7 +633,7 @@ class DatabaseHelper {
String dbPath = await _databasePath();

// Open the backup database
Database backupDb = await openDatabase(selectedFile.path);
Database backupDb = await openDatabase(file_path);

// Open the destination database
Database destinationDb = await openDatabase(dbPath);
Expand Down Expand Up @@ -664,9 +676,9 @@ class DatabaseHelper {
// Close the backup database after transferring records
await backupDb.close();

print('Database restored successfully from $selectedFilePath');
print('Database restored successfully from $file_path');
await _showDialog(context, 'Restore Successful',
'Database restored successfully from $selectedFilePath');
'Database restored successfully from $file_path');
} catch (e) {
print('Failed to restore database: $e');
await _showDialog(
Expand Down
24 changes: 1 addition & 23 deletions lib/widgets/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,29 +428,7 @@ class SettingsModalState extends State<SettingsModal> {
trailing: IconButton(
icon: const Icon(Icons.backup, color: Colors.blueAccent),
onPressed: () async {
if (Platform.isIOS) {
// Show dialog informing the user that backup is only available on Android
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Backup Unavailable'),
content: const Text(
'This functionality is only available on Android.'),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
} else {
await _dbHelper.backupDatabase(context);
}
await _dbHelper.backupDatabase(context);
},
),
),
Expand Down

0 comments on commit a3d747f

Please sign in to comment.