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 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 = []; final index = archive.findFile('index.json')!; List> 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(); } }