improved performance and battery usage on older devices
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../../../../api/errors/ticker_content_unavailable_exception.dart';
|
||||
import '../../../../../api/marianumconnect/queries/get_ticker_page/get_ticker_page_response.dart';
|
||||
import '../../../infrastructure/utility_widgets/loadable_hydrated_bloc/loadable_hydrated_bloc.dart';
|
||||
@@ -17,7 +22,35 @@ class TickerPageBloc
|
||||
> {
|
||||
final String slug;
|
||||
|
||||
TickerPageBloc(this.slug);
|
||||
TickerPageBloc(this.slug) {
|
||||
unawaited(_rememberSlug());
|
||||
}
|
||||
|
||||
static const _recentSlugsKey = 'tickerPageRecentSlugs';
|
||||
static const _keepPages = 50;
|
||||
|
||||
/// Every page ever opened used to stay in the hydrated box forever — and
|
||||
/// the whole box is decoded before the first frame on each cold start.
|
||||
/// The most recently opened pages (far more than a normal reader revisits)
|
||||
/// keep their offline copy; only long-forgotten ones are dropped.
|
||||
Future<void> _rememberSlug() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final recent = prefs.getStringList(_recentSlugsKey) ?? <String>[];
|
||||
recent
|
||||
..remove(slug)
|
||||
..insert(0, slug);
|
||||
for (final evicted in recent.skip(_keepPages)) {
|
||||
await HydratedBloc.storage.delete('$storagePrefix$evicted');
|
||||
}
|
||||
await prefs.setStringList(
|
||||
_recentSlugsKey,
|
||||
recent.take(_keepPages).toList(),
|
||||
);
|
||||
} on Object {
|
||||
// Best effort: a failed cleanup only leaves an extra offline copy.
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String get id => slug;
|
||||
|
||||
Reference in New Issue
Block a user