182 lines
5.3 KiB
Dart
182 lines
5.3 KiB
Dart
import 'package:easy_refresh/easy_refresh.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_kinetra/kt_model/kt_home_category_bean.dart';
|
|
import 'package:flutter_kinetra/kt_pages/kt_home/state.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../dio_cilent/kt_apis.dart';
|
|
import '../../dio_cilent/kt_request.dart';
|
|
import '../../kt_model/kt_short_video_bean.dart';
|
|
import '../../kt_widgets/kt_status_widget.dart';
|
|
|
|
class KtHomeLogic extends GetxController {
|
|
final state = KtHomeState();
|
|
|
|
final EasyRefreshController easyRefreshController = EasyRefreshController(
|
|
controlFinishRefresh: true,
|
|
controlFinishLoad: true,
|
|
);
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
refreshData();
|
|
}
|
|
|
|
refreshData() {
|
|
getHomeInfo();
|
|
getMostSearchList();
|
|
}
|
|
|
|
loadMoreData() {
|
|
if (state.selDiscover == 0) {
|
|
easyRefreshController.finishLoad(IndicatorResult.noMore);
|
|
return;
|
|
}
|
|
state.pageIndex++;
|
|
getCategoryVideoList();
|
|
}
|
|
|
|
getHomeInfo() async {
|
|
state.loadStatus = KtLoadStatusType.loading;
|
|
try {
|
|
ApiResponse res = await KtHttpClient().request(
|
|
KtApis.homeAllModules,
|
|
method: HttpMethod.get,
|
|
);
|
|
easyRefreshController.finishRefresh();
|
|
|
|
if (res.success) {
|
|
state.loadStatus = KtLoadStatusType.loadSuccess;
|
|
res.data['list'].forEach((item) {
|
|
if (item['module_key'] == 'home_banner') {
|
|
state.bannerList = [
|
|
...item['data']
|
|
.map(
|
|
(item) =>
|
|
KtShortVideoBean.fromJson(item as Map<String, dynamic>),
|
|
)
|
|
.toList(),
|
|
];
|
|
} else if (item['module_key'] == 'highest_payment_hot_video') {
|
|
state.topPickList = [
|
|
...item['data']
|
|
.map(
|
|
(item) =>
|
|
KtShortVideoBean.fromJson(item as Map<String, dynamic>),
|
|
)
|
|
.toList(),
|
|
];
|
|
} else if (item['module_key'] == 'new_recommand') {
|
|
state.arrivalList = [
|
|
...item['data']['list']
|
|
.map(
|
|
(item) =>
|
|
KtShortVideoBean.fromJson(item as Map<String, dynamic>),
|
|
)
|
|
.toList(),
|
|
];
|
|
} else if (item['module_key'] == 'week_highest_recommend') {
|
|
state.hotList = [
|
|
...item['data']
|
|
.map(
|
|
(item) =>
|
|
KtShortVideoBean.fromJson(item as Map<String, dynamic>),
|
|
)
|
|
.toList(),
|
|
];
|
|
} else if (item['module_key'] == 'category_navigation') {
|
|
state.categoryList = [
|
|
...item['data']
|
|
.map(
|
|
(item) => KtHomeCategoryBean.fromJson(
|
|
item as Map<String, dynamic>,
|
|
),
|
|
)
|
|
.toList(),
|
|
];
|
|
if (state.selCategoryId == -1) {
|
|
state.selCategoryId = state.categoryList.first.categoryId ?? -1;
|
|
}
|
|
getCategoryVideoList(isRefresh: true);
|
|
}
|
|
});
|
|
|
|
if (state.hotList.isEmpty &&
|
|
state.bannerList.isEmpty &&
|
|
state.arrivalList.isEmpty) {
|
|
state.loadStatus = KtLoadStatusType.loadNoData;
|
|
}
|
|
} else {
|
|
state.loadStatus = KtLoadStatusType.loadFailed;
|
|
}
|
|
update();
|
|
} catch (e) {
|
|
state.loadStatus = KtLoadStatusType.loadFailed;
|
|
update();
|
|
}
|
|
}
|
|
|
|
getMostSearchList() async {
|
|
ApiResponse res = await KtHttpClient().request(
|
|
KtApis.searchHot,
|
|
method: HttpMethod.get,
|
|
);
|
|
if (res.success) {
|
|
state.mostSearchedList = [
|
|
...res.data['list'].map((item) => KtShortVideoBean.fromJson(item)),
|
|
];
|
|
if (state.mostSearchedList.length > 3) {
|
|
state.mostSearchedList = state.mostSearchedList.sublist(0, 3);
|
|
}
|
|
update(['trend']);
|
|
}
|
|
}
|
|
|
|
getCategoryVideoList({bool isRefresh = false}) async {
|
|
state.categoryLoadStatus = KtLoadStatusType.loading;
|
|
if (isRefresh) {
|
|
state.pageIndex = 1;
|
|
state.categoryVideoList.clear();
|
|
update();
|
|
}
|
|
Map<String, dynamic> params = {
|
|
'current_page': state.pageIndex,
|
|
'page_size': 20,
|
|
};
|
|
if (state.selCategoryId != -1) {
|
|
params.putIfAbsent('category_id', () => state.selCategoryId);
|
|
}
|
|
|
|
ApiResponse res = await KtHttpClient().request(
|
|
KtApis.homeVideoList,
|
|
method: HttpMethod.get,
|
|
queryParameters: params,
|
|
);
|
|
easyRefreshController.finishRefresh();
|
|
easyRefreshController.finishLoad();
|
|
if (res.success) {
|
|
state.categoryLoadStatus = KtLoadStatusType.loadSuccess;
|
|
List<KtShortVideoBean> list = [
|
|
...res.data['list']
|
|
.map(
|
|
(item) => KtShortVideoBean.fromJson(item as Map<String, dynamic>),
|
|
)
|
|
.toList(),
|
|
];
|
|
if (list.length < 20) {
|
|
easyRefreshController.finishLoad(IndicatorResult.noMore);
|
|
}
|
|
state.categoryVideoList.addAll(list);
|
|
if (state.categoryVideoList.isEmpty) {
|
|
state.categoryLoadStatus = KtLoadStatusType.loadNoData;
|
|
}
|
|
|
|
update();
|
|
} else {
|
|
state.categoryLoadStatus = KtLoadStatusType.loadFailed;
|
|
update();
|
|
}
|
|
}
|
|
}
|