Skip to content

Commit

Permalink
Fix the bug that the local cache is not updated after reloading the i…
Browse files Browse the repository at this point in the history
…mage on the reading page

修复阅读页重载图片后,本地缓存未更新的bug
jiangtian616 committed Nov 3, 2024
1 parent 7944298 commit ddbad26
Showing 6 changed files with 71 additions and 30 deletions.
4 changes: 3 additions & 1 deletion changelog/v8.0.5+267.md
Original file line number Diff line number Diff line change
@@ -2,10 +2,12 @@
- 适配E站不再能查看非本人上传画廊的排行数据的变动
- 优化从分享链接打开app的跳转逻辑
- 修复移动端退出主路由的问题
- 修复阅读页重载图片后,本地缓存未更新的bug

--------------------

- Optimize the search tag prompt
- Adapt to the change that users can no longer view the ranking data of galleries uploaded by others
- Optimize the jump logic from the shared link to open the app
- Fix the problem of exiting the main route on the mobile terminal
- Fix the problem of exiting the main route on the mobile terminal
- Fix the bug that the local cache is not updated after reloading the image on the reading page
29 changes: 27 additions & 2 deletions lib/src/network/eh_cache_manager.dart
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ class EHCacheManager extends Interceptor {
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
CacheOptions cacheOptions = _getCacheOptions(options);

options.extra[realUriExtraKey] = options.uri.toString();
options.extra[realUriExtraKey] = _computeCachedUrl(options, cacheOptions);

if (_shouldSkipRequest(options, cacheOptions)) {
handler.next(options);
@@ -98,6 +98,25 @@ class EHCacheManager extends Interceptor {
return options.store ?? _store;
}

String _computeCachedUrl(RequestOptions options, CacheOptions cacheOptions) {
String cachedUrl = options.uri.toString();
if (cacheOptions.ignoreParams) {
Uri raw = Uri.parse(cachedUrl);
Uri replaced = Uri(
scheme: raw.scheme,
userInfo: raw.userInfo,
host: raw.host,
port: raw.port,
path: raw.path,
query: null,
fragment: raw.fragment.isEmpty ? null : raw.fragment,
);
cachedUrl = replaced.toString();
}

return cachedUrl;
}

bool _shouldSkipRequest(RequestOptions requestOptions, CacheOptions cacheOptions) {
if (requestOptions.method.toUpperCase() == 'POST') {
return true;
@@ -167,13 +186,19 @@ class CacheOptions {

final SqliteCacheStore? store;

final bool ignoreParams;

static const _extraKey = '@cache_options@';

static get noCacheOptions => CacheOptions(policy: CachePolicy.noCache, expire: networkSetting.pageCacheMaxAge.value);

static get noCacheOptionsIgnoreParams => CacheOptions(policy: CachePolicy.noCache, expire: networkSetting.pageCacheMaxAge.value, ignoreParams: true);

static get cacheOptions => CacheOptions(policy: CachePolicy.cache, expire: networkSetting.pageCacheMaxAge.value);

const CacheOptions({this.policy = CachePolicy.cache, required this.expire, this.store});
static get cacheOptionsIgnoreParams => CacheOptions(policy: CachePolicy.cache, expire: networkSetting.pageCacheMaxAge.value, ignoreParams: true);

const CacheOptions({this.policy = CachePolicy.cache, required this.expire, this.store, this.ignoreParams = false});

static CacheOptions? fromExtra(RequestOptions request) {
return request.extra[_extraKey];
2 changes: 1 addition & 1 deletion lib/src/network/eh_request.dart
Original file line number Diff line number Diff line change
@@ -527,7 +527,7 @@ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
if (reloadKey != null) 'nl': reloadKey,
},
cancelToken: cancelToken,
options: useCacheIfAvailable ? CacheOptions.cacheOptions.toOptions() : CacheOptions.noCacheOptions.toOptions(),
options: useCacheIfAvailable ? CacheOptions.cacheOptionsIgnoreParams.toOptions() : CacheOptions.noCacheOptionsIgnoreParams.toOptions(),
);
return _parseResponse(response, parser);
}
55 changes: 30 additions & 25 deletions lib/src/pages/read/layout/vertical_list/vertical_list_layout.dart
Original file line number Diff line number Diff line change
@@ -2,15 +2,29 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jhentai/src/model/read_page_info.dart';
import 'package:jhentai/src/pages/read/layout/vertical_list/vertical_list_layout_state.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:zoom_view/zoom_view.dart';

import '../../../../setting/read_setting.dart';
import '../../../../utils/screen_size_util.dart';
import '../../../../widget/eh_wheel_speed_controller_for_read_page.dart';
import '../base/base_layout.dart';
import 'vertical_list_layout_logic.dart';

class ScrollOffsetToScrollController extends ScrollController {
ScrollOffsetToScrollController({required this.scrollOffsetController});

final ScrollOffsetController scrollOffsetController;

@override
ScrollPosition get position => scrollOffsetController.position;

@override
void jumpTo(double value) {
scrollOffsetController.jumpTo(value: value);
}
}

class VerticalListLayout extends BaseLayout {
VerticalListLayout({Key? key}) : super(key: key);

@@ -22,31 +36,22 @@ class VerticalListLayout extends BaseLayout {
Widget buildBody(BuildContext context) {
return GetBuilder<VerticalListLayoutLogic>(
id: logic.verticalLayoutId,
builder: (_) => PhotoViewGallery.builder(
scrollDirection: Axis.vertical,
itemCount: 1,
builder: (_, __) => PhotoViewGalleryPageOptions.customChild(
controller: state.photoViewController,
initialScale: 1.0,
minScale: 1.0,
maxScale: 2.5,
scaleStateCycle: readSetting.enableDoubleTapToScaleUp.isTrue ? logic.scaleStateCycle : null,
enableTapDragZoom: readSetting.enableTapDragToScaleUp.isTrue,
child: EHWheelSpeedControllerForReadPage(
builder: (_) => ZoomView(
controller: ScrollOffsetToScrollController(scrollOffsetController: state.scrollOffsetController),
child: EHWheelSpeedControllerForReadPage(
scrollOffsetController: state.scrollOffsetController,
child: ScrollablePositionedList.separated(
physics: const ClampingScrollPhysics(),
minCacheExtent: readPageState.readPageInfo.mode == ReadMode.online
? readSetting.preloadDistance * screenHeight * 1
: readSetting.preloadDistanceLocal * screenHeight * 1,
initialScrollIndex: readPageState.readPageInfo.initialIndex,
itemCount: readPageState.readPageInfo.pageCount,
itemScrollController: state.itemScrollController,
itemPositionsListener: state.itemPositionsListener,
scrollOffsetController: state.scrollOffsetController,
child: ScrollablePositionedList.separated(
physics: const ClampingScrollPhysics(),
minCacheExtent: readPageState.readPageInfo.mode == ReadMode.online
? readSetting.preloadDistance * screenHeight * 1
: readSetting.preloadDistanceLocal * screenHeight * 1,
initialScrollIndex: readPageState.readPageInfo.initialIndex,
itemCount: readPageState.readPageInfo.pageCount,
itemScrollController: state.itemScrollController,
itemPositionsListener: state.itemPositionsListener,
scrollOffsetController: state.scrollOffsetController,
itemBuilder: _imageBuilder,
separatorBuilder: (_, __) => Obx(() => SizedBox(height: readSetting.imageSpace.value.toDouble())),
),
itemBuilder: _imageBuilder,
separatorBuilder: (_, __) => Obx(() => SizedBox(height: readSetting.imageSpace.value.toDouble())),
),
),
),
10 changes: 9 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
@@ -1436,7 +1436,7 @@ packages:
description:
path: "packages/scrollable_positioned_list"
ref: HEAD
resolved-ref: "21e8469b2d06a410a0acd24d735a8b1b3a820a20"
resolved-ref: "5dc660081452cfb76f574e1252eb34ba69c40257"
url: "https://github.com/jiangtian616/flutter.widgets"
source: git
version: "0.3.8+1"
@@ -2078,6 +2078,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.4+10"
zoom_view:
dependency: "direct main"
description:
name: zoom_view
sha256: fe1c6a0f5112836a7ac81d61396fef742151e419aa84bde57d47e72fd8669bee
url: "https://pub.dev"
source: hosted
version: "0.0.15"
zoom_widget:
dependency: "direct main"
description:
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ dependencies:
git:
url: https://github.com/jiangtian616/photo_view
ref: separate
zoom_view: 0.0.15
package_info_plus: 8.0.3
file_picker: 8.1.3
local_auth: 2.2.0

0 comments on commit ddbad26

Please sign in to comment.