|
|
|
@ -1,10 +1,18 @@
|
|
|
|
|
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 {
|
|
|
|
|
var pageIds = RxList<int>();
|
|
|
|
|
bool syncRunning = false;
|
|
|
|
|
|
|
|
|
|
var favPageIds = RxList<int>();
|
|
|
|
|
|
|
|
|
|
void updateFromPageList(List<PageInfo> list) {
|
|
|
|
|
List<int> addList = [];
|
|
|
|
@ -19,14 +27,14 @@ class FavoriteListController extends GetxController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newPageIds = pageIds.where((pageId) => !removeList.contains(pageId)).toList();
|
|
|
|
|
var newPageIds = favPageIds.where((pageId) => !removeList.contains(pageId)).toList();
|
|
|
|
|
for (var pageId in addList) {
|
|
|
|
|
if (!newPageIds.contains(pageId)) {
|
|
|
|
|
newPageIds.add(pageId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pageIds.value = newPageIds;
|
|
|
|
|
favPageIds.value = newPageIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateFromWatchList(List<PageInfo> list) {
|
|
|
|
@ -36,29 +44,103 @@ class FavoriteListController extends GetxController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var pageId in addList) {
|
|
|
|
|
if (!pageIds.contains(pageId)) {
|
|
|
|
|
pageIds.add(pageId);
|
|
|
|
|
if (!favPageIds.contains(pageId)) {
|
|
|
|
|
favPageIds.add(pageId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isFavorite(PageInfo pageInfo) {
|
|
|
|
|
return pageIds.contains(pageInfo.pageid);
|
|
|
|
|
return favPageIds.contains(pageInfo.pageid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> setFavorite(PageInfo pageInfo, bool isFavorite, bool showToast) async {
|
|
|
|
|
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;
|
|
|
|
|
return !isFavorite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isFavorite) {
|
|
|
|
|
// 添加收藏
|
|
|
|
|
} else {
|
|
|
|
|
// 删除收藏
|
|
|
|
|
}
|
|
|
|
|
if (syncRunning) {
|
|
|
|
|
alert(Get.overlayContext!, "正在同步收藏列表,请稍后再试", title: "提示");
|
|
|
|
|
return !isFavorite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFavorite(pageInfo, isFavorite, true);
|
|
|
|
|
|
|
|
|
|
// 立即在UI上显示收藏动画
|
|
|
|
|
return isFavorite;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|