Skip to content

Commit

Permalink
Fix: 修正双页模式自动翻页问题
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jul 8, 2021
1 parent cfbd02e commit f71a915
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
69 changes: 63 additions & 6 deletions lib/pages/image_view/controller/view_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:fehviewer/models/base/eh_models.dart';
import 'package:fehviewer/pages/gallery/controller/gallery_page_controller.dart';
import 'package:fehviewer/pages/image_view/common.dart';
import 'package:fehviewer/utils/logger.dart';
import 'package:fehviewer/utils/utility.dart';
import 'package:fehviewer/utils/utility.dart' as utility;
import 'package:fehviewer/utils/vibrate.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -150,6 +150,7 @@ class ViewController extends GetxController {
logger.d('恢复系统旋转设置');
OrientationPlugin.setPreferredOrientations(DeviceOrientation.values);
// OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
cancelAutoRead();
super.onClose();
}

Expand Down Expand Up @@ -245,7 +246,8 @@ class ViewController extends GetxController {

// 点击周围
void handOnPanDown(DragDownDetails details) {
final Rect _centRect = WidgetUtil.getWidgetGlobalRect(vState.centkey);
final Rect _centRect =
utility.WidgetUtil.getWidgetGlobalRect(vState.centkey);

final double _dx = details.globalPosition.dx;
final double _dy = details.globalPosition.dy;
Expand Down Expand Up @@ -443,16 +445,26 @@ class ViewController extends GetxController {
);
}

Future<void> _startAutoRead() async {
await Future.delayed(Duration(milliseconds: _ehConfigService.turnPageInv));
void cancelAutoRead() {
vState.autoRead = false;
}

Future<void> _startAutoRead({delayed = true}) async {
if (delayed) {
await Future.delayed(
Duration(milliseconds: _ehConfigService.turnPageInv));
}

if (vState.autoRead && vState.pageIndex < vState.pageCount - 1) {
logger.d('next page');
logger.v('next page');
pageController.nextPage(
duration: const Duration(milliseconds: 300), curve: Curves.easeOut);
}
}

Future<void> _startAutoReadNotDelayed() async =>
await _startAutoRead(delayed: false);

Future<void> onLoadCompleted(int ser) async {
vState.loadCompleMap[ser] = true;

Expand All @@ -466,11 +478,56 @@ class ViewController extends GetxController {
if (vState.filecount > serLeft) {
final leftComplet = vState.loadCompleMap[serLeft] ?? false;
final rigthComple = vState.loadCompleMap[serLeft + 1] ?? false;
logger.d(
logger.v(
' $serLeft leftComplet: $leftComplet , ${serLeft + 1} rigthComple:$rigthComple');
if (leftComplet && rigthComple) {
debounce(_startAutoReadNotDelayed,
Duration(milliseconds: _ehConfigService.turnPageInv));
}
} else {
await _startAutoRead();
}
}
}

static const deFaultDurationTime = Duration(milliseconds: 300);
static Timer? timer;

// 防抖函数
void debounce(Function? doSomething,
[Duration durationTime = deFaultDurationTime]) {
// timer?.cancel();
if (timer?.isActive ?? false) {
logger.v('timer.cancel');
timer?.cancel();
}
timer = Timer(durationTime, () {
loggerTime.v('func.call');
doSomething?.call();
timer = null;
});
}

/// 函数防抖
///
/// [func]: 要执行的方法
/// [delay]: 要迟延的时长
Function _debounce(
Function? func, [
Duration delay = const Duration(milliseconds: 2000),
]) {
logger.d('debounce');
Timer? timer;
final Function target = () {
if (timer?.isActive ?? false) {
logger.d('timer.cancel');
timer?.cancel();
}
timer = Timer(delay, () async {
loggerTime.d('func.call');
await func?.call();
});
};
return target;
}
}
2 changes: 1 addition & 1 deletion lib/pages/image_view/controller/view_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ViewState {
debounce(_itemIndex, (int val) {
if (_galleryPageController.galleryItem.gid != null &&
conditionItemIndex) {
logger.d('debounce 300 _itemIndex to $val');
logger.v('debounce 300 _itemIndex to $val');
_galleryPageController.lastIndex = itemIndex;
_galleryCacheController.setIndex(
_galleryPageController.galleryItem.gid ?? '', itemIndex);
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/image_view/view/view_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ class _ViewImageState extends State<ViewImage>
imageWidth: snapshot.data!.imageWidth!,
imageHeight: snapshot.data!.imageHeight!,
retry: widget.retry,
onLoadCompleted: () =>
_viewController.onLoadCompleted(widget.ser),
onLoadCompleted: () async =>
await _viewController.onLoadCompleted(widget.ser),
);

image = Stack(
Expand Down
14 changes: 13 additions & 1 deletion lib/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ String? logFileName;

late Logger logger;

late Logger loggerTime;

late Logger logger5;

late Logger loggerNoStack;
Expand Down Expand Up @@ -67,7 +69,6 @@ void resetLogLevel() {

void initLoggerValue() {
logger = Logger(
// level: _logLevel,
filter: EHLogFilter(),
// printer: SimplePrinter(),
printer: EhPrettyPrinter(
Expand All @@ -78,6 +79,17 @@ void initLoggerValue() {
output: _outPut,
);

loggerTime = Logger(
filter: EHLogFilter(),
// printer: SimplePrinter(),
printer: EhPrettyPrinter(
// lineLength: 100,
colors: false,
printTime: true,
),
output: _outPut,
);

logger5 = Logger(
filter: EHLogFilter(),
printer: EhPrettyPrinter(
Expand Down
1 change: 1 addition & 0 deletions lib/utils/utility.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: fehviewer

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.1.17+222
version: 1.1.18+224

environment:
sdk: '>=2.13.0 <3.0.0'
Expand Down

0 comments on commit f71a915

Please sign in to comment.