pikapika/lib/screens/ViewLogsScreen.dart

256 lines
6.2 KiB
Dart
Raw Permalink Normal View History

2022-07-09 07:11:26 +00:00
import 'dart:convert';
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-07-09 07:11:26 +00:00
import 'package:pikapika/screens/components/ComicInfoCard.dart';
2022-03-19 04:12:27 +00:00
import 'package:pikapika/screens/components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
2022-07-09 07:11:26 +00:00
import '../basic/Entities.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();
2022-07-09 07:11:26 +00:00
final _comicList = <ViewLog>[];
2021-09-29 23:57:09 +00:00
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 {
2022-07-09 07:11:26 +00:00
_comicList.addAll(page);
2021-09-29 23:57:09 +00:00
}
_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-07-09 07:11:26 +00:00
var entries = _comicList.map((e) {
return InkWell(
onTap: () {
_chooseComic(e.id);
},
onLongPress: () {
_clearOnce(e.id);
},
child: ViewInfoCard(
fileServer: e.thumbFileServer,
author: e.author,
categories: _decodeCate(e.categories),
path: e.thumbPath,
title: e.title,
),
);
});
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-07-09 07:11:26 +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,
2022-07-09 07:11:26 +00:00
children: entries.toList(),
2021-09-29 23:57:09 +00:00
),
),
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
_handScroll();
}
return true;
},
);
2022-03-25 14:57:30 +00:00
return rightClickPop(
child: screen,
context: context,
canPop: true,
);
2021-09-29 23:57:09 +00:00
}
void _chooseComic(String comicId) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ComicInfoScreen(
comicId: comicId,
),
),
);
}
2022-07-09 07:11:26 +00:00
List<String> _decodeCate(String categories) {
try {
var decode = jsonDecode(categories);
if (decode is List) {
return List.of(decode).cast();
}
return [decode];
} catch (e) {
return [categories];
}
}
2021-09-29 23:57:09 +00:00
}
2022-07-09 07:11:26 +00:00
class ViewInfoCard extends StatelessWidget {
final String fileServer;
final String path;
final String title;
final String author;
final List<String> categories;
2021-09-29 23:57:09 +00:00
2022-07-09 07:11:26 +00:00
const ViewInfoCard({
2021-09-29 23:57:09 +00:00
Key? key,
2022-07-09 07:11:26 +00:00
required this.fileServer,
required this.path,
required this.title,
required this.author,
required this.categories,
2021-09-29 23:57:09 +00:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-07-09 07:11:26 +00:00
var theme = Theme.of(context);
return Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: theme.dividerColor,
),
),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.only(right: 10),
child: RemoteImage(
fileServer: fileServer,
path: path,
width: imageWidth,
height: imageHeight,
),
),
Expanded(
child: Row(
children: [
2022-07-09 07:11:26 +00:00
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: titleStyle),
Container(height: 5),
Text(author, style: authorStyle),
Container(height: 5),
Text.rich(
TextSpan(text: "分类 : ${categories.join(' ')}"),
style: TextStyle(
fontSize: 13,
color: Theme.of(context)
.textTheme
.bodyText1!
.color!
.withAlpha(0xCC),
),
),
Container(height: 5),
],
),
),
],
2021-09-29 23:57:09 +00:00
),
2021-10-23 02:59:18 +00:00
),
2022-07-09 07:11:26 +00:00
],
),
2021-09-29 23:57:09 +00:00
);
}
}