Line data Source code
1 : part of apptive_grid_model; 2 : 3 : /// Model representing a DataEntry from ApptiveGrid 4 : /// 5 : /// [T] type of the data used in Flutter 6 : /// [S] type used when sending Data back 7 : abstract class DataEntity<T, S> { 8 : /// Create a new DataEntity with [value] 9 11 : DataEntity([this.value]); 10 : 11 : /// The value of the Entity 12 : T? value; 13 : 14 : /// The Value that is used when sending data back to the server. Matching against the schema 15 : S? get schemaValue; 16 : 17 6 : @override 18 : String toString() { 19 18 : return '$runtimeType(value: $value)}'; 20 : } 21 : 22 8 : @override 23 : bool operator ==(Object other) { 24 8 : return other is DataEntity<T, S> && 25 24 : runtimeType == other.runtimeType && 26 24 : value == (other).value; 27 : } 28 : 29 2 : @override 30 4 : int get hashCode => toString().hashCode; 31 : } 32 : 33 : /// [DataEntity] representing [String] Objects 34 : class StringDataEntity extends DataEntity<String, String> { 35 : /// Creates a new StringDataEntity Object with value [value] 36 18 : StringDataEntity([String? value]) : super(value); 37 : 38 5 : @override 39 5 : String? get schemaValue => value; 40 : } 41 : 42 : /// [DataEntity] representing [DateTime] Objects 43 : class DateTimeDataEntity extends DataEntity<DateTime, String> { 44 : /// Creates a new DateTimeDataEntity Object with value [value] 45 12 : DateTimeDataEntity([DateTime? value]) : super(value); 46 : 47 : /// Creates a new DateTimeDataEntity Object from json 48 : /// [json] needs to be a Iso8601String 49 5 : factory DateTimeDataEntity.fromJson(dynamic json) { 50 : DateTime? jsonValue; 51 : if (json != null) { 52 4 : jsonValue = DateTime.parse(json); 53 : } 54 5 : return DateTimeDataEntity(jsonValue); 55 : } 56 : 57 : /// Returns [value] as a Iso8601 Date String 58 5 : @override 59 9 : String? get schemaValue => value?.toIso8601String(); 60 : } 61 : 62 : /// [DataEntity] representing a Date 63 : /// Internally this is using [DateTime] ignoring the Time Part 64 : class DateDataEntity extends DataEntity<DateTime, String> { 65 : /// Creates a new DateTimeDataEntity Object with value [value] 66 12 : DateDataEntity([DateTime? value]) : super(value); 67 : 68 : /// Creates a new DateTimeDataEntity Object from json 69 : /// [json] needs to be a Date String with Format yyyy-MM-dd 70 5 : factory DateDataEntity.fromJson(dynamic json) { 71 : DateTime? jsonValue; 72 : if (json != null) { 73 8 : jsonValue = _dateFormat.parse(json); 74 : } 75 5 : return DateDataEntity(jsonValue); 76 : } 77 : 78 12 : static final DateFormat _dateFormat = DateFormat('yyyy-MM-dd'); 79 : 80 : /// Returns [value] formatted to yyyy-MM-dd 81 5 : @override 82 17 : String? get schemaValue => value != null ? _dateFormat.format(value!) : null; 83 : } 84 : 85 : /// [DataEntity] representing [boolean] Objects 86 : class BooleanDataEntity extends DataEntity<bool, bool> { 87 : /// Creates a new BooleanDataEntity Object 88 14 : BooleanDataEntity([bool? value = false]) : super(value ?? false); 89 : 90 6 : @override 91 6 : bool? get schemaValue => value; 92 : } 93 : 94 : /// [DataEntity] representing [int] Objects 95 : class IntegerDataEntity extends DataEntity<int, int> { 96 : /// Creates a new IntegerDataEntity Object 97 12 : IntegerDataEntity([int? value]) : super(value); 98 : 99 5 : @override 100 5 : int? get schemaValue => value; 101 : } 102 : 103 : /// [DataEntity] representing [double] Objects 104 : class DecimalDataEntity extends DataEntity<double, double> { 105 : /// Creates a new DecimalDataEntity Object 106 8 : DecimalDataEntity([double? value]) : super(value); 107 : 108 4 : @override 109 4 : double? get schemaValue => value; 110 : } 111 : 112 : /// [DataEntity] representing an enum like Object 113 : class EnumDataEntity extends DataEntity<String, String> { 114 : /// Creates a new EnumDataEntity Object with [value] out of possible [options] 115 8 : EnumDataEntity({String? value, this.options = const []}) : super(value); 116 : 117 : /// Possible options of the Data Entity 118 : List<String> options; 119 : 120 4 : @override 121 4 : String? get schemaValue => value; 122 : 123 2 : @override 124 : String toString() { 125 8 : return '$runtimeType(value: $value, values: $options)}'; 126 : } 127 : 128 3 : @override 129 : bool operator ==(Object other) { 130 3 : return other is EnumDataEntity && 131 9 : value == (other).value && 132 9 : f.listEquals(options, (other).options); 133 : } 134 : 135 1 : @override 136 2 : int get hashCode => toString().hashCode; 137 : } 138 : 139 : /// [DataEntity] representing an Object CrossReferencing to a different Grid 140 : class CrossReferenceDataEntity extends DataEntity<String, dynamic> { 141 : /// Create a new CrossReference Data Entity 142 2 : CrossReferenceDataEntity({ 143 : String? value, 144 : this.entityUri, 145 : required this.gridUri, 146 2 : }) : super(value); 147 : 148 : /// Creates a new CrossReferenceDataEntity from a Json Response 149 2 : factory CrossReferenceDataEntity.fromJson({ 150 : required Map? jsonValue, 151 : required String gridUri, 152 : }) => 153 2 : CrossReferenceDataEntity( 154 2 : value: jsonValue?['displayValue'], 155 2 : entityUri: jsonValue?['uri'] != null 156 4 : ? EntityUri.fromUri(jsonValue?['uri']) 157 : : null, 158 2 : gridUri: GridUri.fromUri(gridUri), 159 : ); 160 : 161 : /// The [EntityUri] pointing to the Entity this is referencing 162 : EntityUri? entityUri; 163 : 164 : /// Pointing to the [Grid] this is referencing 165 : final GridUri gridUri; 166 : 167 1 : @override 168 : dynamic get schemaValue { 169 2 : if (value == null || entityUri == null) { 170 : return null; 171 : } else { 172 4 : return {'displayValue': value, 'uri': entityUri?.uriString}; 173 : } 174 : } 175 : 176 1 : @override 177 : String toString() { 178 4 : return 'CrossReferenceDataEntity(displayValue: $value, enitytUri: $entityUri, gridUri: $gridUri)}'; 179 : } 180 : 181 1 : @override 182 : bool operator ==(Object other) { 183 1 : return other is CrossReferenceDataEntity && 184 3 : value == other.value && 185 3 : entityUri == other.entityUri && 186 3 : gridUri == other.gridUri; 187 : } 188 : 189 1 : @override 190 2 : int get hashCode => toString().hashCode; 191 : }