Skip to content

Commit a9baa11

Browse files
committed
add cache for blog images & unload when not visible
1 parent b2bdc8d commit a9baa11

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

lib/Frontend/Widgets/tail_blog.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33

44
import 'package:connectivity_plus/connectivity_plus.dart';
55
import 'package:dio/dio.dart';
6+
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_riverpod/flutter_riverpod.dart';
89
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -269,18 +270,24 @@ class _TailBlogImageState extends ConsumerState<TailBlogImage> {
269270
}
270271
}
271272

272-
@Riverpod(keepAlive: true)
273+
@Riverpod()
273274
Future<Widget> getBlogImage(Ref ref, String url) async {
274275
if (!await tailBlogConnectivityCheck()) {
275276
return Container();
276277
}
277278
Dio dio = await initDio();
279+
278280
Response<List<int>> response = await dio.get(
279281
url,
280-
options: Options(
281-
responseType: ResponseType.bytes,
282-
followRedirects: true,
283-
),
282+
options: cacheOptions
283+
.copyWith(
284+
policy: CachePolicy.forceCache,
285+
)
286+
.toOptions()
287+
.copyWith(
288+
responseType: ResponseType.bytes,
289+
followRedirects: true,
290+
),
284291
);
285292

286293
if (response.statusCode! < 400) {

lib/Frontend/utils.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:device_info_plus/device_info_plus.dart';
22
import 'package:dio/dio.dart';
3+
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
34
import 'package:dio_smart_retry/dio_smart_retry.dart';
45
import 'package:flutter/material.dart';
56
import 'package:flutter/services.dart';
67
import 'package:flutter_riverpod/flutter_riverpod.dart';
8+
import 'package:http_cache_hive_store/http_cache_hive_store.dart';
79
import 'package:intl/intl.dart';
810
import 'package:logarte/logarte.dart';
911
import 'package:logging/logging.dart';
@@ -70,6 +72,17 @@ Future<String> initLocale(Ref ref) async {
7072
final dioLogger = Logger('Dio');
7173

7274
Dio? _dio;
75+
final cacheOptions = CacheOptions(
76+
// A default store is required for interceptor.
77+
store: HiveCacheStore(
78+
null,
79+
hiveBoxName: "dioCache",
80+
),
81+
hitCacheOnErrorCodes: const [500],
82+
83+
hitCacheOnNetworkFailure: true,
84+
maxStale: const Duration(days: 7),
85+
);
7386

7487
Future<Dio> initDio({skipSentry = false}) async {
7588
if (_dio != null) {
@@ -108,6 +121,10 @@ Future<Dio> initDio({skipSentry = false}) async {
108121
],
109122
),
110123
);
124+
125+
// Global options
126+
127+
dio.interceptors.add(DioCacheInterceptor(options: cacheOptions));
111128
if (!skipSentry) {
112129
/// This *must* be the last initialization step of the Dio setup, otherwise
113130
/// your configuration of Dio might overwrite the Sentry configuration.
@@ -123,7 +140,8 @@ Future<WordpressClient> getWordpressClient() async {
123140
if (_wordpressClient != null) {
124141
return _wordpressClient!;
125142
}
126-
return WordpressClient.fromDioInstance(baseUrl: Uri.parse('https://thetailcompany.com/wp-json/wp/v2'), instance: await initDio());
143+
Dio dio = await initDio();
144+
return WordpressClient.fromDioInstance(baseUrl: Uri.parse('https://thetailcompany.com/wp-json/wp/v2'), instance: dio);
127145
}
128146

129147
Version getVersionSemVer(String input) {

pubspec.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ packages:
386386
url: "https://pub.dev"
387387
source: hosted
388388
version: "5.8.0+1"
389+
dio_cache_interceptor:
390+
dependency: "direct main"
391+
description:
392+
name: dio_cache_interceptor
393+
sha256: c1cbf8be886b3e077165dda50a1b3bb299b8a72694af94d065b4d2ac0fee67d7
394+
url: "https://pub.dev"
395+
source: hosted
396+
version: "4.0.3"
389397
dio_smart_retry:
390398
dependency: "direct main"
391399
description:
@@ -817,6 +825,22 @@ packages:
817825
url: "https://pub.dev"
818826
source: hosted
819827
version: "1.3.0"
828+
http_cache_core:
829+
dependency: transitive
830+
description:
831+
name: http_cache_core
832+
sha256: b0accfa821e73085b5252dd42a6908d19ea0c29badd46db3668af6f8e510cfe1
833+
url: "https://pub.dev"
834+
source: hosted
835+
version: "1.1.1"
836+
http_cache_hive_store:
837+
dependency: "direct main"
838+
description:
839+
name: http_cache_hive_store
840+
sha256: "85847efdb18094961a66b74d3b856da093ddcbaf7739adecdc28149e871fb8fe"
841+
url: "https://pub.dev"
842+
source: hosted
843+
version: "5.0.1"
820844
http_multi_server:
821845
dependency: transitive
822846
description:

pubspec.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ dependencies:
1717
go_router: ^15.1.1
1818
vector_math: # used for joystick, pinned to SDK version
1919
collection: ^1.19.1 # Priority Queue
20-
intl: #pinned to flutter version?
2120
uuid: ^4.5.1 # Used to generate UUID v4 ids for custom actions & move lists
2221
json_annotation: ^4.9.0
2322
crypto: ^3.0.6 # used for md5 hash checking during ota download
@@ -76,11 +75,17 @@ dependencies:
7675
git:
7776
url: https://github.com/Codel1417/packages.flutter
7877
path: packages/pdfx
78+
79+
# Locale
7980
flutter_localized_locales: ^2.0.5 # Get language names from shortcode
8081
country_flags: ^3.2.0 # locale flags
82+
intl: #pinned to flutter version?
83+
8184
# Dio HTTP
8285
dio: ^5.8.0+1
8386
dio_smart_retry: ^7.0.1
87+
dio_cache_interceptor: ^4.0.3
88+
http_cache_hive_store: ^5.0.1
8489

8590
# Sensors
8691
pedometer: ^4.1.1

0 commit comments

Comments
 (0)