import 'dart:convert';
// import 'dart:html';
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/repository/hive_repository.dart';
import 'package:discover_module/ui_screen/interactionform/util.dart';
import 'package:flutter/cupertino.dart';
import 'package:hive_flutter/hive_flutter.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';
class InteractionProvider extends ChangeNotifier {
List interactionReponseList = [];
// List sectionList = [];
List sectionList = [];
late Location locationList;
List textEditingControllerList = [];
List multipletextEditingControllerList = [];
int textfieldIndex = 0;
List checkboxlist = [];
String radioValue = '';
bool checkboxValue = false;
//List data = [];
List newList = [];
String sectionName = '';
late String selectedCity = 'Selected City', selectedState = 'Selected State';
String? selectedValue;
List selectedItems = [];
InputClass? selectedObj;
List savedList = [];
List intConfigDataList = [];
String? intId, intName;
final HiveDataRepository _hiveprovider = HiveDataRepository(
Hive.box('InteractionConfigDataBox'));
initConfigData() async {
_hiveprovider.openHiveBox();
intConfigDataList = _hiveprovider.getAllDataFromHive();
notifyListeners();
}
Future getRecords() async {
var box = Hive.box('InteractionDataBox');
if (!box.isOpen) {
box = await Hive.openBox('InteractionDataBox');
}
savedList = box.values.toList();
print("savedListt: $savedList");
notifyListeners();
}
init(int index) async {
await initConfigData();
InteractionConfigData interactionConfigData = intConfigDataList[index];
intId = intConfigDataList[index].id;
intName = intConfigDataList[index].name;
radioValue = '';
print("data $intConfigDataList");
await fetchData(interactionConfigData.widgets);
notifyListeners();
}
initSavedForm(SaveInteraction saveInteractiondata) {
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,
name: mobj.name,
param: mobj.param,
selectedValue: mobj.selectedValue,
fileName: mobj.fileName,
extension: mobj.extension,
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,
name: obj.name,
param: obj.param,
fileName: obj.fileName,
extension: obj.extension,
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 (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].id;
print("value : ${list.first} ");
}
}
}
print(interactionReponseList);
print("check textcontrollers ${textEditingControllerList.length}");
}
String getDataValue(String widgetId, String id) {
print("issue *************");
if (id != "") {
List list = [];
String value = ' ';
for (FormFieldData obj1 in interactionReponseList) {
// List list =
for (SectionList obj in obj1.sectionList) {
if (obj.id == widgetId) {
list = obj.inputList!;
}
}
}
if (list.isNotEmpty) {
int index = list.indexWhere((element) => element.id.toString() == id);
if (index != -1) {
value = list[index].name;
}
}
return value;
} else {
return " ";
}
}
// 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) {
int i = 0;
print("selected $value");
for (var obj in interactionReponseList) {
if (multiple && obj.multipleList != null) {
i = obj.multipleList!
.indexWhere((element) => element.id == sectionItem.id);
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();
}
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);
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();
}
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) {
sectionItem.selectedValue = [];
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) {
// int index = data
// .indexWhere((element) => element.widgetId == sectionItem.id);
// List list = data[index].data;
List list = sectionItem.inputList!;
sectionItem.value = list[0].id;
if (sectionItem.widget == InteractionWidget.MULTISELECT) {
sectionItem.selectedValue!.add(list[0].name);
} else {
sectionItem.selectedValue!.add(list[0].id);
}
sectionItem.selectedObject = list[0];
print("value : ${list.first} ");
} else if (sectionItem.widget == InteractionWidget.CHECKBOX) {
List list = sectionItem.inputList!;
if (list.isNotEmpty) {
for (var element in sectionItem.inputList!) {
element.ischecked = false;
}
}
// sectionItem.value = list[0].id;
// sectionItem.selectedValue!.add(list[0].id);
}
}
}
print(interactionReponseList);
print("check textcontrollers ${textEditingControllerList.length}");
notifyListeners();
return "success";
}
resetAllWidgetsData() {
textEditingControllerList.clear();
for (var item in interactionReponseList) {
item.multipleList = [];
sectionList = item.sectionList;
for (var sectionItem in item.sectionList) {
sectionItem.selectedValue = [];
// sectionItem.value = '';
// sectionItem.selectedObject = InputClass(id: '', name: '');
if (sectionItem.widget == InteractionWidget.TEXT) {
var textEditingController = TextEditingController();
textEditingControllerList.add(textEditingController);
sectionItem.controller = textEditingControllerList.last;
}
if (item.sectionName != "Other") {
if (sectionItem.widget == InteractionWidget.DROPDOWN ||
sectionItem.widget == InteractionWidget.AUTOCOMPLETE ||
sectionItem.widget == InteractionWidget.MULTISELECT) {
// int index = data
// .indexWhere((element) => element.widgetId == sectionItem.id);
print("Selected widget : ${sectionItem.id}");
List list = sectionItem.inputList!;
sectionItem.value = list[0].id;
sectionItem.selectedObject = list[0];
print("value : ${list.first.name} ");
}
}
}
}
print(interactionReponseList);
print("check textcontrollers ${textEditingControllerList.length}");
notifyListeners();
}
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);
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);
var textEditingController = TextEditingController();
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);
for (SectionList obj in addList) {
//obj.gid = obj.gid ?? addList.length;
if (obj.widget == InteractionWidget.TEXT) {
// multipletextEditingControllerList.clear();
var textEditingController = TextEditingController();
obj.controller = textEditingController;
multipletextEditingControllerList.add(textEditingController);
}
// newList.add(obj);
}
// if (interactionReponseList[index].multipleList!.isEmpty) {
// newList = addList;
// } else {
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) {
// var textEditingController = TextEditingController();
// multipletextEditingControllerList.add(textEditingController);
// obj.controller = multipletextEditingControllerList.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);
print("controllerssssss : ${multipletextEditingControllerList.length}");
interactionReponseList[index]
.multipleList!
.removeWhere((item) => item.gid == gid);
notifyListeners();
}
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;
}
Future saveJsonObject(BuildContext context, String form,
{bool isEdit = false}) async {
print("form_data: $form");
// for (var ob in interactionReponseList) {
// for (var ob2 in ob.multipleList!) {
// debugPrint("!!!!!!!!!!!!In multiple :${ob2.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,
name: mobj.name,
param: mobj.param,
selectedValue: mobj.selectedValue,
extension: mobj.extension,
fileName: mobj.fileName,
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,
name: obj.name,
param: obj.param,
selectedValue: obj.selectedValue,
widget: obj.widget,
controller: obj.controller,
gid: obj.gid,
input: obj.input,
extension: obj.extension,
fileName: obj.fileName,
selectedId: obj.selectedId,
value: obj.value))
.toList(),
sectionName: e.sectionName,
multiple: e.multiple))
.toList();
String generateId = '${intId}R${await getNextAutoIncrementValue()}';
final data = SaveInteraction(
save: resultData,
id: generateId,
updatedTime: DateTime.now().toString(),
form: form,
intId: intId ?? "id",
intName: intName ?? "name");
var box = Hive.box('InteractionDataBox');
if (!box.isOpen) {
box = await Hive.openBox('InteractionDataBox');
}
await box.put(await getNextAutoIncrementValue(), data);
// box.close();
await getRecords();
return generateId;
// await MockApiCall().postFormData(data);
// await prov.addOfflineData(data);
}
List getModifiedList(List sectionList) {
List newSectionList = [];
for (var obj in sectionList) {
if (obj.id != 'deletebtn') {
if (obj.input == 'chooseFile') {
MultipleSectionList newobj = MultipleSectionList(
id: obj.id,
selectedValue: obj.selectedValue!,
extension: obj.extension!,
fileName: obj.fileName!,
);
newSectionList.add(newobj);
} else {
MultipleSectionList newobj = MultipleSectionList(
id: obj.id,
selectedValue: obj.selectedValue!,
);
newSectionList.add(newobj);
}
}
}
return newSectionList;
}
List> getMultipleSectionList(
List sectionList, List multipleList) {
List> list = [];
List> listing = [];
List secList = getModifiedList(sectionList);
List multipleList0 = getModifiedList(multipleList);
list.add(secList);
listing.add(secList);
// List listing = [];
if (multipleList.isNotEmpty) {
final releaseDateMap = multipleList.groupBy((m) => m.gid);
print("see map : $releaseDateMap");
if (releaseDateMap.isNotEmpty) {
listing = [];
List> mulList =
releaseDateMap.values.toList(growable: true);
for (var item in mulList) {
listing.add(getModifiedList(item));
}
listing.add(secList);
list = [...listing];
//});
}
}
return list;
}
SaveInteractionFormJson formJson(SaveInteraction saveInteraction) {
List saveList = [];
for (var obj in saveInteraction.save) {
Save saveobj = Save(
sectionName: obj.sectionName,
multipleSectionList:
getMultipleSectionList(obj.sectionList, obj.multipleList!));
saveList.add(saveobj);
}
SaveInteractionFormJson saveInteractionFormJson = SaveInteractionFormJson(
interactionForm1: saveInteraction.form!,
intId: saveInteraction.id,
intName: saveInteraction.intName,
save: saveList);
return saveInteractionFormJson;
}
}