From cf7fd025990134ac00fa19a8aac2cae4e56a165e Mon Sep 17 00:00:00 2001 From: Fransolet Thomas Date: Fri, 25 Mar 2022 17:42:09 +0100 Subject: [PATCH] Import / export config update --- lib/Components/image_input_container.dart | 11 +++- lib/Helpers/FileHelper.dart | 13 ++-- .../configuration_detail_screen.dart | 18 +++++- .../new_configuration_popup.dart | 8 ++- pubspec.lock | 63 ++++++++++++++----- pubspec.yaml | 2 +- 6 files changed, 86 insertions(+), 29 deletions(-) diff --git a/lib/Components/image_input_container.dart b/lib/Components/image_input_container.dart index c081571..f18a6ea 100644 --- a/lib/Components/image_input_container.dart +++ b/lib/Components/image_input_container.dart @@ -84,7 +84,8 @@ class _ImageInputContainerState extends State { future: getResource(resourceIdToShow, appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { - return Transform.scale( + if (snapshot.data != null) { + return Transform.scale( scale: isSmall ? size.aspectRatio * 0.5: size.aspectRatio * 0.9, child: AspectRatio( aspectRatio: 4/4, @@ -93,6 +94,10 @@ class _ImageInputContainerState extends State { ), ), ); + } else { + return Text("No data"); + } + } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { @@ -137,9 +142,9 @@ class _ImageInputContainerState extends State { borderRadius: BorderRadius.circular(30.0), image: new DecorationImage( fit: widget.imageFit, - image: new NetworkImage( + image: resourceDTO.type != null ? new NetworkImage( resourceDTO.type == ResourceType.image ? appContext.getContext().clientAPI.resourceApi.apiClient.basePath+"/api/Resource/"+ resourceDTO.id : resourceDTO.data, - ), + ) : null, ), boxShadow: [ BoxShadow( diff --git a/lib/Helpers/FileHelper.dart b/lib/Helpers/FileHelper.dart index f1cbf4b..645129a 100644 --- a/lib/Helpers/FileHelper.dart +++ b/lib/Helpers/FileHelper.dart @@ -1,7 +1,9 @@ import 'dart:convert'; +import 'dart:developer'; import 'dart:io'; import 'package:encrypt/encrypt.dart'; +import 'package:file_picker/file_picker.dart'; import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Models/session.dart'; import 'package:manager_app/client.dart'; @@ -47,13 +49,10 @@ class FileHelper { return file.writeAsString(jsonEncode(exportConfigurationDTO.toJson())); } - Future importConfiguration(String path, Client client, context) async { - // Gets the file - File file = File(path); - - final contents = await file.readAsString(); - - ExportConfigurationDTO export = ExportConfigurationDTO.fromJson(jsonDecode(contents)); + Future importConfiguration(FilePickerResult filePickerResult, String path, Client client, context) async { + var fileTest = filePickerResult.files[0]; + String fileContent = utf8.decode(fileTest.bytes); + ExportConfigurationDTO export = ExportConfigurationDTO.fromJson(jsonDecode(fileContent)); try { String test = await client.configurationApi.configurationImport(export); if (test.contains("successfully")) { diff --git a/lib/Screens/Configurations/configuration_detail_screen.dart b/lib/Screens/Configurations/configuration_detail_screen.dart index 397ac71..5d65710 100644 --- a/lib/Screens/Configurations/configuration_detail_screen.dart +++ b/lib/Screens/Configurations/configuration_detail_screen.dart @@ -1,6 +1,8 @@ +import 'dart:developer'; import 'dart:io'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/painting.dart'; import 'package:manager_app/Components/color_picker_input_container.dart'; @@ -19,6 +21,8 @@ import 'package:manager_app/constants.dart'; import 'package:managerapi/api.dart'; import 'package:provider/provider.dart'; import 'package:intl/intl.dart'; +import 'dart:html' as html; + class ConfigurationDetailScreen extends StatefulWidget { final String id; @@ -82,9 +86,19 @@ class _ConfigurationDetailScreenState extends State { // Export config Client clientAPI = appContext.getContext().clientAPI; ExportConfigurationDTO export = await clientAPI.configurationApi.configurationExport(configurationDTO.id); - File test = await FileHelper().storeConfiguration(export); - showNotification(Colors.green, kWhite, "L'export de la configuration a réussi, le document se trouve là : " + test.path, context, 3000); + + if (kIsWeb) { + html.AnchorElement anchorElement = new html.AnchorElement(); + var uri = (Uri.parse(appContext.getContext().clientAPI.resourceApi.apiClient.basePath+'/api/Configuration/${configurationDTO.id}/export')); + anchorElement.href = uri.toString(); + anchorElement.download = '${configurationDTO.label}.json'; + anchorElement.click(); + } else { + File test = await FileHelper().storeConfiguration(export); + showNotification(Colors.green, kWhite, "L'export de la configuration a réussi, le document se trouve là : " + test.path, context, 3000); + } } catch(e) { + log(e); showNotification(kPrimaryColor, kWhite, "L'export de la configuration a échoué", context, null); } }, diff --git a/lib/Screens/Configurations/new_configuration_popup.dart b/lib/Screens/Configurations/new_configuration_popup.dart index aa856de..50ea38f 100644 --- a/lib/Screens/Configurations/new_configuration_popup.dart +++ b/lib/Screens/Configurations/new_configuration_popup.dart @@ -1,4 +1,5 @@ //import 'package:filepicker_windows/filepicker_windows.dart'; +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:manager_app/Components/message_notification.dart'; import 'package:manager_app/Components/rounded_button.dart'; @@ -40,9 +41,12 @@ void showNewConfiguration(AppContext appContext, ValueChanged isImport, Bu ), InkWell( onTap: () async { - String result = filePicker(); + FilePickerResult result = await FilePicker.platform.pickFiles(); + + //String result = filePicker(); if (result != null) { - await FileHelper().importConfiguration(result, appContext.getContext().clientAPI, mainContext); + + await FileHelper().importConfiguration(result, null, appContext.getContext().clientAPI, mainContext); isImport(true); Navigator.of(context).pop(); } diff --git a/pubspec.lock b/pubspec.lock index f17232e..1acffeb 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,14 +7,14 @@ packages: name: args url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "2.3.0" asn1lib: dependency: transitive description: name: asn1lib url: "https://pub.dartlang.org" source: hosted - version: "1.0.2" + version: "1.1.0" async: dependency: transitive description: @@ -78,20 +78,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.1" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.1" cupertino_icons: dependency: "direct main" description: name: cupertino_icons url: "https://pub.dartlang.org" source: hosted - version: "1.0.3" + version: "1.0.4" drag_and_drop_lists: dependency: "direct main" description: name: drag_and_drop_lists url: "https://pub.dartlang.org" source: hosted - version: "0.3.2" + version: "0.3.2+2" encrypt: dependency: "direct main" description: @@ -119,7 +126,7 @@ packages: name: file_picker url: "https://pub.dartlang.org" source: hosted - version: "4.3.0" + version: "4.5.1" flare_flutter: dependency: "direct main" description: @@ -138,7 +145,7 @@ packages: name: flutter_colorpicker url: "https://pub.dartlang.org" source: hosted - version: "0.6.0" + version: "0.6.1" flutter_plugin_android_lifecycle: dependency: transitive description: @@ -156,6 +163,13 @@ packages: description: flutter source: sdk version: "0.0.0" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.0" http: dependency: transitive description: @@ -225,7 +239,7 @@ packages: name: multiselect_formfield url: "https://pub.dartlang.org" source: hosted - version: "0.1.6" + version: "0.1.7" nested: dependency: transitive description: @@ -260,14 +274,14 @@ packages: name: plugin_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.0.2" + version: "2.1.2" pointycastle: dependency: transitive description: name: pointycastle url: "https://pub.dartlang.org" source: hosted - version: "3.3.1" + version: "3.5.2" provider: dependency: "direct main" description: @@ -349,21 +363,42 @@ packages: name: video_player url: "https://pub.dartlang.org" source: hosted - version: "2.1.14" + version: "2.3.0" + video_player_android: + dependency: transitive + description: + name: video_player_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" + video_player_avfoundation: + dependency: transitive + description: + name: video_player_avfoundation + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" video_player_platform_interface: dependency: transitive description: name: video_player_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.1.0" + version: "5.1.1" video_player_web: dependency: transitive description: name: video_player_web url: "https://pub.dartlang.org" source: hosted - version: "2.0.3" + version: "2.0.7" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.4" sdks: - dart: ">=2.14.0 <3.0.0" - flutter: ">=2.0.0" + dart: ">=2.15.0 <3.0.0" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index d0fb16a..addb874 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,7 +33,7 @@ dependencies: convert: ^3.0.0 collection: any #filepicker_windows: ^2.0.0 - file_picker: ^4.3.0 + file_picker: ^4.5.1 flare_flutter: ^3.0.1 #dart_vlc: ^0.0.6 video_player: ^2.1.1