fix: trakt integration calling too many apis
Some checks are pending
Build and Deploy / build_windows (push) Waiting to run
Build and Deploy / build_android (push) Waiting to run
Build and Deploy / build_android_tv (push) Waiting to run
Build and Deploy / build_ipa (push) Waiting to run
Build and Deploy / build_linux (push) Waiting to run
Build and Deploy / build_macos (push) Waiting to run

This commit is contained in:
omkar 2025-01-11 18:18:33 +05:30
parent 91e0968cd8
commit fbea109161
3 changed files with 50 additions and 39 deletions

View file

@ -156,9 +156,11 @@ class TraktContainerState extends State<TraktContainer> {
}
Future<void> refresh() async {
_logger.info('Refreshing data');
_cachedItems = [];
await _loadData();
try {
_logger.info('Refreshing data for ${widget.loadId}');
_cachedItems = [];
await _loadData();
} catch (e) {}
}
String get title {
@ -234,7 +236,7 @@ class TraktContainerState extends State<TraktContainer> {
),
Stack(
children: [
if ((_cachedItems ?? []).isEmpty && !_isLoading)
if ((_cachedItems ?? []).isEmpty && !_isLoading && _error != null)
const Positioned.fill(
child: Center(
child: Text("Nothing to see here"),
@ -242,18 +244,46 @@ class TraktContainerState extends State<TraktContainer> {
),
if (_isLoading && (_cachedItems ?? []).isEmpty)
const SpinnerCards(),
SizedBox(
height: getListHeight(context),
child: RenderListItems(
isWide: widget.loadId == "up_next_series",
items: _cachedItems ?? [],
error: _error,
itemScrollController: _scrollController,
hasError: _error != null,
heroPrefix: "trakt_up_next${widget.loadId}",
service: TraktService.stremioService!,
if (_error != null) Text(_error!),
if (_error != null)
Positioned.fill(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Error: $_error",
style: theme.textTheme.bodyMedium?.copyWith(
color: Colors.red,
),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
setState(() {
_error = null;
});
_loadData();
},
child: const Text("Retry"),
),
],
),
),
),
if (_error == null)
SizedBox(
height: getListHeight(context),
child: RenderListItems(
isWide: widget.loadId == "up_next_series",
items: _cachedItems ?? [],
error: _error,
itemScrollController: _scrollController,
hasError: _error != null,
heroPrefix: "trakt_up_next${widget.loadId}",
service: TraktService.stremioService!,
),
),
),
],
)
],

View file

@ -24,8 +24,6 @@ class TraktService {
final refetchKey = BehaviorSubject<List<String>>();
static const Duration _cacheRevalidationInterval = Duration(hours: 1);
static TraktService? _instance;
static TraktService? get instance => _instance;
static BaseConnectionService? stremioService;
@ -43,8 +41,6 @@ class TraktService {
);
}
Timer? _cacheRevalidationTimer;
clearCache() {
_logger.info('Clearing cache');
_cache.clear();
@ -132,25 +128,6 @@ class TraktService {
void _startCacheRevalidation() {
_logger.info('Starting cache revalidation timer');
_cacheRevalidationTimer = Timer.periodic(
_cacheRevalidationInterval,
(_) async {
await _revalidateCache();
},
);
}
Future<void> _revalidateCache() async {
_logger.info('Revalidating cache');
for (final key in _cache.keys) {
final cachedData = _cache[key];
if (cachedData != null) {
final updatedData = await _makeRequest(key, bypassCache: true);
_cache[key] = updatedData;
}
}
saveCacheToDisk();
}
Future<dynamic> _makeRequest(String url, {bool bypassCache = false}) async {

View file

@ -67,7 +67,11 @@ class _HomeTabPageState extends State<HomeTabPage> {
if (state == null) continue;
promises.add(state.refresh());
promises.add(() async {
try {
state.refresh();
} catch (e) {}
}());
}
await Future.wait(promises);