mirror of
https://github.com/dancojocaru2000/logic-circuits-simulator.git
synced 2025-02-22 00:59:35 +02:00
Implemented project export
This commit is contained in:
parent
dd62a26004
commit
bd10853866
8 changed files with 260 additions and 10 deletions
|
@ -1,3 +1,7 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logic_circuits_simulator/dialogs/new_project.dart';
|
||||
|
@ -5,7 +9,10 @@ import 'package:logic_circuits_simulator/models/projects.dart';
|
|||
import 'package:logic_circuits_simulator/pages/project.dart';
|
||||
import 'package:logic_circuits_simulator/state/project.dart';
|
||||
import 'package:logic_circuits_simulator/state/projects.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
class ProjectsPage extends StatelessWidget {
|
||||
const ProjectsPage({Key? key}) : super(key: key);
|
||||
|
@ -58,6 +65,61 @@ class ProjectsPage extends StatelessWidget {
|
|||
Navigator.of(context).pushNamed(ProjectPage.routeName);
|
||||
}
|
||||
|
||||
bool get canExport => Platform.isWindows || Platform.isMacOS || Platform.isLinux;
|
||||
void onProjectExport(BuildContext context, ProjectEntry p) async {
|
||||
final projectsState = Provider.of<ProjectsState>(context, listen: false);
|
||||
final msg = ScaffoldMessenger.of(context);
|
||||
|
||||
final outputFile = await FilePicker.platform.saveFile(
|
||||
dialogTitle: 'Export ${p.projectName}',
|
||||
fileName: '${p.projectId}.lcsproj',
|
||||
allowedExtensions: ['lcsproj'],
|
||||
lockParentWindow: true,
|
||||
type: FileType.custom,
|
||||
);
|
||||
|
||||
if (outputFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final enc = ZipEncoder();
|
||||
await projectsState.archiveProject(
|
||||
p,
|
||||
(archive) async {
|
||||
enc.encode(archive, output: OutputFileStream(outputFile));
|
||||
},
|
||||
);
|
||||
|
||||
msg.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Project ${p.projectName} exported'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool get canShare => !(Platform.isWindows || Platform.isLinux);
|
||||
void onProjectShare(BuildContext context, ProjectEntry p) async {
|
||||
final projectsState = Provider.of<ProjectsState>(context, listen: false);
|
||||
|
||||
final tmpDir = await getTemporaryDirectory();
|
||||
final archiveFile = File(path.join(tmpDir.path, '${p.projectId}.lcsproj'));
|
||||
|
||||
final enc = ZipEncoder();
|
||||
await projectsState.archiveProject(
|
||||
p,
|
||||
(archive) async {
|
||||
enc.encode(archive, output: OutputFileStream(archiveFile.path));
|
||||
},
|
||||
);
|
||||
|
||||
await Share.shareFiles(
|
||||
[archiveFile.path],
|
||||
mimeTypes: ['application/zip'],
|
||||
);
|
||||
|
||||
await archiveFile.delete();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final projects = Provider.of<ProjectsState>(context).projects;
|
||||
|
@ -77,10 +139,8 @@ class ProjectsPage extends StatelessWidget {
|
|||
child: ProjectTile(
|
||||
p,
|
||||
onProjectDelete: () => onProjectDelete(context, p),
|
||||
onProjectExport: () {
|
||||
// TODO: Implement project export
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Export coming soon...')));
|
||||
},
|
||||
onProjectExport: canExport ? () => onProjectExport(context, p) : null,
|
||||
onProjectShare: canShare ? () => onProjectShare(context, p) : null,
|
||||
onProjectSelect: () => onProjectSelect(context, p),
|
||||
),
|
||||
)).toList(growable: false),
|
||||
|
@ -126,10 +186,11 @@ class ProjectTile extends StatelessWidget {
|
|||
final ProjectEntry project;
|
||||
final void Function() onProjectSelect;
|
||||
final void Function() onProjectDelete;
|
||||
final void Function() onProjectExport;
|
||||
final void Function()? onProjectExport;
|
||||
final void Function()? onProjectShare;
|
||||
|
||||
const ProjectTile(this.project,
|
||||
{Key? key, required this.onProjectSelect, required this.onProjectDelete, required this.onProjectExport})
|
||||
{Key? key, required this.onProjectSelect, required this.onProjectDelete, required this.onProjectExport, required this.onProjectShare})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
|
@ -158,7 +219,10 @@ class ProjectTile extends StatelessWidget {
|
|||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(2.0),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 2.0,
|
||||
horizontal: 8.0,
|
||||
),
|
||||
child: Text(
|
||||
DateFormat.yMMMd().add_jms().format(project.lastUpdate.toLocal()),
|
||||
style: Theme.of(context).textTheme.caption,
|
||||
|
@ -173,10 +237,14 @@ class ProjectTile extends StatelessWidget {
|
|||
child: PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.more_horiz),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem(
|
||||
if (onProjectExport != null) const PopupMenuItem(
|
||||
value: 'export',
|
||||
child: Text('Export'),
|
||||
),
|
||||
if (onProjectShare != null) const PopupMenuItem(
|
||||
value: 'share',
|
||||
child: Text('Share'),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'delete',
|
||||
child: Text('Delete'),
|
||||
|
@ -188,7 +256,10 @@ class ProjectTile extends StatelessWidget {
|
|||
onProjectDelete();
|
||||
break;
|
||||
case 'export':
|
||||
onProjectExport();
|
||||
onProjectExport?.call();
|
||||
break;
|
||||
case 'share':
|
||||
onProjectShare?.call();
|
||||
break;
|
||||
default:
|
||||
throw Exception('Unexpected option: $selectedOption');
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logic_circuits_simulator/models/projects.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
@ -77,4 +78,51 @@ class ProjectsState extends ChangeNotifier {
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
Future<T> archiveProject<T>(ProjectEntry project, Future<T> Function(Archive archive) callback) async {
|
||||
final projectsDir = await _getProjectsDir();
|
||||
|
||||
// Create dir where export is prepared
|
||||
final exportDir = Directory(path.join(projectsDir.path, '.export'));
|
||||
await exportDir.create();
|
||||
|
||||
// Write index.json with only that project
|
||||
final exportIndex = index.copyWith(
|
||||
projects: index.projects.where((p) => p.projectId == project.projectId).toList(growable: false),
|
||||
);
|
||||
final exportIndexFile = File(path.join(exportDir.path, 'index.json'));
|
||||
await exportIndexFile.writeAsString(jsonEncode(exportIndex));
|
||||
|
||||
final exportProjectIdFile = File(path.join(exportDir.path, 'projectId.txt'));
|
||||
await exportProjectIdFile.writeAsString(project.projectId);
|
||||
|
||||
// Copy project folder
|
||||
final projectDir = Directory(path.join(projectsDir.path, project.projectId));
|
||||
final exportProjectDir = Directory(path.join(exportDir.path, project.projectId));
|
||||
await exportProjectDir.create();
|
||||
await for (final entry in projectDir.list(recursive: true, followLinks: false)) {
|
||||
final filename = path.relative(entry.path, from: projectDir.path);
|
||||
if (entry is Directory) {
|
||||
final newDir = Directory(path.join(exportProjectDir.path, filename));
|
||||
await newDir.create(recursive: true);
|
||||
}
|
||||
else if (entry is File) {
|
||||
await entry.copy(path.join(exportProjectDir.path, filename));
|
||||
}
|
||||
else if (entry is Link) {
|
||||
final newLink = Link(path.join(exportProjectDir.path, filename));
|
||||
await newLink.create(await entry.target());
|
||||
}
|
||||
}
|
||||
|
||||
// Create archive
|
||||
final archive = createArchiveFromDirectory(exportDir, includeDirName: false);
|
||||
|
||||
final result = await callback(archive);
|
||||
|
||||
// Remove preparation dir
|
||||
await exportDir.delete(recursive: true);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
|
||||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <url_launcher_linux/url_launcher_plugin.h>
|
||||
|
||||
void fl_register_plugins(FlPluginRegistry* registry) {
|
||||
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
|
||||
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#
|
||||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
url_launcher_linux
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
121
pubspec.lock
121
pubspec.lock
|
@ -15,6 +15,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.0.0"
|
||||
archive:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: archive
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.3.0"
|
||||
args:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -183,6 +190,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "6.1.2"
|
||||
file_picker:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: file_picker
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.6.1"
|
||||
fixnum:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -209,6 +223,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
flutter_plugin_android_lifecycle:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: flutter_plugin_android_lifecycle
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.6"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
|
@ -464,6 +485,48 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0"
|
||||
share_plus:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: share_plus
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.0.8"
|
||||
share_plus_linux:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: share_plus_linux
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.0"
|
||||
share_plus_macos:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: share_plus_macos
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
share_plus_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: share_plus_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.3"
|
||||
share_plus_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: share_plus_web
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
share_plus_windows:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: share_plus_windows
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
shared_preferences:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
@ -616,6 +679,62 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0"
|
||||
url_launcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "6.1.3"
|
||||
url_launcher_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_android
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "6.0.17"
|
||||
url_launcher_ios:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_ios
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "6.0.17"
|
||||
url_launcher_linux:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_linux
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
url_launcher_macos:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_macos
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
url_launcher_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0"
|
||||
url_launcher_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_web
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.12"
|
||||
url_launcher_windows:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_windows
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
uuid:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
@ -667,4 +786,4 @@ packages:
|
|||
version: "3.1.0"
|
||||
sdks:
|
||||
dart: ">=2.17.0-266.1.beta <3.0.0"
|
||||
flutter: ">=2.8.1"
|
||||
flutter: ">=2.10.0"
|
||||
|
|
|
@ -35,6 +35,9 @@ dependencies:
|
|||
flutter_hooks: ^0.18.3
|
||||
uuid: ^3.0.6
|
||||
collection: ^1.16.0
|
||||
archive: ^3.3.0
|
||||
file_picker: ^4.6.1
|
||||
share_plus: ^4.0.8
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
|
||||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <url_launcher_windows/url_launcher_windows.h>
|
||||
|
||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
UrlLauncherWindowsRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#
|
||||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
url_launcher_windows
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
Loading…
Add table
Reference in a new issue