|
@@ -1,9 +1,15 @@
|
|
|
|
|
|
+import 'dart:async';
|
|
|
+
|
|
|
import 'package:cpt_community/modules/community/community_vm.dart';
|
|
|
import 'package:cs_resources/generated/assets.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
+import 'package:plugin_basic/constants/app_constant.dart';
|
|
|
+import 'package:plugin_platform/engine/sp/sp_util.dart';
|
|
|
import 'package:plugin_platform/engine/toast/toast_engine.dart';
|
|
|
+import 'package:plugin_platform/http/http_result.dart';
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
+import 'package:router/ext/auto_router_extensions.dart';
|
|
|
import 'package:router/path/router_path.dart';
|
|
|
import 'package:shared/utils/color_utils.dart';
|
|
|
import 'package:shared/utils/log_utils.dart';
|
|
@@ -129,33 +135,51 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- // state = state.copyWith(
|
|
|
- // saleSelectedOption:_initialSaleSelectedItem,
|
|
|
- // );
|
|
|
}
|
|
|
|
|
|
- Future getGarageCategoryList() async {
|
|
|
- final communityVm = ref.read(communityVmProvider.notifier);
|
|
|
- final result = await communityVm.getGarageSaleCategoryOptions();
|
|
|
- if (result.isSuccess) {
|
|
|
- // state.categoryOptions = result.data!;
|
|
|
- // 将 [{id: 1, name: Kids}, {id: 2, name: Homeware}, {id: 3, name: Fashion}, {id: 4, name: Electronics}, {id: 5, name: Sports}, {id: 6, name: Furniture}, {id: 7, name: Others}] 处理成
|
|
|
- // ['Kids', 'Homeware', 'Fashion', 'Electronics', 'Sports', 'Furniture', 'Others']
|
|
|
- final listJson = result.getListJson();
|
|
|
- _resCategoryOptionsList = listJson;
|
|
|
- Log.d("_resCategoryOptionsList $_resCategoryOptionsList");
|
|
|
-
|
|
|
- List<String> catList = (listJson as List)!.map((e) => (e['name'] as String)).toList()?? [];
|
|
|
-
|
|
|
-
|
|
|
- Log.d("--initialSelectedItem----$_initialSaleSelectedItem-");
|
|
|
-
|
|
|
- state = state.copyWith(
|
|
|
- saleSelectedOption:_initialSaleSelectedItem,
|
|
|
- categoryOptionsList: catList,
|
|
|
- );
|
|
|
+ Future<List<String>> getGarageCategoryList() async{
|
|
|
+ // 获取分类列表
|
|
|
+ List<String> catNameResult = [];
|
|
|
+ try {
|
|
|
+ // 加入有缓存 就取缓存
|
|
|
+ List<Map<String, dynamic>>? StorageCategoryList = SPUtil.getObjectList(AppConstant.storageGarageCategoryList)?.cast<Map<String, dynamic>>();
|
|
|
+
|
|
|
+ if(StorageCategoryList != null && StorageCategoryList.isNotEmpty){
|
|
|
+ Log.d("取StorageCategoryList 缓存: $StorageCategoryList ");
|
|
|
+ _resCategoryOptionsList = StorageCategoryList;
|
|
|
+ // 将 listJson 转换为 List<String>
|
|
|
+ catNameResult = (StorageCategoryList as List)!.map((e) => (e['name'] as String)).toList()?? [];;
|
|
|
+ }else {
|
|
|
+ final communityVm = ref.read(communityVmProvider.notifier);
|
|
|
+ final result = await communityVm.getGarageSaleCategoryOptions();
|
|
|
+ if (result.isSuccess) {
|
|
|
+ // state.categoryOptions = result.data!;
|
|
|
+ // 将 [{id: 1, name: Kids}, {id: 2, name: Homeware}, {id: 3, name: Fashion}, {id: 4, name: Electronics}, {id: 5, name: Sports}, {id: 6, name: Furniture}, {id: 7, name: Others}] 处理成
|
|
|
+ // ['Kids', 'Homeware', 'Fashion', 'Electronics', 'Sports', 'Furniture', 'Others']
|
|
|
+ final listJson = result.getListJson();
|
|
|
+ Log.d("_resCategoryOptionsList $_resCategoryOptionsList");
|
|
|
+ // 将 listJson 转换为 List<Map<String, dynamic>>
|
|
|
+ List<Map<String, dynamic>> garageCategoryList = (listJson as List?)?.map((item) => item as Map<String, dynamic>).toList() ?? [];
|
|
|
+ _resCategoryOptionsList = garageCategoryList;
|
|
|
+ // 将 garageCategoryList 存入缓存
|
|
|
+ Log.d("设置StorageCategoryList 缓存");
|
|
|
+ SPUtil.putObjectList(AppConstant.storageGarageCategoryList, garageCategoryList);
|
|
|
+
|
|
|
+ // 将 listJson 转换为 List<String>
|
|
|
+ List<String> catNameResult = (listJson as List)!.map((e) => (e['name'] as String)).toList()?? [];
|
|
|
+
|
|
|
+ Log.d("--initialSelectedItem----$_initialSaleSelectedItem-");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ state = state.copyWith(
|
|
|
+ saleSelectedOption:_initialSaleSelectedItem,
|
|
|
+ categoryOptionsList: catNameResult,
|
|
|
+ );
|
|
|
+ }catch(error){
|
|
|
+ Log.d("------获取分类列表error---------- $error");
|
|
|
}
|
|
|
- return result;
|
|
|
+
|
|
|
+ return catNameResult;
|
|
|
}
|
|
|
|
|
|
//catogery 选择选项
|
|
@@ -253,7 +277,7 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
}
|
|
|
|
|
|
///提交反馈
|
|
|
- void submitGaragesalePost(BuildContext context, {VoidCallback? sCallback, VoidCallback? fCallback}) {
|
|
|
+ Future submitGaragesalePost(BuildContext context, {VoidCallback? sCallback, VoidCallback? fCallback}) async{
|
|
|
Log.d("GaragesalePostPageState 提交表单");
|
|
|
state = state.copyWith(titleErrorText: null, descriptionErrorText: null);
|
|
|
_dismissKeyboard(keyStr: "");
|
|
@@ -261,6 +285,7 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
// 获取表单的值
|
|
|
String saleValue= state.saleSelectedOption ?? "";
|
|
|
String categoryValue= state.categorySelectedOption ?? "";
|
|
|
+ int categoryId;
|
|
|
String contactTypeValue= state.contactTypeSelectedOption ?? "";
|
|
|
|
|
|
String titleValue= _getFormFieldValue(keyStr: 'title');
|
|
@@ -278,6 +303,17 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
if (Utils.isEmpty(categoryValue)) {
|
|
|
ToastEngine.show('Category is empty');
|
|
|
return;
|
|
|
+ }else {
|
|
|
+ // 通过 categoryValue 值 来获取 categoryId
|
|
|
+ Log.d("_resCategoryOptionsList $_resCategoryOptionsList");
|
|
|
+ try {
|
|
|
+ Map<String, dynamic>? categoryItem = _resCategoryOptionsList.firstWhere((element) => element['name'] == categoryValue, orElse: () => {'id': -1});
|
|
|
+ categoryId = categoryItem?['id'];
|
|
|
+ } catch (e) {
|
|
|
+ Log.d("未找到匹配的 categoryValue: $categoryValue");
|
|
|
+ ToastEngine.show('Category is invalid');
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
if (Utils.isEmpty(contactTypeValue)) {
|
|
|
ToastEngine.show('ContactType is empty');
|
|
@@ -325,18 +361,25 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
}
|
|
|
|
|
|
// 提交数据
|
|
|
- handlerSubmitPost(
|
|
|
+ final resResult = await handlerSubmitPost(
|
|
|
type: saleValue == 'For Sale'? 1:2,
|
|
|
titleValue: titleValue,
|
|
|
- categoryId: 1,
|
|
|
+ categoryId: categoryId!,
|
|
|
priceValue: priceValue,
|
|
|
descriptionValue: descriptionValue,
|
|
|
contactValue: totalContactStr,
|
|
|
imgList: state.imgList,
|
|
|
);
|
|
|
+
|
|
|
+ if(resResult.isSuccess){
|
|
|
+ final communitVm = ref.read(communityVmProvider.notifier);
|
|
|
+ communitVm.getCurrentPageViewVm(null).initPageData();
|
|
|
+ // Navigator.pop(context);
|
|
|
+ appRouter.maybePop();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- Future handlerSubmitPost({
|
|
|
+ Future<HttpResult<Object>> handlerSubmitPost({
|
|
|
required int type,
|
|
|
required String titleValue,
|
|
|
required int categoryId,
|
|
@@ -345,17 +388,17 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
required String descriptionValue,
|
|
|
required List imgList,
|
|
|
}) async{
|
|
|
- final params = <String, dynamic>{
|
|
|
- "type": type, // 1 = Sale 2 = Rent
|
|
|
- "category_id": categoryId,
|
|
|
- "title": titleValue,
|
|
|
- "price": priceValue,
|
|
|
- "description": descriptionValue,
|
|
|
- "contact": contactValue, // string($例如:whatsapp:+8617671757687 或 mobile:+8617671757687)
|
|
|
- "resources": imgList,
|
|
|
- };
|
|
|
- final result = await commonGarageRespositoryInstance.fetchPublishGarage(params);
|
|
|
- return result;
|
|
|
+ final params = <String, dynamic>{
|
|
|
+ "type": type, // 1 = Sale 2 = Rent
|
|
|
+ "category_id": categoryId,
|
|
|
+ "title": titleValue,
|
|
|
+ "price": priceValue,
|
|
|
+ "description": descriptionValue,
|
|
|
+ "contact": contactValue, // string($例如:whatsapp:+8617671757687 或 mobile:+8617671757687)
|
|
|
+ "resources": imgList,
|
|
|
+ };
|
|
|
+ final result = await commonGarageRespositoryInstance.fetchPublishGarage(params);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -402,6 +445,4 @@ class GaragesalePostVm extends _$GaragesalePostVm {
|
|
|
|
|
|
Log.d("GaragesalePostPageState 销毁 onDispose");
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|