import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:discover_module/ui_screen/interactionform/interactionprovider.dart'; import 'package:discover_module/ui_screen/interactionform/model/interaction_config_data.dart'; import 'package:discover_module/ui_screen/interactionform/model/interaction_data.dart'; import 'package:discover_module/ui_screen/interactionform/model/json_form_data.dart'; import 'package:discover_module/ui_screen/interactionform/model/location_model.dart'; import 'package:discover_module/ui_screen/interactionform/model/save_interaction.dart'; import 'package:discover_module/ui_screen/interactionform/repository/hive_repository.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:hive_flutter/hive_flutter.dart'; class ViewInteractionProvider extends ChangeNotifier { List interactionReponseList = []; // List sectionList = []; List sectionList = []; late Location locationList; List textEditingControllerList = []; List multipletextEditingControllerList = []; int textfieldIndex = 0; List countryList = []; List stateList = []; List cityList = []; List checkboxlist = []; String selectedCountry = 'Selected Country'; String radioValue = ''; bool checkboxValue = false; late SaveInteraction saveInteraction; List saveData = []; List newList = []; String sectionName = ''; late String selectedCity = 'Selected City', selectedState = 'Selected State'; String customdropdownValue = ''; String? selectedValue; List selectedItems = []; InputClass? selectedObj; List savedList = []; List intConfigDataList = []; String? intId, intName; final HiveDataRepository _hiveprovider = HiveDataRepository( Hive.box('InteractionConfigDataBox')); late final InteractionProvider interactionProvider; initConfigData() async { intConfigDataList = _hiveprovider.getAllDataFromHive(); notifyListeners(); } Future getRecords(String formname) async { var box = await Hive.openBox('InteractionDataBox'); savedList = box.values.toList(); savedList = savedList .where( (element) => element.form == formname, ) .toList(); notifyListeners(); } Future> getAllRecords() async { var box = await Hive.openBox('InteractionDataBox'); List savedList = box.values.toList(); return savedList; } init(int index) async { // _hiveprovider = HiveDataRepository( // Hive.box('InteractionConfigDataBox')); // intConfigDataList = _hiveprovider.getAllDataFromHive(); InteractionConfigData interactionConfigData = intConfigDataList[index]; intId = intConfigDataList[index].id; intName = intConfigDataList[index].name; print("data $intConfigDataList"); // await fetchSaveDataJson(); // await fetchDataSet(); await fetchData(interactionConfigData.widgets); await fetchLocationData(); } initSavedForm(SaveInteraction saveInteractiondata) { intConfigDataList .indexWhere((element) => element.id == saveInteractiondata.intId); interactionReponseList = saveInteractiondata.save .map((e) => FormFieldData( multipleList: e.multipleList == null ? [] : e.multipleList! .map((mobj) => SectionList( depid: mobj.depid, id: mobj.id, inputList: mobj.inputList, isRequired: mobj.isRequired, extension: mobj.extension, fileName: mobj.fileName, selectedObject: mobj.selectedObject, name: mobj.name, param: mobj.param, selectedValue: mobj.selectedValue, widget: mobj.widget, controller: mobj.controller, gid: mobj.gid, input: mobj.input, selectedId: mobj.selectedId, value: mobj.value)) .toList(), sectionList: e.sectionList .map((obj) => SectionList( depid: obj.depid, id: obj.id, inputList: obj.inputList, isRequired: obj.isRequired, extension: obj.extension, fileName: obj.fileName, name: obj.name, param: obj.param, selectedObject: obj.selectedObject, selectedValue: obj.selectedValue, widget: obj.widget, controller: obj.controller, gid: obj.gid, input: obj.input, selectedId: obj.selectedId, value: obj.value)) .toList(), sectionName: e.sectionName, multiple: e.multiple)) .toList(); textEditingControllerList.clear(); for (var item in interactionReponseList) { sectionList = item.sectionList; for (SectionList obj in item.multipleList!) { obj.gid = obj.gid ?? item.multipleList!.length; if (obj.widget == InteractionWidget.TEXT) { debugPrint("textcontr:${obj.gid}"); debugPrint("textcontr:${obj.selectedValue}"); // debugPrint("textcontr:${obj.controller!.value}"); var textEditingController = TextEditingController(); textEditingController.value = TextEditingValue( text: obj.selectedValue!.isNotEmpty ? obj.selectedValue!.last ?? "" : ""); textEditingController.text = obj.selectedValue!.isNotEmpty ? obj.selectedValue!.last ?? "" : ""; multipletextEditingControllerList.add(textEditingController); // obj.controller!.value = textEditingController.value; obj.controller = textEditingController; // obj.controller = obj.selectedValue.last ?? " "; } if (obj.widget == InteractionWidget.DROPDOWN || obj.widget == InteractionWidget.AUTOCOMPLETE || obj.widget == InteractionWidget.MULTISELECT) { List list = obj.inputList!; if (obj.selectedObject != null) { } else { obj.selectedObject = obj.selectedValue!.isNotEmpty ? getDataObject(obj.id, obj.selectedValue!.last, list) : list[0]; } print("value : ${list.first.name} "); } if (obj.widget == InteractionWidget.CHECKBOX) { List selectedvalues = []; if (obj.selectedValue!.isNotEmpty) { for (var id in obj.selectedValue!) { int ind = obj.inputList!.indexWhere((element) => element.id == id); if (ind != -1) { obj.inputList![ind].ischecked = true; selectedvalues.add(obj.inputList![ind].name); } } } } if (obj.widget == InteractionWidget.RADIO) { List list = obj.inputList!; if (obj.selectedValue!.isNotEmpty) { int ind = list .indexWhere((element) => element.id == obj.selectedValue!.last); if (ind != -1) { obj.inputList![ind].ischecked = true; radioValue = obj.inputList![ind].name; } } } // newList.add(obj); } for (var sectionItem in item.sectionList) { if (sectionItem.widget == InteractionWidget.TEXT) { var textEditingController = TextEditingController(); textEditingController.text = sectionItem.selectedValue!.isNotEmpty ? sectionItem.selectedValue!.last ?? " " : " "; textEditingControllerList.add(textEditingController); sectionItem.controller = textEditingControllerList.last; } if (sectionItem.widget == InteractionWidget.DROPDOWN) { List list = sectionItem.inputList!; if (sectionItem.selectedObject != null) { print("#######not null"); } else { print("#######is_null"); if (sectionItem.selectedValue == null) { print("#######is_null#####"); } else { print('###${sectionItem.selectedValue}'); } sectionItem.selectedObject = sectionItem.selectedValue!.isNotEmpty ? getDataObject( sectionItem.id, sectionItem.selectedValue!.last, list) : list[0]; } print("valuesssss : ${sectionItem.selectedObject!.name} "); } if (sectionItem.widget == InteractionWidget.AUTOCOMPLETE || sectionItem.widget == InteractionWidget.MULTISELECT) { List list = sectionItem.inputList!; if (sectionItem.selectedObject != null) { } else { sectionItem.selectedObject = sectionItem.selectedValue!.isNotEmpty ? getDataObject( sectionItem.id, sectionItem.selectedValue!.last, list) : list[0]; } print("value : ${list.first.name} "); } if (sectionItem.widget == InteractionWidget.CHECKBOX) { List selectedvalues = []; if (sectionItem.selectedValue!.isNotEmpty) { for (var id in sectionItem.selectedValue!) { int ind = sectionItem.inputList! .indexWhere((element) => element.id == id); if (ind != -1) { sectionItem.inputList![ind].ischecked = true; selectedvalues.add(sectionItem.inputList![ind].name); } } } } if (sectionItem.widget == InteractionWidget.RADIO) { List list = sectionItem.inputList!; if (sectionItem.selectedValue!.isNotEmpty) { int ind = list.indexWhere( (element) => element.id == sectionItem.selectedValue!.last); if (ind != -1) { sectionItem.inputList![ind].ischecked = true; radioValue = sectionItem.inputList![ind].name; } } } if (sectionItem.widget == InteractionWidget.BUTTON && sectionItem.param == 'chooseFile' && sectionItem.selectedValue!.isNotEmpty) { print("choosed file"); print(sectionItem.selectedValue!.last); } } } print(interactionReponseList); print("check textcontrollers ${textEditingControllerList.length}"); } Future fetchDataSet() async { List list = getData("intlocation_1"); print(list); return "success"; } List getData(String widgetId) { List list = []; return list; } InputClass getDataObject(String widgetId, String id, List list) { // if (id != "") { // List list = []; InputClass value = InputClass(id: '', name: ''); if (list.isNotEmpty) { int index = list.indexWhere((element) => element.id.toString() == id); if (index != -1) { value = list[index]; } } return value; } // TODO: Search for widget with depid and check if selected is not null and by selected id get the data of current widget List getData2(SectionList sectionItem) { List list = []; // if (sectionItem.inputList != null) { list = sectionItem.inputList!; if (sectionItem.depid != "") { // print("check depid : ${sectionItem.depid}"); int i = 0; for (var obj in interactionReponseList) { i = obj.sectionList .indexWhere((element) => element.id == sectionItem.depid); // print("check depid index: $i"); if (i != -1) { //print("check depid value: ${obj.sectionList[i].value}"); if (obj.sectionList[i].value != null) { if (list .where((element) => element.pid == obj.sectionList[i].value) .isNotEmpty) { list = list .where((element) => element.pid == obj.sectionList[i].value) .toList(); sectionItem.selectedObject = list[0]; } else { // InputClass obj = InputClass( // id: "obj.sectionList[i].value", // name: "Select ${sectionItem.name}"); list = []; // list.add(obj); sectionItem.selectedObject = null; } } else { // int index = obj.sectionList // .indexWhere((element) => element.id == sectionItem.id); // list = obj.sectionList[index].inputList; } } } // int index = data[i].data.indexWhere((element) => element.) } // } return list; } setDropDownValue(String value, SectionList sectionItem, bool multiple, InputClass selectedObject) { int i = 0; for (var obj in interactionReponseList) { if (multiple && obj.multipleList != null) { i = obj.multipleList!.indexWhere((element) => element.id == sectionItem.id && element.gid == sectionItem.gid); if (i != -1) { obj.multipleList![i].value = value; obj.multipleList![i].tempselectedValue = []; obj.multipleList![i].tempselectedValue!.add(value); // obj.multipleList![i].selectedObject = selectedObj; } } else { i = obj.sectionList .indexWhere((element) => element.id == sectionItem.id); if (i != -1) { obj.sectionList[i].value = value; obj.sectionList[i].tempselectedValue = []; obj.sectionList[i].tempselectedValue!.add(value); // obj.sectionList[i].selectedObject = selectedObj; } } } notifyListeners(); } Future disposeValues() async { print("dispose called"); // await _hiveprovider.closeHiveBox(); for (var obj in interactionReponseList) { obj.multipleList!.clear(); // for (var obj2 in obj.sectionList) { // obj2.selectedObject = null; // } obj.sectionList.clear(); } interactionReponseList.clear(); } setTextValue(String value, SectionList sectionItem, bool multiple) { int i = 0; for (var obj in interactionReponseList) { if (multiple && obj.multipleList != null) { i = obj.multipleList!.indexWhere((element) => element.id == sectionItem.id && element.gid == sectionItem.gid); if (i != -1) { obj.multipleList![i].value = value; obj.multipleList![i].selectedValue!.add(value); } } else { i = obj.sectionList .indexWhere((element) => element.id == sectionItem.id); if (i != -1) { obj.sectionList[i].value = value; obj.sectionList[i].selectedValue!.add(value); } } } notifyListeners(); } setAutoCompleteValue(String value, SectionList sectionItem, bool multiple) { int i = 0; for (var obj in interactionReponseList) { if (multiple && obj.multipleList != null) { i = obj.multipleList!.indexWhere((element) => element.id == sectionItem.id && element.gid == sectionItem.gid); if (i != -1) { obj.multipleList![i].value = value; obj.multipleList![i].tempselectedValue = []; obj.multipleList![i].tempselectedValue!.add(value); } } else { i = obj.sectionList .indexWhere((element) => element.id == sectionItem.id); if (i != -1) { obj.sectionList[i].value = value; obj.sectionList[i].tempselectedValue = []; obj.sectionList[i].tempselectedValue!.add(value); } } } notifyListeners(); } String getDropDownValue( String value, SectionList sectionItem, List list) { int i = 0; String svalue = ''; i = list.indexWhere((element) => element.id == value); if (i != -1) { svalue = list[i].name; } else { svalue = list[0].name; } return svalue; } Future fetchData(InteractionResultData interactionResultData) async { InteractionResultData interactionConfig = interactionResultData; print("itemCategoryModel Item = + ${interactionConfig.result}"); interactionReponseList = interactionConfig.result; print("check stored: $interactionReponseList"); textEditingControllerList.clear(); for (var item in interactionReponseList) { sectionList = item.sectionList; for (var sectionItem in item.sectionList) { if (sectionItem.widget == InteractionWidget.TEXT) { var textEditingController = TextEditingController(); textEditingControllerList.add(textEditingController); sectionItem.controller = textEditingControllerList.last; } if (sectionItem.widget == InteractionWidget.DROPDOWN || sectionItem.widget == InteractionWidget.AUTOCOMPLETE || sectionItem.widget == InteractionWidget.MULTISELECT) { List list = sectionItem.inputList!; sectionItem.value = list[0].name; print("value : ${list.first} "); } } } print(interactionReponseList); print("check textcontrollers ${textEditingControllerList.length}"); return "success"; } Future fetchLocationData() async { var data = await rootBundle.loadString("assets/images/locationdetailsform.json"); Locations loc = Locations(location: Location.fromJson(json.decode(data)["location"])); locationList = loc.location; countryList = locationList.country; stateList = locationList.state; cityList = locationList.city; print(locationList); notifyListeners(); return "success"; } List getState(String cid) { List states = stateList.where((element) => element.countryId == cid).toList(); return states; } List getCity(String sid) { List city = cityList.where((element) => element.stateId == sid).toList(); return city; } String getCountryId(String name) { if (countryList.isNotEmpty) { int i = countryList.indexWhere((element) => element.countryName == name); return countryList[i].countryId; } return ''; } String getStateId(String name) { if (stateList.isNotEmpty) { int i = stateList.indexWhere((element) => element.stateName == name); return stateList[i].stateId; } return ''; } String getCityId(String name) { if (cityList.isNotEmpty) { int i = cityList.indexWhere((element) => element.cityName == name); return cityList[i].distId; } return ''; } setRadioValue(SectionList sectionItem) { List list = (sectionItem.input as List) .map((itemWord) => InputClass.fromJson(itemWord)) .toList(); radioValue = list[0].name; notifyListeners(); } setcheckBoxValue(SectionList sectionItem, String sectionName, bool newValue, String id, bool multiple) { int index = sectionItem.inputList!.indexWhere((element) => element.id == id); sectionItem.inputList![index].ischecked = newValue; // sectionItem.selectedValue.add(data[i].data[index].id); int index2 = 0; for (var obj in interactionReponseList) { if (multiple && obj.multipleList != null) { index2 = obj.multipleList!.indexWhere((element) => element.id == sectionItem.id && element.gid == sectionItem.gid); if (index2 != -1) { obj.multipleList![index2].value = sectionItem.inputList![index].id; obj.multipleList![index2].selectedValue! .add(sectionItem.inputList![index].id); } } else { index2 = obj.sectionList .indexWhere((element) => element.id == sectionItem.id); if (index2 != -1) { obj.sectionList[index2].value = sectionItem.inputList![index].id; obj.sectionList[index2].selectedValue! .add(sectionItem.inputList![index].id); } } } notifyListeners(); } getSectionItem(String sectionName) { newList = []; List addList = []; int index = interactionReponseList .indexWhere((element) => element.sectionName == sectionName); addList = interactionReponseList[index] .sectionList .map((e) => SectionList( depid: e.depid, id: e.id, inputList: e.inputList, isRequired: e.isRequired, name: e.name, param: e.param, selectedValue: [], widget: e.widget, controller: e.controller, gid: e.gid, input: e.input, selectedId: e.selectedId, value: e.value)) .toList(); SectionList delItem = SectionList( name: "delete", param: "deletebtn", id: "deletebtn", selectedValue: [], depid: "", widget: InteractionWidget.BUTTON, inputList: [], isRequired: true); addList.add(delItem); // if (interactionReponseList[index].multipleList!.isEmpty) { // newList = addList; // } else { if (addList.isNotEmpty) { for (SectionList obj in addList) { // obj.gid = obj.gid ?? addList.length; if (obj.widget == InteractionWidget.TEXT) { var textEditingController = TextEditingController(); multipletextEditingControllerList.add(textEditingController); obj.controller = multipletextEditingControllerList.last; obj.controller!.text = obj.selectedValue != null && obj.selectedValue!.isNotEmpty ? obj.selectedValue!.last : ''; } // newList.add(obj); } } if (interactionReponseList[index].multipleList == null) { interactionReponseList[index].multipleList = addList; } else { interactionReponseList[index].multipleList = interactionReponseList[index].multipleList! + addList; } newList = interactionReponseList[index].multipleList!; // newList = newList + addList; // } if (interactionReponseList[index].multipleList != null) { for (SectionList obj in interactionReponseList[index].multipleList!) { obj.gid = obj.gid ?? interactionReponseList[index].multipleList!.length; if (obj.widget == InteractionWidget.TEXT) { obj.controller!.text = obj.selectedValue != null && obj.selectedValue!.isNotEmpty ? obj.selectedValue!.last : ''; } // newList.add(obj); } } print( "check length : ${interactionReponseList[index].multipleList!.length}"); notifyListeners(); } deleteMultipleRows( int gid, SectionList sectionItem, String selectedSectionName) { int index = interactionReponseList .indexWhere((element) => element.sectionName == selectedSectionName); interactionReponseList[index] .multipleList! .removeWhere((item) => item.gid == gid); notifyListeners(); } saveJsonObject(BuildContext context, String form, SaveInteraction saveInteraction) async { for (var res in interactionReponseList) { for (var sec in res.sectionList) { debugPrint("print ${sec.selectedValue}"); } for (var sec in res.multipleList!) { debugPrint("printmultiple ${sec.selectedValue}"); } } List resultData = interactionReponseList .map((e) => FormFieldData( multipleList: e.multipleList == null ? [] : e.multipleList! .map((mobj) => SectionList( depid: mobj.depid, id: mobj.id, inputList: mobj.inputList, isRequired: mobj.isRequired, extension: mobj.extension, fileName: mobj.fileName, name: mobj.name, param: mobj.param, selectedValue: mobj.tempselectedValue ?? mobj.selectedValue, widget: mobj.widget, gid: mobj.gid, input: mobj.input, selectedId: mobj.selectedId, value: mobj.value)) .toList(), sectionList: e.sectionList .map((obj) => SectionList( depid: obj.depid, id: obj.id, inputList: obj.inputList, extension: obj.extension, fileName: obj.fileName, isRequired: obj.isRequired, name: obj.name, param: obj.param, selectedValue: obj.tempselectedValue ?? obj.selectedValue, widget: obj.widget, controller: obj.controller, gid: obj.gid, input: obj.input, selectedId: obj.selectedId, value: obj.value)) .toList(), sectionName: e.sectionName, multiple: e.multiple, )) .toList(); final data = SaveInteraction( save: resultData, id: saveInteraction.id, updatedTime: DateTime.now().toString(), form: saveInteraction.form, intId: saveInteraction.intId, intName: saveInteraction.intName, ); var box = Hive.box('InteractionDataBox'); if (!box.isOpen) { box = await Hive.openBox('InteractionDataBox'); } int index = box.values.toList().indexWhere((element) => element.id == data.id); box.putAt(index, data); await getRecords(saveInteraction.form!); // box.close(); // await MockApiCall().postFormData(data); } Future deleteRecord(SaveInteraction saveInteraction) async { var box = await Hive.openBox('InteractionDataBox'); final Map deliveriesmap = box.toMap(); dynamic deleteKey; deliveriesmap.forEach((key, value) { if (value.id == saveInteraction.id) { deleteKey = key; } }); box.delete(deleteKey); await getRecords(saveInteraction.form!); //box.close(); } List getModifiedList(List sectionList) { List newSectionList = []; for (var obj in sectionList) { if (obj.id != 'deletebtn') { if (obj.id == 'chooseFile') { List files = []; if (obj.selectedValue!.isNotEmpty && obj.selectedValue != null) { for (var file in obj.selectedValue!) { files.add(MultipartFile.fromFileSync(file)); } } MultipleSectionList newobj = MultipleSectionList( id: obj.id, selectedValue: files, ); newSectionList.add(newobj); } else { MultipleSectionList newobj = MultipleSectionList( id: obj.id, selectedValue: obj.selectedValue!, ); newSectionList.add(newobj); } } } return newSectionList; } bool validateMultipleRows() { for (var obj in interactionReponseList) { if (obj.multipleList != null) { for (var mulobj in obj.multipleList!) { if (mulobj.widget == InteractionWidget.TEXT) { if (mulobj.controller!.text.isEmpty) { return true; } } } } } return false; } bool validateTextFields() { for (var obj in interactionReponseList) { for (var mulobj in obj.sectionList) { if (mulobj.widget == InteractionWidget.TEXT) { if (mulobj.controller!.text.isEmpty) { return true; } } } } return false; } }