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.

63 lines
1.6 KiB
Dart

2 years ago
import 'package:get/get.dart';
import 'package:isekai_wiki/api/response/page_info.dart';
import 'package:isekai_wiki/models/user.dart';
import 'package:isekai_wiki/utils/dialog.dart';
class FavoriteListController extends GetxController {
var pageIds = 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 =
pageIds.where((pageId) => !removeList.contains(pageId)).toList();
for (var pageId in addList) {
if (!newPageIds.contains(pageId)) {
newPageIds.add(pageId);
}
}
pageIds.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 (!pageIds.contains(pageId)) {
pageIds.add(pageId);
}
}
}
bool isFavorite(PageInfo pageInfo) {
return pageIds.contains(pageInfo.pageid);
}
Future<bool> setFavorite(
PageInfo pageInfo, bool isFavorite, bool showToast) async {
// 如果未登录,则提示需要登录
var uc = Get.find<UserController>();
if (!uc.isLoggedIn) {
var result = await confirm(Get.overlayContext!, "使用收藏功能需要登录",
title: "提示", positiveText: "登录");
return false;
}
return false;
}
}