import 'package:easy_refresh/easy_refresh.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_kinetra/kt_utils/kt_string_extend.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'; import '../kt_routes.dart'; class KtHomeLogic extends GetxController { final state = KtHomeState(); final EasyRefreshController easyRefreshController = EasyRefreshController( controlFinishRefresh: true, controlFinishLoad: true, ); @override void onReady() { super.onReady(); refreshData(); getReceiveDayCoin(); } 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), ) .toList(), ]; } else if (item['module_key'] == 'highest_payment_hot_video') { state.topPickList = [ ...item['data'] .map( (item) => KtShortVideoBean.fromJson(item as Map), ) .toList(), ]; } else if (item['module_key'] == 'new_recommand') { state.arrivalList = [ ...item['data']['list'] .map( (item) => KtShortVideoBean.fromJson(item as Map), ) .toList(), ]; } else if (item['module_key'] == 'week_highest_recommend') { state.hotList = [ ...item['data'] .map( (item) => KtShortVideoBean.fromJson(item as Map), ) .toList(), ]; } else if (item['module_key'] == 'category_navigation') { state.categoryList = [ ...item['data'] .map( (item) => KtHomeCategoryBean.fromJson( item as Map, ), ) .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 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 list = [ ...res.data['list'] .map( (item) => KtShortVideoBean.fromJson(item as Map), ) .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(); } } getReceiveDayCoin() async { ApiResponse res = await KtHttpClient().request( KtApis.getReceiveDayCoin, method: HttpMethod.get, ); if (res.success) { state.receiveCoin = res.data['coins']; state.hasSubCoin = res.data['is_exist_sub'] == 1 && state.receiveCoin > 0; if (state.hasSubCoin) { Get.dialog( Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( width: 301.w, height: 260.w, padding: EdgeInsets.only(top: 140.w), decoration: BoxDecoration( image: DecorationImage( image: AssetImage('home_coins_bg.png'.ktIcon), fit: BoxFit.fill, ), ), child: Column( children: [ Text( 'Daily Reward', style: TextStyle( color: Color(0xFFFF4306), fontSize: 22.sp, fontWeight: FontWeight.w900, ), ), Text( 'is waiting!', style: TextStyle( color: Colors.black, fontSize: 20.sp, fontWeight: FontWeight.w700, ), ), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset('ic_coin.png'.ktIcon, width: 28.w), SizedBox(width: 4.w), Text( '+${state.receiveCoin}', style: TextStyle( color: Color(0xFFFF9500), fontSize: 22.sp, fontWeight: FontWeight.w700, ), ), ], ), ], ), ), GestureDetector( onTap: () { Get.back(); Get.toNamed(KtRoutes.refill); }, child: Container( width: 200.w, height: 50.w, margin: EdgeInsets.only(bottom: 12.w), decoration: BoxDecoration( image: DecorationImage( image: AssetImage('home_coins_btn.png'.ktIcon), fit: BoxFit.fill, ), ), child: Center( child: Text( 'Claim Now', style: TextStyle( color: Colors.white, fontSize: 18.sp, fontWeight: FontWeight.w700, ), ), ), ), ), GestureDetector( onTap: () => Get.back(), child: Image.asset('home_coins_close.png'.ktIcon, width: 24.w), ), ], ), ); } } } }