yumi-flutter/lib/modules/room/background/room_background_history.dart
2026-05-07 10:06:02 +08:00

49 lines
1.3 KiB
Dart

import 'dart:convert';
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
class RoomBackgroundHistory {
static const int _maxCount = 30;
static const String _prefix = "room_background_history";
static String get _key {
final userId =
AccountStorage().getCurrentUser()?.userProfile?.id ?? "guest";
return "${_prefix}_$userId";
}
static List<String> load() {
final raw = DataPersistence.getString(_key);
if (raw.trim().isEmpty) {
return [];
}
try {
final decoded = jsonDecode(raw);
if (decoded is List) {
return decoded
.map((item) => item?.toString().trim() ?? "")
.where((item) => item.isNotEmpty)
.toSet()
.toList();
}
} catch (_) {}
return [];
}
static Future<void> add(String imageUrl) async {
final normalized = imageUrl.trim();
if (normalized.isEmpty) {
return;
}
final next =
load()
..removeWhere((item) => item == normalized)
..insert(0, normalized);
if (next.length > _maxCount) {
next.removeRange(_maxCount, next.length);
}
await DataPersistence.setString(_key, jsonEncode(next));
}
}