Skip to content

Commit

Permalink
merge import
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 9, 2024
1 parent 97283a7 commit efdc9b0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lib/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class DatabaseService {
where: 'id = ?', whereArgs: [recipe.id]);
}

Future<String> generateBackup({bool isEncrypted = false}) async {
Future<String> export({bool isEncrypted = false}) async {
print('GENERATE BACKUP');

var dbs = await DatabaseService.initializeDb();
Expand Down Expand Up @@ -146,7 +146,8 @@ class DatabaseService {
}
}

Future<void> restoreBackup(String backup, {bool isEncrypted = false}) async {
Future<void> import(String backup, {bool isEncrypted = false}) async {
print("IIIIIIIMMMMPOOORT");
var dbs = await DatabaseService.initializeDb();

Batch batch = dbs.batch();
Expand All @@ -158,12 +159,16 @@ class DatabaseService {
List json = convert
.jsonDecode(isEncrypted ? encrypter.decrypt64(backup, iv: iv) : backup);

for (var table in tables) {
batch.execute("DELETE FROM $table");
}
List<Recipe> actualRecipes = await DatabaseService.getRecipes();

for (var i = 0; i < json[0].length; i++) {
for (var k = 0; k < json[1][i].length; k++) {
batch.insert(json[0][i], json[1][i][k]);
if (actualRecipes
.where((recipe) => recipe.title == json[1][i][k]["title"])
.isEmpty) {
json[1][i][k]["id"] = null;
batch.insert(json[0][i], json[1][i][k]);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class _HomePageState extends State<HomePage> {
File file = File(filePath);

DatabaseService db = DatabaseService();
db.generateBackup().then((String result) {
db.export().then((String result) {
file.writeAsString(result);
Share.shareXFiles([XFile(filePath)]);
});
Expand All @@ -139,7 +139,7 @@ class _HomePageState extends State<HomePage> {
if (file != null) {
String backupContent = await file.readAsString();
DatabaseService db = DatabaseService();
db.restoreBackup(backupContent);
db.import(backupContent);
loadRecipes();
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/widgets/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class _SettingsState extends State<Settings> {
children: [
ListTile(
title: const Text("Import recipes"),
onTap: () => widget.restore()),
onTap: () {
widget.restore().then(() => Navigator.pop(context));
}),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.0
version: 1.2.0

environment:
sdk: '>=3.3.1 <4.0.0'
Expand Down

0 comments on commit efdc9b0

Please sign in to comment.