feat: 观看记录查询

This commit is contained in:
guozhigq
2023-06-05 17:22:29 +08:00
parent 92f77d2d65
commit d1aa0144c9
11 changed files with 506 additions and 10 deletions

View File

@ -0,0 +1,44 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/http/user.dart';
import 'package:pilipala/models/user/history.dart';
class HistoryController extends GetxController {
final ScrollController scrollController = ScrollController();
RxList<HisListItem> historyList = [HisListItem()].obs;
bool isLoadingMore = false;
@override
void onInit() {
queryHistoryList();
super.onInit();
}
Future queryHistoryList({type = 'init'}) async {
int max = 0;
int viewAt = 0;
if (type == 'onload') {
max = historyList.last.history!.oid!;
viewAt = historyList.last.viewAt!;
}
isLoadingMore = true;
var res = await UserHttp.historyList(max, viewAt);
isLoadingMore = false;
if (res['status']) {
if (type == 'onload') {
historyList.addAll(res['data'].list);
} else {
historyList.value = res['data'].list;
}
}
return res;
}
Future onLoad() async {
queryHistoryList(type: 'onload');
}
Future onRefresh() async {
queryHistoryList(type: 'onRefresh');
}
}

View File

@ -0,0 +1,4 @@
library history;
export './controller.dart';
export './view.dart';

View File

@ -0,0 +1,94 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/common/skeleton/video_card_h.dart';
import 'package:pilipala/common/widgets/http_error.dart';
import 'package:pilipala/pages/history/index.dart';
import 'widgets/item.dart';
class HistoryPage extends StatefulWidget {
const HistoryPage({super.key});
@override
State<HistoryPage> createState() => _HistoryPageState();
}
class _HistoryPageState extends State<HistoryPage> {
final HistoryController _historyController = Get.put(HistoryController());
Future? _futureBuilderFuture;
@override
void initState() {
_futureBuilderFuture = _historyController.queryHistoryList();
super.initState();
_historyController.scrollController.addListener(
() {
if (_historyController.scrollController.position.pixels >=
_historyController.scrollController.position.maxScrollExtent -
300) {
if (!_historyController.isLoadingMore) {
_historyController.onLoad();
}
}
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('观看记录'),
centerTitle: false,
),
body: RefreshIndicator(
onRefresh: () async {
await _historyController.onRefresh();
return;
},
child: CustomScrollView(
controller: _historyController.scrollController,
slivers: [
FutureBuilder(
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map data = snapshot.data;
if (data['status']) {
return Obx(
() => SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return HistoryItem(
videoItem: _historyController.historyList[index],
);
}, childCount: _historyController.historyList.length),
),
);
} else {
return HttpError(
errMsg: data['msg'],
fn: () => setState(() {}),
);
}
} else {
// 骨架屏
return SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return const VideoCardHSkeleton();
}, childCount: 10),
);
}
},
),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).padding.bottom + 10,
),
)
],
),
),
);
}
}

View File

@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/common/constants.dart';
import 'package:pilipala/common/widgets/network_img_layer.dart';
import 'package:pilipala/utils/utils.dart';
class HistoryItem extends StatelessWidget {
var videoItem;
HistoryItem({super.key, required this.videoItem});
@override
Widget build(BuildContext context) {
int aid = videoItem.history.oid;
String heroTag = Utils.makeHeroTag(aid);
return InkWell(
onTap: () async {
await Future.delayed(const Duration(milliseconds: 200));
Get.toNamed('/video?aid=$aid',
arguments: {'heroTag': heroTag, 'pic': videoItem.cover});
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(
StyleString.cardSpace, 7, StyleString.cardSpace, 7),
child: LayoutBuilder(
builder: (context, boxConstraints) {
double width =
(boxConstraints.maxWidth - StyleString.cardSpace * 6) / 2;
return SizedBox(
height: width / StyleString.aspectRatio,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AspectRatio(
aspectRatio: StyleString.aspectRatio,
child: LayoutBuilder(
builder: (context, boxConstraints) {
double maxWidth = boxConstraints.maxWidth;
double maxHeight = boxConstraints.maxHeight;
double PR = MediaQuery.of(context).devicePixelRatio;
return Stack(
children: [
Hero(
tag: heroTag,
child: NetworkImgLayer(
// src: videoItem['pic'] +
// '@${(maxWidth * 2).toInt()}w',
src: videoItem.cover + '@.webp',
width: maxWidth,
height: maxHeight,
),
),
Positioned(
right: 4,
bottom: 4,
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 1, horizontal: 6),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.black54.withOpacity(0.4)),
child: Text(
Utils.timeFormat(videoItem.duration!),
style: const TextStyle(
fontSize: 11, color: Colors.white),
),
),
)
],
);
},
),
),
VideoContent(videoItem: videoItem)
],
),
);
},
),
),
Divider(
height: 1,
indent: 8,
endIndent: 12,
color: Theme.of(context).dividerColor.withOpacity(0.08),
)
],
),
);
}
}
class VideoContent extends StatelessWidget {
final videoItem;
const VideoContent({super.key, required this.videoItem});
@override
Widget build(BuildContext context) {
return Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 2, 6, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
videoItem.title,
textAlign: TextAlign.start,
style: TextStyle(
fontSize: Theme.of(context).textTheme.titleSmall!.fontSize,
fontWeight: FontWeight.w500),
maxLines: videoItem.videos > 1 ? 1 : 2,
overflow: TextOverflow.ellipsis,
),
if (videoItem.videos > 1)
Text(
videoItem.showTitle,
textAlign: TextAlign.start,
style: TextStyle(
fontSize: Theme.of(context).textTheme.titleSmall!.fontSize,
fontWeight: FontWeight.w400,
color: Theme.of(context).colorScheme.outline),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const Spacer(),
Row(
children: [
Text(
videoItem.authorName,
style: TextStyle(
fontSize: Theme.of(context).textTheme.labelSmall!.fontSize,
color: Theme.of(context).colorScheme.outline,
),
),
],
),
Row(
children: [
Text(
Utils.dateFormat(videoItem.viewAt!),
style: TextStyle(
fontSize: 11,
color: Theme.of(context).colorScheme.outline),
)
],
),
],
),
),
);
}
}