DiscoverModule/lib/ui_screen/interactionform/interactionprovider.dart

667 lines
22 KiB
Dart

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<FormFieldData> interactionReponseList = [];
// List<dynamic> sectionList = [];
List<SectionList> sectionList = [];
late Location locationList;
List<TextEditingController> textEditingControllerList = [];
List<TextEditingController> multipletextEditingControllerList = [];
int textfieldIndex = 0;
List<InputClass> checkboxlist = [];
String radioValue = '';
bool checkboxValue = false;
//List<InteractionDatum> data = [];
List<SectionList> newList = [];
String sectionName = '';
late String selectedCity = 'Selected City', selectedState = 'Selected State';
String? selectedValue;
List<String> selectedItems = [];
InputClass? selectedObj;
List<SaveInteraction> savedList = [];
List<InteractionConfigData> intConfigDataList = [];
String? intId, intName;
final HiveDataRepository _hiveprovider = HiveDataRepository(
Hive.box<InteractionConfigData>('InteractionConfigDataBox'));
initConfigData() async {
_hiveprovider.openHiveBox();
intConfigDataList = _hiveprovider.getAllDataFromHive();
notifyListeners();
}
Future<void> getRecords() async {
var box = Hive.box<SaveInteraction>('InteractionDataBox');
if (!box.isOpen) {
box = await Hive.openBox<SaveInteraction>('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<InputClass> 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<InputClass> list = [];
String value = ' ';
for (FormFieldData obj1 in interactionReponseList) {
// List<InputClass> 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<InputClass> getData2(SectionList sectionItem) {
List<InputClass> 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);
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<String> 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<InputClass> list = data[index].data;
List<InputClass> 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<InputClass> 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<InputClass> 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<InputClass> 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<SectionList> 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 (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<String> saveJsonObject(BuildContext context, String form,
{bool isEdit = false}) async {
print("form_data: $form");
List<FormFieldData> 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<SaveInteraction>('InteractionDataBox');
if (!box.isOpen) {
box = await Hive.openBox<SaveInteraction>('InteractionDataBox');
}
await box.put(await getNextAutoIncrementValue(), data);
// box.close();
await getRecords();
return generateId;
// await MockApiCall().postFormData(data);
// await prov.addOfflineData(data);
}
List<MultipleSectionList> getModifiedList(List<SectionList> sectionList) {
List<MultipleSectionList> 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<List<MultipleSectionList>> getMultipleSectionList(
List<SectionList> sectionList, List<SectionList> multipleList) {
List<List<MultipleSectionList>> list = [];
List<List<MultipleSectionList>> listing = [];
List<MultipleSectionList> secList = getModifiedList(sectionList);
List<MultipleSectionList> multipleList0 = getModifiedList(multipleList);
list.add(secList);
listing.add(secList);
// List<SectionList> listing = [];
if (multipleList.isNotEmpty) {
final releaseDateMap = multipleList.groupBy((m) => m.gid);
print("see map : $releaseDateMap");
if (releaseDateMap.isNotEmpty) {
listing = [];
List<List<SectionList>> 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<Save> 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;
}
}