pikapika/lib/screens/ViewLogsScreen.dart

235 lines
5.7 KiB
Dart
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Method.dart';
2022-03-19 04:12:27 +00:00
import 'package:pikapika/screens/components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
import 'ComicInfoScreen.dart';
import 'components/Images.dart';
// 浏览记录
class ViewLogsScreen extends StatefulWidget {
2022-03-17 03:31:25 +00:00
const ViewLogsScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
State<StatefulWidget> createState() => _ViewLogsScreenState();
}
class _ViewLogsScreenState extends State<ViewLogsScreen> {
static const _pageSize = 24;
static const _scrollPhysics = AlwaysScrollableScrollPhysics(); // 即使不足一页仍可滚动
final _scrollController = ScrollController();
final _comicList = <ViewLogWrapEntity>[];
var _isLoading = false; // 是否加载中
var _scrollOvered = false; // 滚动到最后
var _offset = 0;
Future _clearAll() async {
if (await confirmDialog(
context,
"您要清除所有浏览记录吗? ",
"将会同时删除浏览进度!",
)) {
await method.clearAllViewLog();
setState(() {
_comicList.clear();
_isLoading = false;
_scrollOvered = true;
_offset = 0;
});
}
}
Future _clearOnce(String id) async {
if (await confirmDialog(
context,
"您要清除这条浏览记录吗? ",
"将会同时删除浏览进度!",
)) {
await method.deleteViewLog(id);
setState(() {
for (var i = 0; i < _comicList.length; i++) {
if (_comicList[i].id == id) {
2021-10-23 02:59:18 +00:00
_comicList.removeAt(i);
_offset--;
2021-09-29 23:57:09 +00:00
break;
}
}
});
}
}
// 加载一页
Future<dynamic> _loadPage() async {
setState(() {
_isLoading = true;
});
try {
var page = await method.viewLogPage(_offset, _pageSize);
if (page.isEmpty) {
_scrollOvered = true;
} else {
_comicList.addAll(page.map((e) =>
ViewLogWrapEntity(e.id, e.title, e.thumbFileServer, e.thumbPath)));
}
_offset += _pageSize;
} finally {
setState(() {
_isLoading = false;
});
}
}
// 滚动事件
void _handScroll() {
if (_scrollController.position.pixels +
MediaQuery.of(context).size.height / 2 <
_scrollController.position.maxScrollExtent) {
return;
}
if (_isLoading || _scrollOvered) return;
_loadPage();
}
@override
void initState() {
_loadPage();
super.initState();
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2022-03-19 04:12:27 +00:00
final screen = NotificationListener(
2021-09-29 23:57:09 +00:00
child: Scaffold(
appBar: AppBar(
2022-03-19 04:12:27 +00:00
title: const Text('浏览记录'),
2021-09-29 23:57:09 +00:00
actions: [
2022-03-19 04:12:27 +00:00
IconButton(onPressed: _clearAll, icon: const Icon(Icons.auto_delete)),
2021-09-29 23:57:09 +00:00
],
),
body: ListView(
physics: _scrollPhysics,
controller: _scrollController,
children: [
Container(height: 10),
ViewLogWrap(
onTapComic: _chooseComic,
comics: _comicList,
onDelete: _clearOnce,
),
],
),
),
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
_handScroll();
}
return true;
},
);
2022-03-19 04:12:27 +00:00
return RightClickPop(screen);
2021-09-29 23:57:09 +00:00
}
void _chooseComic(String comicId) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ComicInfoScreen(
comicId: comicId,
),
),
);
}
}
class ViewLogWrap extends StatelessWidget {
final Function(String) onTapComic;
final List<ViewLogWrapEntity> comics;
final Function(String id) onDelete;
const ViewLogWrap({
Key? key,
required this.onTapComic,
required this.comics,
required this.onDelete,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
var min = size.width < size.height ? size.width : size.height;
var width = (min - 45) / 4;
var entries = comics.map((e) {
return InkWell(
key: e.key,
onTap: () {
onTapComic(e.id);
},
onLongPress: () {
onDelete(e.id);
},
child: Card(
2022-03-17 03:31:25 +00:00
child: SizedBox(
width: width,
child: Column(
children: [
LayoutBuilder(builder:
(BuildContext context, BoxConstraints constraints) {
return RemoteImage(
width: constraints.maxWidth,
fileServer: e.fileServer,
path: e.path);
}),
Text(
e.title + '\n',
maxLines: 2,
overflow: TextOverflow.ellipsis,
2022-03-19 04:12:27 +00:00
style: const TextStyle(height: 1.4),
strutStyle: const StrutStyle(height: 1.4),
),
],
2021-09-29 23:57:09 +00:00
),
2021-10-23 02:59:18 +00:00
),
),
);
});
2022-03-17 03:31:25 +00:00
Map<int, List<Widget>> map = {};
for (var i = 0; i < entries.length; i++) {
late List<Widget> list;
if (i % 4 == 0) {
list = [];
map[i ~/ 4] = list;
} else {
list = map[i ~/ 4]!;
}
list.add(entries.elementAt(i));
}
return Column(
children: map.values.map((e) => Wrap(
alignment: WrapAlignment.spaceAround,
children: e,
)).toList(),
2021-09-29 23:57:09 +00:00
);
}
}
class ViewLogWrapEntity {
2021-10-23 02:59:18 +00:00
final Key key = UniqueKey();
2021-09-29 23:57:09 +00:00
final String id;
final String title;
final String fileServer;
final String path;
2021-10-23 02:59:18 +00:00
ViewLogWrapEntity(this.id, this.title, this.fileServer, this.path);
2021-09-29 23:57:09 +00:00
}