Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/controllers/cacher/cache_controller.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'dart:convert';

import 'package:anymex/controllers/service_handler/service_handler.dart';
import 'package:anymex/database/kv_helper.dart';
import 'package:anymex/models/Media/media.dart';
import 'package:anymex/utils/logger.dart';
import 'package:anymex_extension_runtime_bridge/anymex_extension_runtime_bridge.dart';
import 'package:get/get.dart';

final cacheController = Get.find<CacheController>();

const _kCacheStorageKey = '__recently_opened_cache__';

class CacheController extends GetxController {
RxList<String> cachedAnilistData = <String>[].obs;
RxList<String> cachedMalData = <String>[].obs;
Expand All @@ -18,6 +21,67 @@ class CacheController extends GetxController {

RxList<String> get currentPool => getCacheContainer();

@override
void onInit() {
super.onInit();
_loadFromStorage();
}

void saveToStorage() {
try {
final service = Get.find<ServiceHandler>().serviceType.value;
final String serviceKey = service.name;
final List<String> pool = getCacheContainer().toList();

KvHelper.set<List<String>>('${_kCacheStorageKey}_$serviceKey', pool);
Logger.i('Saved recently opened cache for ${service.name}: ${pool.length} items');
} catch (e) {
Logger.i('Error saving cache to storage: $e');
}
}

void loadFromStorage() {
_loadFromStorage();
}

void _loadFromStorage() {
try {
final service = Get.find<ServiceHandler>().serviceType.value;
final String serviceKey = service.name;
final List<String>? saved = KvHelper.get<List<String>>(
'${_kCacheStorageKey}_$serviceKey',
defaultVal: [],
);

if (saved != null && saved.isNotEmpty) {
final targetPool = getCacheContainer();
targetPool.assignAll(saved);
Logger.i('Loaded recently opened cache for ${service.name}: ${saved.length} items');
} else {
final targetPool = getCacheContainer();
targetPool.clear();
}
} catch (e) {
Logger.i('Error loading cache from storage: $e');
}
}

void clearAllCache() {
try {
final services = ['anilist', 'mal', 'simkl', 'extensions'];
for (final service in services) {
KvHelper.remove('${_kCacheStorageKey}_$service');
}
cachedAnilistData.clear();
cachedMalData.clear();
cachedSimklData.clear();
cachedExtensionData.clear();
Logger.i('Cleared all recently opened cache');
} catch (e) {
Logger.i('Error clearing cache: $e');
}
}

void addCache(Map<String, dynamic> data) {
if (!data.containsKey('id') ||
data['id'] == null ||
Expand Down Expand Up @@ -52,6 +116,20 @@ class CacheController extends GetxController {
currentPool.add(encodedData);
}
}

_persistCurrentPool();
}

void _persistCurrentPool() {
try {
final service = Get.find<ServiceHandler>().serviceType.value;
final String serviceKey = service.name;
final pool = currentPool.length > 30
? currentPool.sublist(currentPool.length - 30)
: currentPool.toList();
KvHelper.set<List<String>>('${_kCacheStorageKey}_$serviceKey', pool);
} catch (e) {
}
}

List<Media> getStoredAnime() {
Expand Down
Loading