|
|
|
@ -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'); |
|
|
|
|