|
|
@ -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:flutter/material.dart'; |
|
|
|
import 'package:intl/intl.dart'; |
|
|
|
import 'package:intl/intl.dart'; |
|
|
|
import 'package:logic_circuits_simulator/dialogs/new_project.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/pages/project.dart'; |
|
|
|
import 'package:logic_circuits_simulator/state/project.dart'; |
|
|
|
import 'package:logic_circuits_simulator/state/project.dart'; |
|
|
|
import 'package:logic_circuits_simulator/state/projects.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:provider/provider.dart'; |
|
|
|
|
|
|
|
import 'package:share_plus/share_plus.dart'; |
|
|
|
|
|
|
|
|
|
|
|
class ProjectsPage extends StatelessWidget { |
|
|
|
class ProjectsPage extends StatelessWidget { |
|
|
|
const ProjectsPage({Key? key}) : super(key: key); |
|
|
|
const ProjectsPage({Key? key}) : super(key: key); |
|
|
@ -58,6 +65,61 @@ class ProjectsPage extends StatelessWidget { |
|
|
|
Navigator.of(context).pushNamed(ProjectPage.routeName); |
|
|
|
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 |
|
|
|
@override |
|
|
|
Widget build(BuildContext context) { |
|
|
|
Widget build(BuildContext context) { |
|
|
|
final projects = Provider.of<ProjectsState>(context).projects; |
|
|
|
final projects = Provider.of<ProjectsState>(context).projects; |
|
|
@ -77,10 +139,8 @@ class ProjectsPage extends StatelessWidget { |
|
|
|
child: ProjectTile( |
|
|
|
child: ProjectTile( |
|
|
|
p, |
|
|
|
p, |
|
|
|
onProjectDelete: () => onProjectDelete(context, p), |
|
|
|
onProjectDelete: () => onProjectDelete(context, p), |
|
|
|
onProjectExport: () { |
|
|
|
onProjectExport: canExport ? () => onProjectExport(context, p) : null, |
|
|
|
// TODO: Implement project export |
|
|
|
onProjectShare: canShare ? () => onProjectShare(context, p) : null, |
|
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Export coming soon...'))); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
onProjectSelect: () => onProjectSelect(context, p), |
|
|
|
onProjectSelect: () => onProjectSelect(context, p), |
|
|
|
), |
|
|
|
), |
|
|
|
)).toList(growable: false), |
|
|
|
)).toList(growable: false), |
|
|
@ -126,10 +186,11 @@ class ProjectTile extends StatelessWidget { |
|
|
|
final ProjectEntry project; |
|
|
|
final ProjectEntry project; |
|
|
|
final void Function() onProjectSelect; |
|
|
|
final void Function() onProjectSelect; |
|
|
|
final void Function() onProjectDelete; |
|
|
|
final void Function() onProjectDelete; |
|
|
|
final void Function() onProjectExport; |
|
|
|
final void Function()? onProjectExport; |
|
|
|
|
|
|
|
final void Function()? onProjectShare; |
|
|
|
|
|
|
|
|
|
|
|
const ProjectTile(this.project, |
|
|
|
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); |
|
|
|
: super(key: key); |
|
|
|
|
|
|
|
|
|
|
|
@override |
|
|
|
@override |
|
|
@ -158,7 +219,10 @@ class ProjectTile extends StatelessWidget { |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
Padding( |
|
|
|
Padding( |
|
|
|
padding: const EdgeInsets.all(2.0), |
|
|
|
padding: const EdgeInsets.symmetric( |
|
|
|
|
|
|
|
vertical: 2.0, |
|
|
|
|
|
|
|
horizontal: 8.0, |
|
|
|
|
|
|
|
), |
|
|
|
child: Text( |
|
|
|
child: Text( |
|
|
|
DateFormat.yMMMd().add_jms().format(project.lastUpdate.toLocal()), |
|
|
|
DateFormat.yMMMd().add_jms().format(project.lastUpdate.toLocal()), |
|
|
|
style: Theme.of(context).textTheme.caption, |
|
|
|
style: Theme.of(context).textTheme.caption, |
|
|
@ -173,10 +237,14 @@ class ProjectTile extends StatelessWidget { |
|
|
|
child: PopupMenuButton<String>( |
|
|
|
child: PopupMenuButton<String>( |
|
|
|
icon: const Icon(Icons.more_horiz), |
|
|
|
icon: const Icon(Icons.more_horiz), |
|
|
|
itemBuilder: (context) => [ |
|
|
|
itemBuilder: (context) => [ |
|
|
|
const PopupMenuItem( |
|
|
|
if (onProjectExport != null) const PopupMenuItem( |
|
|
|
value: 'export', |
|
|
|
value: 'export', |
|
|
|
child: Text('Export'), |
|
|
|
child: Text('Export'), |
|
|
|
), |
|
|
|
), |
|
|
|
|
|
|
|
if (onProjectShare != null) const PopupMenuItem( |
|
|
|
|
|
|
|
value: 'share', |
|
|
|
|
|
|
|
child: Text('Share'), |
|
|
|
|
|
|
|
), |
|
|
|
const PopupMenuItem( |
|
|
|
const PopupMenuItem( |
|
|
|
value: 'delete', |
|
|
|
value: 'delete', |
|
|
|
child: Text('Delete'), |
|
|
|
child: Text('Delete'), |
|
|
@ -188,7 +256,10 @@ class ProjectTile extends StatelessWidget { |
|
|
|
onProjectDelete(); |
|
|
|
onProjectDelete(); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'export': |
|
|
|
case 'export': |
|
|
|
onProjectExport(); |
|
|
|
onProjectExport?.call(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'share': |
|
|
|
|
|
|
|
onProjectShare?.call(); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
throw Exception('Unexpected option: $selectedOption'); |
|
|
|
throw Exception('Unexpected option: $selectedOption'); |
|
|
|