From 1716cf45d0e4e41c7833105dab10542f5ed5dcf1 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 8 Feb 2026 10:10:24 -0500 Subject: Initial commit --- lib/index_file.dart | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 lib/index_file.dart (limited to 'lib/index_file.dart') 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 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(); + } +} -- cgit v1.2.3