40 lines
963 B
Dart
40 lines
963 B
Dart
import "package:hive_flutter/hive_flutter.dart";
|
|
import "package:quotegen_client/models/gallery_item.dart";
|
|
|
|
class GalleryDB {
|
|
|
|
static const String _boxName = "gallery";
|
|
|
|
static LazyBox<String> get _box => Hive.lazyBox<String>(_boxName);
|
|
|
|
|
|
static Future<void> saveItem(GalleryItem item) async {
|
|
await _box.put(item.id, item.toJsonString());
|
|
}
|
|
|
|
static Future<void> deleteItem(String id) async {
|
|
await _box.delete(id);
|
|
}
|
|
|
|
static Future<List<GalleryItem>> getAllItems() async {
|
|
final keys = _box.keys.toList();
|
|
final items = <GalleryItem>[];
|
|
|
|
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;
|
|
}
|
|
|
|
}
|