import "package:hive_flutter/hive_flutter.dart"; import "package:quotegen_client/models/gallery_item.dart"; class GalleryDB { static const String _boxName = "gallery"; static LazyBox get _box => Hive.lazyBox(_boxName); static Future saveItem(GalleryItem item) async { await _box.put(item.id, item.toJsonString()); } static Future deleteItem(String id) async { await _box.delete(id); } static Future> getAllItems() async { final keys = _box.keys.toList(); final items = []; for (final key in keys) { final raw = await _box.get(key); if (raw != null) { try { items.add(GalleryItem.fromJsonString(raw)); } catch (e) { // skip corrupted entries print("GalleryDB: failed to parse item $key: $e"); } } } items.sort((a, b) => b.createdAt.compareTo(a.createdAt)); return items; } }