part of fever_api; /// Caches items, feeds and groups class APICache { final FeverAPI api; late final ItemCache items; late final ObjectCache feeds; late final ObjectCache groups; late final ObjectCache favicons; final List feedsGroups = []; APICache(this.api) { items = ItemCache(api); feeds = ObjectCache(api); groups = ObjectCache(api); favicons = ObjectCache(api); } void clear() { items._items.clear(); feeds._items.clear(); groups._items.clear(); favicons._items.clear(); feedsGroups.clear(); } } class ObjectCache { final FeverAPI api; final Map _items = {}; ObjectCache(this.api); /// Returns an item from the cache, or null if it doesn't exist T? get(K key) { return _items.containsKey(key) ? _items[key] : null; } /// Returns a copy of the cache content Map getAll() { return {..._items}; } void set(K key, T value) { _items[key] = value; } int get size => _items.length; } class ItemCache extends ObjectCache { /// Get all items for a given feed List getForFeed(int feed) { if (api.cache.feeds.get(feed) == null) throw Exception('Feed $feed doesn\'t exist'); return _items.values.where((item) => item.feedId == feed).toList(); } ItemCache(FeverAPI api) : super(api); }