Skip to content

Commit

Permalink
Refactor: 完成列表阅读的重构
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jul 24, 2021
1 parent a9b6f5e commit 0d99d9a
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 135 deletions.
84 changes: 81 additions & 3 deletions lib/pages/image_view_ext/controller/view_ext_contorller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
import 'package:get/get.dart';
import 'package:orientation/orientation.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

import 'view_ext_state.dart';

Expand All @@ -29,6 +30,7 @@ const String idViewBar = 'ViewExtController.ViewBar';
const String idViewPageSlider = 'ViewExtController.ViewPageSlider';
const String idSlidePage = 'ViewExtController.ViewImageSlidePage';
const String idImagePageView = 'ViewExtController.ImagePageView';
const String idImageListView = 'ViewExtController.ImageListView';
const String idViewColumnModeIcon = 'ViewExtController.ViewColumnModeIcon';
const String idAutoReadIcon = 'ViewExtController.AutoReadIcon';

Expand All @@ -50,6 +52,10 @@ class ViewExtController extends GetxController {

Map<int, Future<GalleryImage?>> imageFutureMap = {};

final ItemScrollController itemScrollController = ItemScrollController();
final ItemPositionsListener itemPositionsListener =
ItemPositionsListener.create();

@override
void onInit() {
super.onInit();
Expand All @@ -60,6 +66,18 @@ class ViewExtController extends GetxController {
viewportFraction: vState.showPageInterval ? 1.1 : 1.0,
);

// 竖屏模式初始页码
if (vState.viewMode == ViewMode.topToBottom) {
// logger.v('竖屏模式初始页码: ${vState.itemIndex}');
Future.delayed(const Duration(milliseconds: 50)).then((value) =>
itemScrollController.jumpTo(index: vState.currentItemIndex));
}

itemPositionsListener.itemPositions.addListener(() {
final positions = itemPositionsListener.itemPositions.value;
handItemPositionsChange(positions);
});

/// 初始预载
/// 后续的预载触发放在翻页事件中
if (vState.loadType == LoadType.network) {
Expand Down Expand Up @@ -97,6 +115,7 @@ class ViewExtController extends GetxController {
@override
void onClose() {
Get.find<GalleryCacheController>().saveAll();
vState.saveLastIndex(saveToStore: true);
pageController.dispose();
vState.getMoreCancelToken.cancel();
// SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
Expand All @@ -112,7 +131,9 @@ class ViewExtController extends GetxController {

void resetPageController() {
pageController = PageController(
initialPage: pageController.page?.round() ?? vState.currentItemIndex,
initialPage: pageController.positions.isNotEmpty
? pageController.page?.round() ?? vState.currentItemIndex
: vState.currentItemIndex,
viewportFraction: vState.showPageInterval ? 1.1 : 1.0,
);
}
Expand Down Expand Up @@ -321,8 +342,13 @@ class ViewExtController extends GetxController {

void handOnSliderChangedEnd(double value) {
vState.currentItemIndex = value.round();
// TODO(honjow): 上下滚动模式
pageController.jumpToPage(vState.pageIndex);

if (vState.viewMode != ViewMode.topToBottom) {
pageController.jumpToPage(vState.pageIndex);
} else {
itemScrollController.jumpTo(index: vState.currentItemIndex);
update([idViewTopBar]);
}
}

void handOnSliderChanged(double value) {
Expand Down Expand Up @@ -488,6 +514,46 @@ class ViewExtController extends GetxController {
}
}

/// 竖屏阅读下页码变化的监听
void handItemPositionsChange(Iterable<ItemPosition> positions) {
int min;
int max;
if (positions.isNotEmpty) {
// Determine the first visible item by finding the item with the
// smallest trailing edge that is greater than 0. i.e. the first
// item whose trailing edge in visible in the viewport.
min = positions
.where((ItemPosition position) => position.itemTrailingEdge > 0)
.reduce((ItemPosition min, ItemPosition position) =>
position.itemTrailingEdge < min.itemTrailingEdge ? position : min)
.index;
// Determine the last visible item by finding the item with the
// greatest leading edge that is less than 1. i.e. the last
// item whose leading edge in visible in the viewport.
max = positions
.where((ItemPosition position) => position.itemLeadingEdge < 1)
.reduce((ItemPosition max, ItemPosition position) =>
position.itemLeadingEdge > max.itemLeadingEdge ? position : max)
.index;

vState.tempIndex = (min + max) ~/ 2;
// logger.d('max $max min $min tempIndex ${vState.tempIndex}');

// logger.d('${positions.elementAt(index).itemLeadingEdge} ');
if (vState.tempIndex != vState.currentItemIndex &&
vState.conditionItemIndex) {
// logger.d('${vState.tempIndex} ${vState.currentItemIndex}');
Future.delayed(const Duration(milliseconds: 50)).then((value) {
// logger.d('tempIndex ${vState.tempIndex}');
vState.currentItemIndex = vState.tempIndex;
vState.sliderValue = vState.currentItemIndex / 1.0;
update([idViewTopBar, idViewPageSlider]);
});
}
}
// logger.i('First Item: ${min ?? ''}\nLast Item: ${max ?? ''}');
}

Future<void> _turnNextPage() async {
if (vState.autoRead && vState.pageIndex < vState.pageCount - 1) {
logger5.v('next page ${vState.pageIndex + 1}');
Expand Down Expand Up @@ -521,4 +587,16 @@ class ViewExtController extends GetxController {
debounceTimer = null;
});
}

Future<void> handOnViewModeChanged(ViewMode val) async {
final itemIndex = vState.currentItemIndex;
update([idImagePageView, idViewBottomBar]);
await Future.delayed(Duration(milliseconds: 50));
if (val == ViewMode.topToBottom) {
itemScrollController.jumpTo(index: itemIndex);
} else {
vState.currentItemIndex = itemIndex;
pageController.jumpToPage(vState.pageIndex);
}
}
}
7 changes: 4 additions & 3 deletions lib/pages/image_view_ext/controller/view_ext_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class ViewExtState {
_currentItemIndex = val;

// 防抖
vDebounceM(() => _saveLastIndex(), id: '_currentItemIndex');
vDebounceM(() => saveLastIndex(), id: '_currentItemIndex');
vDebounceM(
() => _saveLastIndex(saveToStore: true),
() => saveLastIndex(saveToStore: true),
id: '_currentItemIndex',
durationTime: const Duration(seconds: 5),
);
}

void _saveLastIndex({bool saveToStore = false}) {
void saveLastIndex({bool saveToStore = false}) {
if (loadType == LoadType.network) {
if (galleryPageController.galleryItem.gid != null && conditionItemIndex) {
galleryPageController.lastIndex = currentItemIndex;
Expand Down Expand Up @@ -178,4 +178,5 @@ class ViewExtState {
bool needRebuild = false;

bool conditionItemIndex = true;
int tempIndex = 0;
}
Loading

0 comments on commit 0d99d9a

Please sign in to comment.