Skip to content

Commit 871be83

Browse files
committed
# 优化
- 优化图片的获取逻辑,减少下载次数,提高效率。 - 优化图片的加载逻辑,缓解抖动问题。
1 parent 6828373 commit 871be83

File tree

10 files changed

+62
-73
lines changed

10 files changed

+62
-73
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@
6868
- 开发者:**[windy](https://github.com/deretame)**
6969
- 项目仓库:**[Breeze](https://github.com/deretame/Breeze)**
7070
- 联系方式:**[telegram(电报)](https://t.me/breeze_zh_cn)**
71+
72+
73+
/data/user/0/com.zephyr.breeze/files/downloads/bika/original/6172ed076f7967071f380ba4/comic/6172ed076f7967071f380ba5/2a83fa93-8a39-44d9-a665-054376a85705.jpg
74+
/data/user/0/com.zephyr.breeze/files/downloads/bika/original/6172ed076f7967071f380ba4/comic/1/4ed6de25-6317-4ef0-87ff-8c5a671d1bfe.jpg
75+
/data/user/0/com.zephyr.breeze/cache/bika/original/6172ed076f7967071f380ba4/comic/1/4ed6de25-6317-4ef0-87ff-8c5a671d1bfe.jpg

lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Future<void> main() async {
4747

4848
// 告诉系统应该用竖屏
4949
await SystemChrome.setPreferredOrientations(
50-
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
50+
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
51+
);
5152

5253
// 初始化Hive
5354
await Hive.initFlutter();

lib/network/webdav.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:xml/xml.dart' as xml;
88

99
import '../../../main.dart';
1010
import '../object_box/model.dart';
11-
import '../object_box/objectbox.g.dart';
1211

1312
// 测试 WebDAV 服务是否可用
1413
Future<void> testWebDavServer() async {
@@ -308,11 +307,8 @@ Future<List<String>> fetchWebDAVFiles() async {
308307

309308
Future<void> uploadFile2WebDav() async {
310309
// 获取数据并转换为 JSON
311-
var allHistory = await objectbox.bikaHistoryBox
312-
.query()
313-
.order(BikaComicHistory_.history, flags: Order.descending)
314-
.build()
315-
.findAsync();
310+
var allHistory = await objectbox.bikaHistoryBox.getAllAsync();
311+
allHistory.sort((a, b) => b.history.compareTo(a.history));
316312
var comicHistoriesJson = allHistory.map((comic) => comic.toJson()).toList();
317313
String comicHistoriesJsonString = jsonEncode(comicHistoriesJson);
318314

lib/object_box/model.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class BikaComicHistory {
55
@Id()
66
int id;
77

8-
@Index()
8+
// @Index()
99
String comicId;
1010
String creatorId;
1111
String creatorGender;
@@ -53,7 +53,7 @@ class BikaComicHistory {
5353
int commentsCount;
5454
bool isFavourite;
5555
bool isLiked;
56-
@Index()
56+
// @Index()
5757
@Property(type: PropertyType.date)
5858
DateTime history;
5959

lib/object_box/objectbox-model.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@
7777
{
7878
"id": "14:4488370369732020728",
7979
"name": "comicId",
80-
"type": 9,
81-
"flags": 2048,
82-
"indexId": "3:3486826873856380308"
80+
"type": 9
8381
},
8482
{
8583
"id": "15:5901265100428652114",
@@ -89,9 +87,7 @@
8987
{
9088
"id": "16:2976387199063785145",
9189
"name": "history",
92-
"type": 10,
93-
"flags": 8,
94-
"indexId": "4:931357141992892300"
90+
"type": 10
9591
},
9692
{
9793
"id": "17:4401910177588291657",
@@ -500,7 +496,10 @@
500496
264467845223121709,
501497
1327783071807974029
502498
],
503-
"retiredIndexUids": [],
499+
"retiredIndexUids": [
500+
3486826873856380308,
501+
931357141992892300
502+
],
504503
"retiredPropertyUids": [
505504
4194924827811470754,
506505
6505186057625373131,

lib/object_box/objectbox.g.dart

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/page/comic_read/view/comic_read.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class _ComicReadPageState extends State<_ComicReadPage>
148148
var temp2 = comic_all_info_json.comicAllInfoJsonFromJson(temp);
149149
_downloadEpsInfo = temp2.eps;
150150
}
151+
152+
debugPrint(_type.toString().split('.').last);
151153
}
152154

153155
@override
@@ -317,7 +319,7 @@ class _ComicReadPageState extends State<_ComicReadPage>
317319
return ReadImageWidget(
318320
media: medias[index - 1],
319321
comicId: comicId,
320-
epsId: _doc.order,
322+
epsId: _doc.id,
321323
index: index - 1,
322324
chapterId: _doc.docId,
323325
);
@@ -602,11 +604,13 @@ class _ComicReadPageState extends State<_ComicReadPage>
602604
max: maxValue,
603605
label: (_currentSliderValue.toInt() + 1).toString(),
604606
onChanged: (double newValue) {
605-
setState(() {
606-
_currentSliderValue = newValue;
607-
_sliderIsRollingTimer?.cancel();
608-
_isSliderRolling = true;
609-
});
607+
if (_currentSliderValue.toInt() != newValue.toInt()) {
608+
setState(() {
609+
_currentSliderValue = newValue;
610+
});
611+
}
612+
_isSliderRolling = true;
613+
_sliderIsRollingTimer?.cancel();
610614

611615
// 显示 Overlay 提示框
612616
_showOverlayToast((newValue.toInt() + 1).toString());
@@ -744,10 +748,12 @@ class _ComicReadPageState extends State<_ComicReadPage>
744748
}
745749
}
746750

747-
if (closestPosition != null && mounted && _isSliderRolling == false) {
748-
if (pageIndex != closestPosition.index) {
749-
debugPrint('更新索引:$pageIndex');
750-
}
751+
if (closestPosition != null &&
752+
mounted &&
753+
_isSliderRolling == false &&
754+
pageIndex != closestPosition.index) {
755+
debugPrint('更新索引:$pageIndex');
756+
751757
setState(() {
752758
pageIndex = closestPosition!.index;
753759
if (_isComicRolling == false) {
@@ -770,12 +776,7 @@ class _ComicReadPageState extends State<_ComicReadPage>
770776
comicHistory!.epPageCount = pageIndex;
771777
comicHistory!.epTitle = _doc.title;
772778
await objectbox.bikaHistoryBox.putAsync(comicHistory!);
773-
if (mounted) {
774-
// 添加 mounted 检查
775-
setState(() {
776-
_isInserting = false;
777-
});
778-
}
779+
_isInserting = false;
779780
_lastUpdateTime = DateTime.now();
780781
}
781782

lib/page/comic_read/widgets/image_display.dart

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,46 @@ class _ImageDisplayState extends State<ImageDisplay> {
1818
double imageWidth = screenWidth;
1919
double imageHeight = screenWidth;
2020

21-
bool _isMounted = false; // 标志,指示 Widget 是否仍挂载
21+
Future<ImageInfo>? _imageInfoFuture;
2222

2323
@override
2424
void initState() {
2525
super.initState();
26-
_isMounted = true; // Widget 初始化时认为它是挂载的
27-
_getImageResolution(widget.imagePath);
26+
_imageInfoFuture = _getImageResolution(widget.imagePath);
2827
}
2928

30-
Future<void> _getImageResolution(String imagePath) async {
31-
final Completer<void> completer = Completer();
29+
Future<ImageInfo> _getImageResolution(String imagePath) async {
30+
final Completer<ImageInfo> completer = Completer();
3231
final Image image = Image.file(File(imagePath));
3332

34-
// 监听图片解析完成
3533
image.image.resolve(ImageConfiguration()).addListener(
3634
ImageStreamListener((ImageInfo imageInfo, _) {
37-
// 只有在 Widget 仍挂载时才调用 setState
38-
if (_isMounted) {
39-
setState(() {
40-
imageWidth = imageInfo.image.width.toDouble();
41-
imageHeight = imageInfo.image.height.toDouble();
42-
});
43-
}
44-
completer.complete();
35+
completer.complete(imageInfo);
4536
}),
4637
);
4738

48-
await completer.future; // 等待解析完成
49-
}
50-
51-
@override
52-
void dispose() {
53-
_isMounted = false; // Widget 暴露时,设置为未挂载
54-
super.dispose();
39+
return completer.future;
5540
}
5641

5742
@override
5843
Widget build(BuildContext context) {
59-
final double screenWidth = MediaQuery.of(context).size.width;
44+
return FutureBuilder<ImageInfo>(
45+
future: _imageInfoFuture,
46+
builder: (context, snapshot) {
47+
if (snapshot.connectionState == ConnectionState.done &&
48+
snapshot.hasData) {
49+
final imageInfo = snapshot.data!;
50+
imageWidth = imageInfo.image.width.toDouble();
51+
imageHeight = imageInfo.image.height.toDouble();
52+
}
6053

61-
return SizedBox(
62-
width: screenWidth,
63-
height: imageHeight != screenWidth
64-
? (imageHeight * (screenWidth / imageWidth))
65-
: screenWidth, // 动态计算高度
66-
child: imageWidth != screenWidth && imageHeight != screenWidth
67-
? Image.file(
68-
File(widget.imagePath),
69-
fit: BoxFit.cover, // 使图片填充整个屏幕
70-
)
71-
: Container(color: Color(0xFF2D2D2D)), // 占位符
54+
return AspectRatio(
55+
aspectRatio: imageWidth / imageHeight,
56+
child: imageWidth != screenWidth && imageHeight != screenWidth
57+
? Image.file(File(widget.imagePath), fit: BoxFit.cover)
58+
: Container(color: Color(0xFF2D2D2D)),
59+
);
60+
},
7261
);
7362
}
7463
}

lib/page/comic_read/widgets/read_image_widget.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import '../../../widgets/picture_bloc/models/picture_info.dart';
99

1010
class ReadImageWidget extends StatefulWidget {
1111
final String comicId;
12-
final int epsId;
12+
final String epsId;
1313
final Media media;
1414
final int index;
1515
final String chapterId;
@@ -31,7 +31,7 @@ class _ReadImageWidgetState extends State<ReadImageWidget>
3131
with AutomaticKeepAliveClientMixin {
3232
String get comicId => widget.comicId;
3333

34-
int get epsId => widget.epsId;
34+
String get epsId => widget.epsId;
3535

3636
Media get media => widget.media;
3737

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: zephyr
22
description: "使用Flutter开发的三方哔咔应用。"
3-
version: 1.24.0+68
3+
version: 1.24.1+69
44

55
environment:
66
sdk: ">=3.0.0"

0 commit comments

Comments
 (0)