mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-19 16:46:22 +08:00
feat: fav topic
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -1,35 +1,47 @@
|
||||
import 'package:PiliPlus/http/dynamics.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/http/user.dart';
|
||||
import 'package:PiliPlus/models/dynamics/dyn_topic_feed/item.dart';
|
||||
import 'package:PiliPlus/models/dynamics/dyn_topic_feed/topic_card_list.dart';
|
||||
import 'package:PiliPlus/models/dynamics/dyn_topic_feed/topic_sort_by_conf.dart';
|
||||
import 'package:PiliPlus/models/dynamics/dyn_topic_top/top_details.dart';
|
||||
import 'package:PiliPlus/pages/common/common_list_controller.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart' show Accounts;
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class DynTopicController
|
||||
extends CommonListController<TopicCardList?, TopicCardItem> {
|
||||
final topicId = Get.parameters['id']!;
|
||||
final topicName = Get.parameters['name']!;
|
||||
String topicName = Get.parameters['name'] ?? '';
|
||||
|
||||
int sortBy = 0;
|
||||
String offset = '';
|
||||
Rx<TopicSortByConf?> topicSortByConf = Rx<TopicSortByConf?>(null);
|
||||
|
||||
// top
|
||||
// Rx<LoadingState<TopDetails?>> topState =
|
||||
// LoadingState<TopDetails?>.loading().obs;
|
||||
final isLogin = Accounts.main.isLogin;
|
||||
Rx<bool?> isFav = Rx<bool?>(null);
|
||||
Rx<bool?> isLike = Rx<bool?>(null);
|
||||
Rx<LoadingState<TopDetails?>> topState =
|
||||
LoadingState<TopDetails?>.loading().obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
// queryTop();
|
||||
queryTop();
|
||||
queryData();
|
||||
}
|
||||
|
||||
// Future<void> queryTop() async {
|
||||
// topState.value = await DynamicsHttp.topicTop(topicId: topicId);
|
||||
// }
|
||||
Future<void> queryTop() async {
|
||||
topState.value = await DynamicsHttp.topicTop(topicId: topicId);
|
||||
if (topState.value.isSuccess) {
|
||||
topicName = topState.value.data!.topicItem!.name!;
|
||||
isFav.value = topState.value.data!.topicItem!.isFav;
|
||||
isLike.value = topState.value.data!.topicItem!.isLike;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
List<TopicCardItem>? getDataList(TopicCardList? response) {
|
||||
@@ -45,14 +57,12 @@ class DynTopicController
|
||||
@override
|
||||
Future<void> onRefresh() {
|
||||
offset = '';
|
||||
queryTop();
|
||||
return super.onRefresh();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onReload() {
|
||||
// if (topState.value is! Success) {
|
||||
// queryTop();
|
||||
// }
|
||||
scrollController.jumpToTop();
|
||||
return super.onReload();
|
||||
}
|
||||
@@ -69,4 +79,44 @@ class DynTopicController
|
||||
this.sortBy = sortBy;
|
||||
onReload();
|
||||
}
|
||||
|
||||
Future<void> onFav() async {
|
||||
if (!isLogin) {
|
||||
SmartDialog.showToast('账号未登录');
|
||||
return;
|
||||
}
|
||||
bool isFav = this.isFav.value ?? false;
|
||||
var res = isFav
|
||||
? await UserHttp.delFavTopic(topicId)
|
||||
: await UserHttp.addFavTopic(topicId);
|
||||
if (res['status']) {
|
||||
if (isFav) {
|
||||
topState.value.data!.topicItem!.fav -= 1;
|
||||
} else {
|
||||
topState.value.data!.topicItem!.fav += 1;
|
||||
}
|
||||
this.isFav.value = !isFav;
|
||||
} else {
|
||||
SmartDialog.showToast(res['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> onLike() async {
|
||||
if (!isLogin) {
|
||||
SmartDialog.showToast('账号未登录');
|
||||
return;
|
||||
}
|
||||
bool isLike = this.isLike.value ?? false;
|
||||
var res = await UserHttp.likeTopic(topicId, isLike);
|
||||
if (res['status']) {
|
||||
if (isLike) {
|
||||
topState.value.data!.topicItem!.like -= 1;
|
||||
} else {
|
||||
topState.value.data!.topicItem!.like += 1;
|
||||
}
|
||||
this.isLike.value = !isLike;
|
||||
} else {
|
||||
SmartDialog.showToast(res['msg']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user