-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MANAGE_EXTERNAL_STORAGE Permision deleted
- Loading branch information
AnthonyBasdfg
authored and
AnthonyBasdfg
committed
Jan 15, 2025
1 parent
94c9466
commit 4979863
Showing
14 changed files
with
462 additions
and
505 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<paths> | ||
<files-path | ||
name="backup_files" | ||
path="backups/" /> | ||
</paths> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,89 @@ | ||
import 'dart:io'; | ||
import '../services/backup_service.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:share_plus/share_plus.dart'; | ||
import 'package:file_picker/file_picker.dart'; | ||
import '../local/database.dart'; | ||
import '../../domain/repositories/backup_repository.dart'; | ||
|
||
class BackupRepositoryImpl implements BackupRepository { | ||
final BackupService _backupService; | ||
final AppDatabase database; | ||
|
||
BackupRepositoryImpl(this._backupService); | ||
BackupRepositoryImpl({required this.database}); | ||
|
||
Future<Directory> get _backupDirectory async { | ||
final appDir = await getApplicationDocumentsDirectory(); | ||
final backupDir = Directory('${appDir.path}/backups'); | ||
if (!await backupDir.exists()) { | ||
await backupDir.create(recursive: true); | ||
} | ||
return backupDir; | ||
} | ||
|
||
@override | ||
Future<File> createBackup() async { | ||
final backupDir = await _backupDirectory; | ||
final timestamp = DateTime.now().millisecondsSinceEpoch; | ||
final backupFile = File('${backupDir.path}/backup_$timestamp.db'); | ||
|
||
// Copiar la base de datos actual | ||
final dbPath = await database.getDatabasePath(); | ||
final currentDb = File(dbPath); | ||
await currentDb.copy(backupFile.path); | ||
|
||
return backupFile; | ||
} | ||
|
||
@override | ||
Future<String> createBackup() async { | ||
return await _backupService.createBackup(); | ||
Future<void> shareBackup(File backupFile) async { | ||
await Share.shareXFiles([XFile(backupFile.path)]); | ||
} | ||
|
||
@override | ||
Future<void> restoreBackup(String backupPath) async { | ||
await _backupService.restoreBackup(backupPath); | ||
Future<File?> importBackup() async { | ||
try { | ||
final result = await FilePicker.platform.pickFiles( | ||
type: FileType.custom, | ||
allowedExtensions: ['db'], | ||
); | ||
|
||
if (result != null) { | ||
final backupDir = await _backupDirectory; | ||
final importFile = File(result.files.single.path!); | ||
final newPath = '${backupDir.path}/${result.files.single.name}'; | ||
return await importFile.copy(newPath); | ||
} | ||
return null; | ||
} catch (e) { | ||
rethrow; | ||
} | ||
} | ||
|
||
@override | ||
Future<List<FileSystemEntity>> listBackups() async { | ||
return await _backupService.listBackups(); | ||
Future<List<File>> listBackups() async { | ||
final backupDir = await _backupDirectory; | ||
return backupDir | ||
.listSync() | ||
.whereType<File>() | ||
.where((file) => file.path.endsWith('.db')) | ||
.toList(); | ||
} | ||
|
||
@override | ||
Future<void> restoreBackup(File backupFile) async { | ||
final dbPath = await database.getDatabasePath(); | ||
final currentDb = File(dbPath); | ||
|
||
// Cerrar la conexión con la base de datos | ||
await database.close(); | ||
|
||
// Copiar el backup sobre la base de datos actual | ||
await backupFile.copy(currentDb.path); | ||
} | ||
|
||
@override | ||
Future<void> deleteBackup(String backupPath) async { | ||
await _backupService.deleteBackup(backupPath); | ||
Future<void> deleteBackup(File backupFile) async { | ||
if (await backupFile.exists()) { | ||
await backupFile.delete(); | ||
} | ||
} | ||
} |
Oops, something went wrong.