home_service_vm.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/garage_sale_rent_entity.dart';
  3. import 'package:domain/entity/newsfeed_detail_entity.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/load_state_layout.dart';
  10. import 'package:widgets/widget_export.dart';
  11. import '../../../respository/services_respository.dart';
  12. import '../../../router/page/services_page_router.dart';
  13. import 'home_service_state.dart';
  14. part 'home_service_vm.g.dart';
  15. @riverpod
  16. class HomeServiceVm extends _$HomeServiceVm {
  17. late ServicesRespository servicesRespositoryInstance;
  18. bool _needShowPlaceholder = false; //是否展示LoadingView
  19. int _page = 1; // 当前页
  20. int _limit = 10; // 每页数量
  21. int _count = 0; // 总条数
  22. Map<String, dynamic> _queryParams = {
  23. 'category_id': null,
  24. 'keyword': null,
  25. 'is_liked': null,
  26. };
  27. // Refresh 控制器
  28. final EasyRefreshController refreshController = EasyRefreshController(
  29. controlFinishRefresh: true, //允许刷新
  30. controlFinishLoad: true, //允许加载
  31. );
  32. HomeServiceState initState() {
  33. return HomeServiceState(
  34. list: []
  35. );
  36. }
  37. @override
  38. HomeServiceState build(){
  39. // 引入数据仓库
  40. // servicesRespositoryInstance = ref.read(commonGarageRespositoryProvider);
  41. final state = initState();
  42. Log.d("--------------------------build---------------------");
  43. return state;
  44. }
  45. //刷新页面状态
  46. void changeLoadingState(LoadState loadState, String? errorMsg) {
  47. state = state.copyWith(
  48. loadingState: loadState,
  49. errorMessage: errorMsg
  50. );
  51. }
  52. // 初始化页面数据
  53. initPageData() {
  54. Log.d("----home_service_vm-----initPageData ${state.loadingState}");
  55. onRefresh();
  56. }
  57. // 上拉加载 更多
  58. Future loadMore() async {
  59. Log.d("----home_service_vm-----loadMore");
  60. _page++;
  61. getListData();
  62. }
  63. // 下拉刷新
  64. Future onRefresh() async {
  65. // 当前pageView 页面正处于显示状态
  66. Log.d("----forsale_vm-----onRefresh ");
  67. // await Future.delayed(const Duration(seconds: 2));
  68. _page = 1;
  69. getListData();
  70. }
  71. // 手动进行刷新
  72. Future triggerRefresh() async {
  73. Log.d("trggerRefresh");
  74. refreshController.callRefresh();
  75. }
  76. // 手动进行刷新
  77. Future directRefresh() async {
  78. state = state.copyWith(list:[]);
  79. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  80. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  81. refreshController.callRefresh();
  82. refreshController.resetFooter();
  83. _page = 1;
  84. _needShowPlaceholder = true;
  85. getListData();
  86. }
  87. // 重试请求
  88. Future retryRequest() async {
  89. _page = 1;
  90. _needShowPlaceholder = true;
  91. getListData();
  92. }
  93. // 获取list 列表数据
  94. Future getListData<T>({bool? isLoadMore}) async {
  95. if (_needShowPlaceholder) {
  96. changeLoadingState(LoadState.State_Loading, null);
  97. }
  98. List<Map<String, dynamic>> list = [
  99. {
  100. 'id':1,
  101. 'service_type': 0, // 0 房屋保洁 1 空调保洁 2 维修
  102. 'goods_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  103. 'resources': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
  104. 'title': 'House Cleaning Services',
  105. 'price': '\$66',
  106. 'isCollection': true,
  107. 'collection_num': '12',
  108. 'publisher': 'William Jefferson',
  109. 'publish_time': 'June 17,2016 at 7:23 p.m.',
  110. 'publisher_avator': Assets.communityCamera
  111. },
  112. {
  113. 'id':1,
  114. 'service_type': 1, // 0 房屋保洁 1 空调保洁 2 维修
  115. 'goods_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  116. 'title': 'Electronic keyboard',
  117. 'price': '\$66',
  118. 'isCollection': true,
  119. 'collection_num': '12',
  120. 'publisher': 'William Jefferson',
  121. 'publish_time': 'June 17,2016 at 7:23 p.m.',
  122. 'publisher_avator': Assets.communityCamera
  123. },
  124. {
  125. 'id':1,
  126. 'service_type': 2, // 0 房屋保洁 1 空调保洁 2 维修
  127. 'goods_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  128. 'title': 'Electronic keyboard',
  129. 'price': '\$66',
  130. 'isCollection': true,
  131. 'collection_num': '12',
  132. 'publisher': 'William Jefferson',
  133. 'publish_time': 'June 17,2016 at 7:23 p.m.',
  134. 'publisher_avator': Assets.communityCamera
  135. },
  136. ];
  137. handlerResultData(true, list:list);
  138. // try {
  139. // //请求网络
  140. // Map<String, dynamic> params = {
  141. // "type": 1, // 类型(1=Sale,2=Rent)
  142. // "category_id": _queryParams['category_id'],
  143. // "keyword": _queryParams['keyword'],
  144. // "page": _page,
  145. // "limit": _limit,
  146. // };
  147. // Log.d("请求参数------$params");
  148. // final result = await servicesRespositoryInstance.fetchGarageDataList(params);
  149. // //校验成功失败
  150. // if (result.isSuccess) {
  151. // // handlerResultList((result.data as GarageSaleRentEntity).list as List<GarageSaleRentList>, isLoadMore ?? false);
  152. // } else {
  153. // String errorMessage = result.errorMsg!;
  154. // changeLoadingState(LoadState.State_Error, errorMessage);
  155. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  156. // }
  157. // } catch (e) {
  158. // ToastEngine.show("Error: $e");
  159. // }
  160. // 最后赋值
  161. _needShowPlaceholder = false;
  162. }
  163. handlerResultData(bool isList, {List<Map<String, dynamic>>? list, dynamic? data}){
  164. Future.delayed(const Duration(seconds: 1)).then((value) {
  165. if(isList){
  166. // list 数据模式
  167. if(list != null && list.isNotEmpty){
  168. if(_page == 1){
  169. state.list.clear();
  170. state.list!.addAll(list);
  171. refreshController.finishRefresh();
  172. changeLoadingState(LoadState.State_Success, null);
  173. }else {
  174. final allList = state.list;
  175. allList!.addAll(list);
  176. state = state.copyWith(list: allList);
  177. refreshController.finishLoad();
  178. }
  179. }else {
  180. if(_page == 1){
  181. state.list.clear();
  182. changeLoadingState(LoadState.State_Empty, null);
  183. refreshController.finishRefresh();
  184. }else {
  185. refreshController.finishLoad(IndicatorResult.noMore);
  186. }
  187. }
  188. }else {
  189. // 单个数据模式
  190. if(data!=null){
  191. if(_page == 1){
  192. refreshController.finishRefresh();
  193. }else{
  194. refreshController.finishLoad();
  195. }
  196. changeLoadingState(LoadState.State_Success, null);
  197. }else {
  198. if(_page == 1){
  199. refreshController.finishRefresh();
  200. }else{
  201. refreshController.finishLoad();
  202. }
  203. changeLoadingState(LoadState.State_Empty, null);
  204. }
  205. }
  206. });
  207. }
  208. void handlerResultList(List<GarageSaleRentList>? list, bool isLoadMore) {
  209. if (list != null && list.isNotEmpty) {
  210. //有数据,判断是刷新还是加载更多的数据
  211. if (_page == 1) {
  212. //刷新的方式
  213. state.list!.clear();
  214. state.list!.addAll(list.map((item) => item.toJson()).toList());
  215. refreshController.finishRefresh();
  216. //更新展示的状态
  217. changeLoadingState(LoadState.State_Success, null);
  218. } else {
  219. //加载更多
  220. final allList = state.list;
  221. allList!.addAll(list.map((item) => item.toJson()).toList());
  222. state = state.copyWith(list: allList);
  223. refreshController.finishLoad();
  224. }
  225. } else {
  226. if (_page == 1) {
  227. //展示无数据的布局
  228. state.list!.clear();
  229. changeLoadingState(LoadState.State_Empty, null);
  230. refreshController.finishRefresh();
  231. } else {
  232. //展示加载完成,没有更多数据了
  233. if (_page == 1) {
  234. //展示无数据的布局
  235. state.list!.clear();
  236. changeLoadingState(LoadState.State_Empty, null);
  237. refreshController.finishRefresh();
  238. } else {
  239. //展示加载完成,没有更多数据了
  240. if(state.list!.length == 0){
  241. changeLoadingState(LoadState.State_Empty, null);
  242. refreshController.finishLoad();
  243. }else {
  244. if(_needShowPlaceholder){
  245. changeLoadingState(LoadState.State_Success, null);
  246. }
  247. }
  248. //更新展示的状态
  249. refreshController.finishLoad(IndicatorResult.noMore);
  250. }
  251. }
  252. }
  253. }
  254. // 点击 收藏/取消收藏
  255. Future<bool?> handlerClickCollection(int id, bool? isCollection) async{
  256. try {
  257. //请求网络
  258. Map<String, dynamic> params = {
  259. "id": id,
  260. };
  261. Log.d("请求参数------$params");
  262. final result = await servicesRespositoryInstance.fetchGarageColleciton(params);
  263. //校验成功失败
  264. if (result.isSuccess) {
  265. // 修改 该id 的 liked 和 likes_count 字段
  266. state.list!.forEach((Map<String, dynamic> element) {
  267. GarageSaleRentList elementEntity = GarageSaleRentList.fromJson(element);
  268. if(elementEntity.id == id){
  269. elementEntity.liked = !elementEntity.liked!;
  270. elementEntity.likesCount = elementEntity.liked! ? (elementEntity.likesCount! + 1) : (elementEntity.likesCount! - 1);
  271. }
  272. element = elementEntity.toJson();
  273. });
  274. return true;
  275. } else {
  276. String errorMessage = result.errorMsg!;
  277. changeLoadingState(LoadState.State_Error, errorMessage);
  278. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  279. }
  280. } catch (e) {
  281. Log.e("Error: $e");
  282. ToastEngine.show("Error: $e");
  283. }
  284. }
  285. // 设置当前的 _queryParams
  286. setCurrentQueryParams(Map<String, dynamic> params){
  287. _queryParams.addAll(params);
  288. }
  289. // 获取当前的 _queryParams
  290. Map<String, dynamic> getCurrentQueryParams(String? key){
  291. if(key!=null && key!.isNotEmpty){
  292. return _queryParams[key];
  293. }
  294. return _queryParams;
  295. }
  296. // 去详情页面
  297. void handlerGotoDetail({BuildContext? context, required int id, String type='forSale'}){
  298. Log.d("去详情页面");
  299. // appRouter.push(GaragesaleDetailPageRoute(id: id, type: 'forSale'));
  300. }
  301. }