-
Notifications
You must be signed in to change notification settings - Fork 3
/
commitment_form_controller.dart
90 lines (78 loc) · 2.86 KB
/
commitment_form_controller.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import 'package:collaction_cms/domain/core/value_validators.dart';
import 'package:collaction_cms/domain/crowdaction/crowdaction.dart';
import 'package:uuid/uuid.dart';
import 'package:flutter/foundation.dart';
class CommitmentFormController with ChangeNotifier {
CommitmentFormController();
String? _id;
final Map<String, ValidationOutput?> _validationOutputs = {
'title': null,
'tags': null,
'description': ValidationOutput(error: false),
'points': null,
'icon': null
};
String? get id => _id;
void setId(String id) {
_id = id;
}
Map<String, ValidationOutput?> get validationOutputs => _validationOutputs;
void setValidationOutput(
String fieldName, ValidationOutput validationOutput) {
_validationOutputs[fieldName] = validationOutput;
bool verificationCompleted = true;
for (var value in _validationOutputs.values) {
if (value == null) {
verificationCompleted = false;
}
}
if (verificationCompleted) {
notifyListeners();
}
}
/// the [skipInitialValidation] is used only to handle edge cases. In the commitmentItem a validation is needed before the child widget is
/// "attached" to the widget tree. The status checker needs to handle the logic of the commitment form child before the lifetime of the children.
/// If you want to bypass this you can use skipInitialValidation. In the context of [AssignedCommitments] makes sense because
/// all the commitments that are already assigned have a correct initial values, since they passed [AssignCommitment] validation.
bool isReadyForBloc([bool skipInitialValidation = false]) {
if (skipInitialValidation == true) {
try {
if (_validationOutputs["title"]!.error ||
_validationOutputs["tags"]!.error ||
_validationOutputs["description"]!.error ||
_validationOutputs["points"]!.error ||
_validationOutputs["icon"]!.error) {
return false;
} else {
return true;
}
} catch (e) {
return true;
}
}
try {
if (_validationOutputs["title"]!.error ||
_validationOutputs["tags"]!.error ||
_validationOutputs["description"]!.error ||
_validationOutputs["points"]!.error ||
_validationOutputs["icon"]!.error) {
return false;
} else {
return true;
}
} catch (e) {
return false;
}
}
Commitment commitmentFactory() {
return Commitment(
id: _id ?? const Uuid().v4(),
iconId: _validationOutputs["icon"]!.output as String,
description: (_validationOutputs["description"]!.output as String).trim(),
tags: _validationOutputs["tags"]!.output as List<String>,
label: (_validationOutputs["title"]!.output as String).trim(),
points: _validationOutputs["points"]!.output as int,
blocks: <String>[],
);
}
}