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 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 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)); } }