Skip to content

Commit

Permalink
recipes import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 9, 2024
1 parent 366e42e commit 9a13b38
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 38 deletions.
33 changes: 27 additions & 6 deletions lib/widgets/homePage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:reciper/widgets/extractRecipeButton.dart';
import 'package:reciper/widgets/settings.dart';
Expand All @@ -6,6 +7,8 @@ import 'newRecipeButton.dart';
import '../database.dart';
import '../models/recipe.dart';
import 'recipesListView.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

class HomePage extends StatefulWidget {
const HomePage({super.key});
Expand Down Expand Up @@ -60,7 +63,7 @@ class _HomePageState extends State<HomePage> {
drawer: Drawer(
child: Settings(
backup: backup,
restore: restore,
restore: import,
)),
body: SingleChildScrollView(
child: Column(
Expand Down Expand Up @@ -111,15 +114,33 @@ class _HomePageState extends State<HomePage> {
}

Future<void> backup() async {
Directory appDocumentsDirectory =
await getApplicationDocumentsDirectory(); // 1
String appDocumentsPath = appDocumentsDirectory.path;
String filePath = '$appDocumentsPath/Reciper_Export.json';

File file = File(filePath);

DatabaseService db = DatabaseService();
db.generateBackup().then((String result) {
Share.share(result);
file.writeAsString(result);
Share.shareXFiles([XFile(filePath)]);
});
}

Future<void> restore(String backup) async {
DatabaseService db = DatabaseService();
db.restoreBackup(backup);
loadRecipes();
Future<void> import() async {
const XTypeGroup typeGroup = XTypeGroup(
label: 'Reciper export',
extensions: <String>['json'],
);
final XFile? file =
await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);

if (file != null) {
String backupContent = await file.readAsString();
DatabaseService db = DatabaseService();
db.restoreBackup(backupContent);
loadRecipes();
}
}
}
33 changes: 2 additions & 31 deletions lib/widgets/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,8 @@ class _SettingsState extends State<Settings> {
child: Column(
children: [
ListTile(
title: const Text("Restore backup"),
onTap: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Import recipes"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: backupCodeController,
decoration: const InputDecoration(
hintText: "Past Backup Code Here"),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
widget.restore(backupCodeController.text);
Navigator.pop(context, 'ok');
},
child: const Text('OK'),
),
],
),
),
),
title: const Text("Import recipes"),
onTap: () => widget.restore()),
],
),
),
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

#include "generated_plugin_registrant.h"

#include <file_selector_linux/file_selector_plugin.h>
#include <sqlite3_flutter_libs/sqlite3_flutter_libs_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) sqlite3_flutter_libs_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "Sqlite3FlutterLibsPlugin");
sqlite3_flutter_libs_plugin_register_with_registrar(sqlite3_flutter_libs_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux
sqlite3_flutter_libs
url_launcher_linux
)
Expand Down
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.0"
file_selector:
dependency: "direct main"
description:
name: file_selector
sha256: "5019692b593455127794d5718304ff1ae15447dea286cdda9f0db2a796a1b828"
url: "https://pub.dev"
source: hosted
version: "1.0.3"
file_selector_android:
dependency: transitive
description:
name: file_selector_android
sha256: "1cd66575f063b689e041aec836905ba7be18d76c9f0634d0d75daec825f67095"
url: "https://pub.dev"
source: hosted
version: "0.5.0+7"
file_selector_ios:
dependency: transitive
description:
name: file_selector_ios
sha256: b015154e6d9fddbc4d08916794df170b44531798c8dd709a026df162d07ad81d
url: "https://pub.dev"
source: hosted
version: "0.5.1+8"
file_selector_linux:
dependency: transitive
description:
name: file_selector_linux
sha256: "045d372bf19b02aeb69cacf8b4009555fb5f6f0b7ad8016e5f46dd1387ddd492"
url: "https://pub.dev"
source: hosted
version: "0.9.2+1"
file_selector_macos:
dependency: transitive
description:
name: file_selector_macos
sha256: b15c3da8bd4908b9918111fa486903f5808e388b8d1c559949f584725a6594d6
url: "https://pub.dev"
source: hosted
version: "0.9.3+3"
file_selector_platform_interface:
dependency: transitive
description:
name: file_selector_platform_interface
sha256: a3994c26f10378a039faa11de174d7b78eb8f79e4dd0af2a451410c1a5c3f66b
url: "https://pub.dev"
source: hosted
version: "2.6.2"
file_selector_web:
dependency: transitive
description:
name: file_selector_web
sha256: "619e431b224711a3869e30dbd7d516f5f5a4f04b265013a50912f39e1abc88c8"
url: "https://pub.dev"
source: hosted
version: "0.9.4+1"
file_selector_windows:
dependency: transitive
description:
name: file_selector_windows
sha256: d3547240c20cabf205c7c7f01a50ecdbc413755814d6677f3cb366f04abcead0
url: "https://pub.dev"
source: hosted
version: "0.9.3+1"
fixnum:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 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.0.2
version: 1.1.0

environment:
sdk: '>=3.3.1 <4.0.0'
Expand Down Expand Up @@ -45,6 +45,7 @@ dependencies:
recipe_extractor: ^2.0.0
url_launcher: ^6.2.5
share_plus: ^8.0.3
file_selector: ^1.0.3


flutter_icons:
Expand Down

0 comments on commit 9a13b38

Please sign in to comment.