feat(project): persist catalog snapshot in project data

This commit is contained in:
gary
2026-02-24 21:43:25 +01:00
parent 25d570c779
commit fd72d53d2a
2 changed files with 57 additions and 3 deletions

View File

@@ -1,6 +1,28 @@
import 'dart:convert';
import 'dart:typed_data';
class MosaicPaletteSnapshotEntry {
final String name;
final int colorValue;
const MosaicPaletteSnapshotEntry({
required this.name,
required this.colorValue,
});
Map<String, dynamic> toJson() => {
'name': name,
'colorValue': colorValue,
};
factory MosaicPaletteSnapshotEntry.fromJson(Map<String, dynamic> json) {
return MosaicPaletteSnapshotEntry(
name: json['name'] as String? ?? 'Unbenannt',
colorValue: (json['colorValue'] as num?)?.toInt() ?? 0xFF000000,
);
}
}
class MosaicProjectData {
final bool useCapSize;
final String gridWidth;
@@ -12,6 +34,7 @@ class MosaicProjectData {
final double colorVariation;
final String selectedPreset;
final Uint8List? sourceImageBytes;
final List<MosaicPaletteSnapshotEntry> catalogSnapshot;
final DateTime savedAt;
const MosaicProjectData({
@@ -25,6 +48,7 @@ class MosaicProjectData {
required this.colorVariation,
required this.selectedPreset,
required this.sourceImageBytes,
required this.catalogSnapshot,
required this.savedAt,
});
@@ -40,23 +64,32 @@ class MosaicProjectData {
'selectedPreset': selectedPreset,
'sourceImageBase64':
sourceImageBytes == null ? null : base64Encode(sourceImageBytes!),
'catalogSnapshot': catalogSnapshot.map((entry) => entry.toJson()).toList(),
'savedAt': savedAt.toIso8601String(),
};
factory MosaicProjectData.fromJson(Map<String, dynamic> json) {
final sourceB64 = json['sourceImageBase64'] as String?;
final snapshotRaw = (json['catalogSnapshot'] as List?) ?? const [];
return MosaicProjectData(
useCapSize: json['useCapSize'] as bool? ?? false,
gridWidth: json['gridWidth'] as String? ?? '40',
gridHeight: json['gridHeight'] as String? ?? '30',
capSize: json['capSize'] as String? ?? '12',
fidelityStructure: (json['fidelityStructure'] as num?)?.toDouble() ?? 0.5,
ditheringStrength: (json['ditheringStrength'] as num?)?.toDouble() ?? 0.35,
ditheringStrength:
(json['ditheringStrength'] as num?)?.toDouble() ?? 0.35,
edgeEmphasis: (json['edgeEmphasis'] as num?)?.toDouble() ?? 0.4,
colorVariation: (json['colorVariation'] as num?)?.toDouble() ?? 0.3,
selectedPreset: json['selectedPreset'] as String? ?? 'ausgewogen',
sourceImageBytes: sourceB64 == null ? null : base64Decode(sourceB64),
savedAt: DateTime.tryParse(json['savedAt'] as String? ?? '') ?? DateTime.now(),
catalogSnapshot: snapshotRaw
.whereType<Map>()
.map((entry) => MosaicPaletteSnapshotEntry.fromJson(
Map<String, dynamic>.from(entry)))
.toList(growable: false),
savedAt:
DateTime.tryParse(json['savedAt'] as String? ?? '') ?? DateTime.now(),
);
}
}