You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
5.4 KiB
192 lines
5.4 KiB
import 'package:dating_touchme_app/model/home/marriage_data.dart';
|
|
import 'package:dating_touchme_app/network/home_api.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
|
|
import '../../model/home/post_data.dart';
|
|
|
|
class TimelineController extends GetxController {
|
|
// 推荐列表数据
|
|
final recommendFeed = <MarriageData>[].obs;
|
|
// 同城列表数据
|
|
final nearbyFeed = <MarriageData>[].obs;
|
|
|
|
// 推荐列表的加载状态和分页信息
|
|
final recommendIsLoading = false.obs;
|
|
final recommendPage = 1.obs;
|
|
final recommendHasMore = true.obs;
|
|
|
|
// 同城列表的加载状态和分页信息
|
|
final nearbyIsLoading = false.obs;
|
|
final nearbyPage = 1.obs;
|
|
final nearbyHasMore = true.obs;
|
|
|
|
// 当前标签页索引
|
|
final selectedTabIndex = 0.obs;
|
|
|
|
final topTab = 0.obs;
|
|
|
|
final timelineTab = 0.obs;
|
|
|
|
// 分页大小
|
|
final pageSize = 10;
|
|
|
|
// 从GetX依赖注入中获取HomeApi实例
|
|
late final HomeApi _homeApi;
|
|
|
|
final page = 1.obs;
|
|
final size = 10.obs;
|
|
|
|
final hasMore = true.obs;
|
|
|
|
final postList = <Records>[].obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// 从全局依赖中获取HomeApi
|
|
_homeApi = Get.find<HomeApi>();
|
|
loadPostList();
|
|
}
|
|
|
|
|
|
|
|
clearData() async {
|
|
final blockList = GetStorage().read('blockList') ?? [];
|
|
final result1 = postList
|
|
.where((user) => !blockList.contains(user.userId))
|
|
.toList();
|
|
postList.value = result1;
|
|
update();
|
|
}
|
|
|
|
loadPostList() async {
|
|
if (recommendIsLoading.value || !recommendHasMore.value) return;
|
|
try{
|
|
recommendIsLoading.value = true;
|
|
final response = await _homeApi.userPagePost(
|
|
pageNum: page.value,
|
|
pageSize: size.value,
|
|
);
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data?.records ?? [];
|
|
final blockList = GetStorage().read('blockList') ?? [];
|
|
final result1 = data
|
|
.where((user) => !blockList.contains(user.userId))
|
|
.toList();
|
|
|
|
postList.addAll(result1);
|
|
if((data.length ?? 0) == size.value){
|
|
hasMore.value = true;
|
|
} else {
|
|
hasMore.value = false;
|
|
}
|
|
} else {
|
|
|
|
// 响应失败,抛出异常
|
|
throw Exception(response.data.message ?? '获取数据失败');
|
|
}
|
|
} catch(e) {
|
|
print('帖子列表获取失败: $e');
|
|
SmartDialog.showToast('帖子列表获取失败');
|
|
rethrow;
|
|
} finally {
|
|
|
|
recommendIsLoading.value = false;
|
|
}
|
|
}
|
|
|
|
/// 设置当前标签页
|
|
void setSelectedTabIndex(int index) {
|
|
print('Setting selected tab index to: $index');
|
|
selectedTabIndex.value = index;
|
|
// 确保UI能够更新
|
|
update();
|
|
}
|
|
|
|
|
|
void setTopTab(int index) {
|
|
print('Setting selected tab index to: $index');
|
|
topTab.value = index;
|
|
// 确保UI能够更新
|
|
update();
|
|
}
|
|
|
|
void setTimelineTab(int index) {
|
|
print('Setting selected tab index to: $index');
|
|
timelineTab.value = index;
|
|
// 确保UI能够更新
|
|
update();
|
|
}
|
|
|
|
/// 获取当前标签页的列表数据
|
|
List<MarriageData> getFeedListByTab(int tabIndex) {
|
|
return tabIndex == 0 ? List.from(recommendFeed) : List.from(nearbyFeed);
|
|
}
|
|
|
|
/// 私有方法:获取婚姻数据(统一的数据获取逻辑)
|
|
/// 返回包含records(数据列表)、current(当前页)、pages(总页数)的Map
|
|
Future<Map<String, dynamic>> _fetchMarriageData({
|
|
required int pageNum,
|
|
required int type,
|
|
}) async {
|
|
try {
|
|
print('_fetchMarriageData - pageNum: $pageNum, pageSize: $pageSize, type: $type');
|
|
// 调用API获取数据
|
|
var response = await _homeApi.getMarriageList(
|
|
pageNum: pageNum,
|
|
pageSize: pageSize,
|
|
type: type,
|
|
);
|
|
|
|
if (response.data.isSuccess) {
|
|
final paginatedData = response.data.data;
|
|
|
|
// 检查data是否为空
|
|
if (paginatedData == null) {
|
|
return {
|
|
'records': <MarriageData>[],
|
|
'current': pageNum,
|
|
'pages': 1,
|
|
'total': 0,
|
|
'size': pageSize,
|
|
};
|
|
}
|
|
|
|
// data 是 PaginatedResponse<dynamic>,直接使用其属性
|
|
// records 中的每个项是 dynamic,需要转换为 MarriageData
|
|
final allRecords = paginatedData.records
|
|
.map((item) => MarriageData.fromJson(item as Map<String, dynamic>))
|
|
.toList();
|
|
|
|
// 过滤掉直播类型的项
|
|
final records = allRecords.where((item) => !item.isLive).toList();
|
|
|
|
print('_fetchMarriageData 返回 - 请求页码: $pageNum, 返回当前页: ${paginatedData.current}, 总页数: ${paginatedData.pages}, 原始记录数: ${allRecords.length}, 过滤后记录数: ${records.length}');
|
|
|
|
return {
|
|
'records': records,
|
|
'current': paginatedData.current,
|
|
'pages': paginatedData.pages,
|
|
'total': paginatedData.total,
|
|
'size': paginatedData.size,
|
|
};
|
|
} else {
|
|
// 响应失败,抛出异常
|
|
throw Exception(response.data.message);
|
|
}
|
|
} catch (e) {
|
|
// 向上抛出异常,让调用方处理
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
/// 私有方法:统一的错误处理
|
|
void _handleError(String logMessage, dynamic error, String toastMessage) {
|
|
// 打印错误日志
|
|
print('$logMessage: $error');
|
|
// 显示错误提示
|
|
SmartDialog.showToast(toastMessage);
|
|
}
|
|
}
|