From efdc9b052e24f6b5fcba51ac26e67fa880f6f645 Mon Sep 17 00:00:00 2001 From: JdM Date: Tue, 9 Apr 2024 12:14:28 +0200 Subject: [PATCH] merge import --- lib/database.dart | 17 +++++++++++------ lib/widgets/homePage.dart | 4 ++-- lib/widgets/settings.dart | 4 +++- pubspec.yaml | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/database.dart b/lib/database.dart index 6b09c1a..7c64ab1 100644 --- a/lib/database.dart +++ b/lib/database.dart @@ -115,7 +115,7 @@ class DatabaseService { where: 'id = ?', whereArgs: [recipe.id]); } - Future generateBackup({bool isEncrypted = false}) async { + Future export({bool isEncrypted = false}) async { print('GENERATE BACKUP'); var dbs = await DatabaseService.initializeDb(); @@ -146,7 +146,8 @@ class DatabaseService { } } - Future restoreBackup(String backup, {bool isEncrypted = false}) async { + Future import(String backup, {bool isEncrypted = false}) async { + print("IIIIIIIMMMMPOOORT"); var dbs = await DatabaseService.initializeDb(); Batch batch = dbs.batch(); @@ -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 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]); + } } } diff --git a/lib/widgets/homePage.dart b/lib/widgets/homePage.dart index 19c28bd..1d13812 100644 --- a/lib/widgets/homePage.dart +++ b/lib/widgets/homePage.dart @@ -122,7 +122,7 @@ class _HomePageState extends State { File file = File(filePath); DatabaseService db = DatabaseService(); - db.generateBackup().then((String result) { + db.export().then((String result) { file.writeAsString(result); Share.shareXFiles([XFile(filePath)]); }); @@ -139,7 +139,7 @@ class _HomePageState extends State { if (file != null) { String backupContent = await file.readAsString(); DatabaseService db = DatabaseService(); - db.restoreBackup(backupContent); + db.import(backupContent); loadRecipes(); } } diff --git a/lib/widgets/settings.dart b/lib/widgets/settings.dart index 34a879c..185b64f 100644 --- a/lib/widgets/settings.dart +++ b/lib/widgets/settings.dart @@ -48,7 +48,9 @@ class _SettingsState extends State { children: [ ListTile( title: const Text("Import recipes"), - onTap: () => widget.restore()), + onTap: () { + widget.restore().then(() => Navigator.pop(context)); + }), ], ), ), diff --git a/pubspec.yaml b/pubspec.yaml index 4c8c034..be2a2d1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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'