Skip to content

Commit

Permalink
Merge pull request #166 from codeforpakistan/handle-in-or-and
Browse files Browse the repository at this point in the history
Handle in or and
  • Loading branch information
mubassirhayat authored Oct 26, 2023
2 parents fb6242f + e7b681c commit 5000e82
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 25 deletions.
64 changes: 39 additions & 25 deletions lib/hcai_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:hcais/components/FormElements.dart';
import 'package:hcais/utils/WidgetHelper.dart';
import 'package:hcais/utils/constants.dart';
import 'package:hcais/utils/helper.dart';
import 'package:hcais/utils/validation.dart';
import 'package:hcais/services/data_service.dart';
import 'args/Arguments.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -220,6 +221,15 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
maskType: ''),
)),
}
else if (field['type'] == 'label')
{
data.add(Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 10),
child: Text(field['label'].toString(),
style: TextStyle(
fontSize: field['fontSize'] ?? 13)),
))
}
else if (field['type'] == 'textfield')
{
data.add(Padding(
Expand Down Expand Up @@ -259,6 +269,8 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
isEditedView:
this._values['isEditedView'] == true,
conditions: field!['conditions'] ?? [],
andConditions: field!['andConditions'] ??
{'conditions': []},
hasHelpLabel:
field['hasHelpLabel'] ?? false,
helpLabelText: field['helpLabelText'] ??
Expand Down Expand Up @@ -544,6 +556,7 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
required bool isRequired,
required bool isEditedView,
List<dynamic> conditions = const [],
andConditions = const {},
required bool hasHelpLabel,
required String helpLabelText}) {
try {
Expand Down Expand Up @@ -596,7 +609,8 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
if (this.mounted) {
this._values[key] = val;
setState(() => this._values[key] = val);
_setCompleteField(key, val.toString(), options, [], conditions);
_setCompleteField(
key, val.toString(), options, [], conditions, andConditions);
}
},
decoration: BoxDecoration(
Expand Down Expand Up @@ -858,23 +872,10 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
value: value,
activeColor: Color(0xFF6200EE),
groupValue: _selectedRole[key],
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
// print('on tap');
// setState(() {
// _trucate[key] = !_trucate[key];
// });
},
child: Text(
title,
maxLines: 10,
softWrap: true,
),
),
],
title: Text(
title,
maxLines: 10,
softWrap: true,
),
onChanged: (Object? value) {
if (this.mounted) {
Expand Down Expand Up @@ -934,7 +935,8 @@ class _HcaiFormPageState extends State<HcaiFormPage> {

void _setCompleteField(String? key, String? value, List<dynamic> options,
List<dynamic> hiddenFields,
[List<dynamic> conditions = const []]) async {
[List<dynamic> conditions = const [],
andConditons = const {'conditions': []}]) async {
try {
var matches = [];
switch (key) {
Expand Down Expand Up @@ -1055,6 +1057,17 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
-1
}));
}
// handle and conditions
if (andConditons?['conditions']?.length > 0) {
String localKey = andConditons?['key'] ?? '';
this._values[localKey] = Validation.handleAndConditions(
this._values,
andConditons['conditions'],
andConditons['returnType']);
setState(() {
_selectedRole[localKey] = this._values[localKey];
});
}
if (matches.length > 0) {
matches.forEach((each) => {
if (each!['unHide']!.length > 0)
Expand Down Expand Up @@ -1097,12 +1110,13 @@ class _HcaiFormPageState extends State<HcaiFormPage> {
setState(() {
_selectedRole['isSSI'] = this._values['isSSI'];
});
if (this._values['isSSI'] == 'No') {
this.updateFlagForAll(this.allSteps, true, 'SSIDetected');
} else if (this._values['isSSI'] == 'Yes') {
// this.allSteps = this.originalSteps;
this.updateFlagForAll(this.allSteps, false, 'SSIDetected');
}
// show simple alert
// if (this._values['isSSI'] == 'No') {
// this.updateFlagForAll(this.allSteps, true, 'SSIDetected');
// } else if (this._values['isSSI'] == 'Yes') {
// // this.allSteps = this.originalSteps;
// this.updateFlagForAll(this.allSteps, false, 'SSIDetected');
// }
}

unHide(List<dynamic> fields, flag) {
Expand Down
91 changes: 91 additions & 0 deletions lib/utils/validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
class Validation {
static bool handleCriteria(
value1, value2, String comparisionType, String criteria) {
if (criteria == '>') {
return isLessThan(value2, value1, comparisionType);
} else if (criteria == '<') {
return isLessThan(value1, value2, comparisionType);
} else if (criteria == '==') {
return isEqual(value1, value2, comparisionType);
} else if (criteria == 'in' || criteria == 'and') {
//now all values of array 2 which is in value2 must be in 1 otherwise false
return hasValue1ContainsValue2(value1, value2, criteria);
} else {
return false;
}
}

static hasValue1ContainsValue2(value1, value2, criteria) {
if (!isNullOrEmpty(value1) &&
!isNullOrEmpty(value2) &&
value1?.length > 0 &&
value2?.length > 0) {
value2 = value2.where((element) => element != null).cast<int>().toList();
if (criteria == 'and') {
return value2?.every((value) =>
!isNullOrEmpty(value) &&
value1.any(
(map) => map.containsKey("index") && map["index"] == value));
} else if (criteria == 'in') {
return value2?.any((value) =>
!isNullOrEmpty(value) &&
value1.any(
(map) => map.containsKey("index") && map["index"] == value));
}
} else {
return false;
}
}

static isLessThan(val, val1, comparisionType) {
if (isNullOrEmpty(val ?? '') && isNullOrEmpty(val1 ?? '')) {
return comparisionType == 'int' ? -1 : false;
}
if (comparisionType == 'int') {
return int.parse(val) < int.parse(val1);
} else {
return val < val1;
}
}

static isEqual(val, val1, comparisionType) {
if (isNullOrEmpty(val) && isNullOrEmpty(val1)) {
return comparisionType == 'int' ? -1 : false;
}
if (comparisionType == 'int') {
return int.parse(val) == int.parse(val1);
} else {
return val == val1;
}
}

static bool isNullOrEmpty(value) {
return value == null || value == '';
}

static handleAndConditions(_currentValue, conditions, String returnType) {
try {
bool isValid = false;
for (var eachOR in conditions) {
isValid = handleCriteria(
_currentValue[eachOR?['key']],
(eachOR?['type'] == 'mutual'
? _currentValue[eachOR?[eachOR?['key']]]
: eachOR?['selecteds'] ?? -1),
eachOR?['comparisionType'] ?? '',
eachOR?['criteria'] ?? '');
if (!isValid) {
break;
}
print(isValid);
print(eachOR?['type']);
print(eachOR?['selecteds']);
}
;
return returnType == 'bool' ? isValid : (isValid ? 'Yes' : 'No');
} catch (err) {
print(err);
return returnType == 'bool' ? false : '';
}
}
}

0 comments on commit 5000e82

Please sign in to comment.