Files
pilipala/lib/pages/rcmd/controller.dart
2024-02-05 00:19:03 +08:00

110 lines
3.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/http/video.dart';
import 'package:pilipala/models/home/rcmd/result.dart';
import 'package:pilipala/models/model_rec_video_item.dart';
import 'package:pilipala/utils/storage.dart';
class RcmdController extends GetxController {
final ScrollController scrollController = ScrollController();
int _currentPage = 0;
// RxList<RecVideoItemAppModel> appVideoList = <RecVideoItemAppModel>[].obs;
// RxList<RecVideoItemModel> webVideoList = <RecVideoItemModel>[].obs;
bool isLoadingMore = true;
OverlayEntry? popupDialog;
Box setting = GStrorage.setting;
RxInt crossAxisCount = 2.obs;
late bool enableSaveLastData;
late String defaultRcmdType = 'web';
late RxList<dynamic> videoList;
@override
void onInit() {
super.onInit();
crossAxisCount.value =
setting.get(SettingBoxKey.customRows, defaultValue: 2);
enableSaveLastData =
setting.get(SettingBoxKey.enableSaveLastData, defaultValue: false);
defaultRcmdType =
setting.get(SettingBoxKey.defaultRcmdType, defaultValue: 'web');
if (defaultRcmdType == 'web') {
videoList = <RecVideoItemModel>[].obs;
} else {
videoList = <RecVideoItemAppModel>[].obs;
}
}
// 获取推荐
Future queryRcmdFeed(type) async {
if (isLoadingMore == false) {
return;
}
if (type == 'onRefresh') {
_currentPage = 0;
}
late final Map<String, dynamic> res;
switch (defaultRcmdType) {
case 'app':
case 'notLogin':
res = await VideoHttp.rcmdVideoListApp(
loginStatus: defaultRcmdType != 'notLogin',
freshIdx: _currentPage,
);
break;
default: //'web'
res = await VideoHttp.rcmdVideoList(
freshIdx: _currentPage,
ps: 20,
);
}
if (res['status']) {
if (type == 'init') {
if (videoList.isNotEmpty) {
videoList.addAll(res['data']);
} else {
videoList.value = res['data'];
}
} else if (type == 'onRefresh') {
if (enableSaveLastData) {
videoList.insertAll(0, res['data']);
} else {
videoList.value = res['data'];
}
} else if (type == 'onLoad') {
videoList.addAll(res['data']);
}
_currentPage += 1;
// 若videoList数量太小可能会影响翻页此时再次请求
// 为避免请求到的数据太少时还在反复请求要求本次返回数据大于1条才触发
if (res['data'].length > 1 && videoList.length < 10) {
queryRcmdFeed('onLoad');
}
}
isLoadingMore = false;
return res;
}
// 下拉刷新
Future onRefresh() async {
isLoadingMore = true;
queryRcmdFeed('onRefresh');
}
// 上拉加载
Future onLoad() async {
queryRcmdFeed('onLoad');
}
// 返回顶部
void animateToTop() async {
if (scrollController.offset >=
MediaQuery.of(Get.context!).size.height * 5) {
scrollController.jumpTo(0);
} else {
await scrollController.animateTo(0,
duration: const Duration(milliseconds: 500), curve: Curves.easeInOut);
}
}
}