diff options
| author | Mica White <botahamec@outlook.com> | 2026-02-08 10:10:24 -0500 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-02-08 10:10:24 -0500 |
| commit | 1716cf45d0e4e41c7833105dab10542f5ed5dcf1 (patch) | |
| tree | 99ae387cfb3b96c30bbe3fd4cd619dc291505f68 /lib/index_file.dart | |
Diffstat (limited to 'lib/index_file.dart')
| -rw-r--r-- | lib/index_file.dart | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/index_file.dart b/lib/index_file.dart new file mode 100644 index 0000000..9b434ed --- /dev/null +++ b/lib/index_file.dart @@ -0,0 +1,119 @@ +import 'dart:convert';
+
+import 'package:archive/archive.dart';
+import 'package:flutter/material.dart';
+import 'package:image/image.dart';
+
+enum ProjectType { video, image, sound }
+
+extension PTExt on ProjectType {
+ IconData icon() {
+ switch (this) {
+ case ProjectType.video:
+ return Icons.video_file;
+ case ProjectType.image:
+ return Icons.image;
+ case ProjectType.sound:
+ return Icons.audio_file;
+ }
+ }
+
+ String normalString() {
+ switch (this) {
+ case ProjectType.video:
+ return 'Video';
+ case ProjectType.image:
+ return 'Image';
+ case ProjectType.sound:
+ return 'Sound';
+ }
+ }
+
+ static ProjectType parse(String type) {
+ switch (type) {
+ case 'Video':
+ return ProjectType.video;
+ case 'Image':
+ return ProjectType.image;
+ case 'Sound':
+ return ProjectType.sound;
+ default:
+ throw ArgumentError.value(type);
+ }
+ }
+}
+
+class ProjectMetadata {
+ final String title;
+ final ProjectType type;
+ final DateTime lastModified;
+ final String? location;
+
+ const ProjectMetadata({
+ this.title = 'My New Project',
+ required this.type,
+ required this.lastModified,
+ this.location,
+ });
+}
+
+class ProjectIndex {
+ final Archive archive;
+ final List<ProjectMetadata> projects;
+
+ const ProjectIndex({
+ required this.archive,
+ required this.projects,
+ });
+
+ factory ProjectIndex.blank() {
+ final archive = Archive();
+ final indexFile = ArchiveFile.string('index.json', '[]');
+ archive.addFile(indexFile);
+
+ return ProjectIndex(archive: archive, projects: [
+ ProjectMetadata(
+ title: 'My first project',
+ type: ProjectType.video,
+ lastModified: DateTime.utc(2025),
+ ),
+ ProjectMetadata(
+ title: 'Project #2',
+ type: ProjectType.image,
+ lastModified: DateTime.utc(2024, DateTime.december),
+ ),
+ ]);
+ }
+
+ factory ProjectIndex.load(String path) {
+ final file = InputFileStream(path);
+ final archive = ZipDecoder().decodeStream(file);
+
+ var projects = <ProjectMetadata>[];
+ final index = archive.findFile('index.json')!;
+ List<Map<String, String>> json = jsonDecode(index.getContent().toString());
+ for (final entry in json) {
+ projects.add(ProjectMetadata(
+ title: entry['title']!,
+ type: PTExt.parse(entry['type']!),
+ location: entry['location'],
+ lastModified: DateTime.parse(entry['lastModified']!),
+ ));
+ }
+ projects.sort((a, b) => b.lastModified.compareTo(a.lastModified));
+
+ return ProjectIndex(
+ archive: archive,
+ projects: projects,
+ );
+ }
+
+ getImage(String title) {
+ final file = archive.find("$title.jpg");
+ if (file == null) {
+ return null;
+ }
+
+ return JpegDecoder().decode(file.content)?.getBytes();
+ }
+}
|
