You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:isekai_wiki/api/mw/watch.dart';
import 'package:isekai_wiki/api/response/page_info.dart';
import 'package:isekai_wiki/api/response/watch.dart';
import 'package:isekai_wiki/models/user.dart';
import 'package:isekai_wiki/utils/dialog.dart';
import 'package:isekai_wiki/utils/error.dart';
class FavoriteListController extends GetxController {
bool syncRunning = false;
var favPageIds = RxList<int>();
void updateFromPageList(List<PageInfo> list) {
List<int> addList = [];
List<int> removeList = [];
for (var pageInfo in list) {
if (pageInfo.inwatchlist != null) {
if (pageInfo.inwatchlist!) {
addList.add(pageInfo.pageid);
} else {
removeList.add(pageInfo.pageid);
}
}
}
var newPageIds = favPageIds.where((pageId) => !removeList.contains(pageId)).toList();
for (var pageId in addList) {
if (!newPageIds.contains(pageId)) {
newPageIds.add(pageId);
}
}
favPageIds.value = newPageIds;
}
void updateFromWatchList(List<PageInfo> list) {
List<int> addList = [];
for (var pageInfo in list) {
addList.add(pageInfo.pageid);
}
for (var pageId in addList) {
if (!favPageIds.contains(pageId)) {
favPageIds.add(pageId);
}
}
}
bool isFavorite(PageInfo pageInfo) {
return favPageIds.contains(pageInfo.pageid);
}
Future<void> setFavorite(PageInfo pageInfo, bool isFavorite, bool showToast) async {
syncRunning = true;
List<WatchActionResponseList> watchRes = [];
if (isFavorite) {
// 添加收藏
try {
var res = await MWApiWatch.watchPage(pageIds: [pageInfo.pageid]);
watchRes = res.data;
// 将页面加入本地收藏列表
if (!favPageIds.contains(pageInfo.pageid)) {
favPageIds.add(pageInfo.pageid);
}
} catch (err, stack) {
alert(Get.overlayContext!, ErrorUtils.getErrorMessage(err), title: "错误");
if (kDebugMode) {
print("Exception in logout: $err");
stack.printError();
}
}
} else {
// 删除收藏
try {
var res = await MWApiWatch.unwatchPage(pageIds: [pageInfo.pageid]);
watchRes = res.data;
} catch (err, stack) {
alert(Get.overlayContext!, ErrorUtils.getErrorMessage(err), title: "错误");
if (kDebugMode) {
print("Exception in logout: $err");
stack.printError();
}
}
}
if (watchRes.isNotEmpty) {
if (watchRes[0].isWatched) {
// 将页面加入本地收藏列表
if (!favPageIds.contains(pageInfo.pageid)) {
favPageIds.add(pageInfo.pageid);
}
if (showToast) {
Fluttertoast.showToast(
msg: "已将《${pageInfo.displayTitle}》添加到收藏列表",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
fontSize: 16.0,
backgroundColor: CupertinoColors.black.withOpacity(0.8),
textColor: CupertinoColors.white,
);
}
} else {
// 将页面从本地收藏列表中移除
if (favPageIds.contains(pageInfo.pageid)) {
favPageIds.remove(pageInfo.pageid);
}
if (showToast) {
Fluttertoast.showToast(
msg: "已将《${pageInfo.displayTitle}》从收藏列表中移除",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
fontSize: 16.0,
backgroundColor: CupertinoColors.black.withOpacity(0.8),
textColor: CupertinoColors.white,
);
}
}
}
syncRunning = false;
}
/// 判断是否可以更改收藏,并异步提交
Future<bool> setFavoriteImmediate(PageInfo pageInfo, bool isFavorite) async {
// 如果未登录,则提示需要登录
var uc = Get.find<UserController>();
if (!uc.isLoggedIn) {
alert(Get.overlayContext!, "使用收藏功能需要登录", title: "提示");
return !isFavorite;
}
if (syncRunning) {
alert(Get.overlayContext!, "正在同步收藏列表,请稍后再试", title: "提示");
return !isFavorite;
}
setFavorite(pageInfo, isFavorite, true);
// 立即在UI上显示收藏动画
return isFavorite;
}
}