Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:folly_fields/folly_fields.dart';
3 : import 'package:folly_fields/util/hashable.dart';
4 :
5 : ///
6 : ///
7 : ///
8 : abstract class AbstractModel<A> with Hashable {
9 : A? id;
10 : int? updatedAt;
11 : int? deletedAt;
12 : bool selected = false;
13 :
14 : ///
15 : ///
16 : ///
17 5 : AbstractModel({
18 : this.id,
19 : this.updatedAt,
20 : this.deletedAt,
21 : this.selected = false,
22 : });
23 :
24 : ///
25 : ///
26 : ///
27 15 : String get modelIdKey => FollyFields().modelIdKey;
28 :
29 : ///
30 : ///
31 : ///
32 15 : String get modelUpdatedAtKey => FollyFields().modelUpdatedAtKey;
33 :
34 : ///
35 : ///
36 : ///
37 15 : String get modelDeletedAtKey => FollyFields().modelDeletedAtKey;
38 :
39 : ///
40 : ///
41 : ///
42 0 : FollyDateParse? get dateParseUpdate => FollyFields().dateParseUpdate;
43 :
44 : ///
45 : ///
46 : ///
47 0 : FollyDateParse? get dateParseDelete => FollyFields().dateParseDelete;
48 :
49 : ///
50 : ///
51 : ///
52 5 : AbstractModel.fromJson(Map<String, dynamic> map) {
53 15 : id = map[modelIdKey];
54 :
55 10 : if (map.containsKey(modelUpdatedAtKey)) {
56 0 : updatedAt = dateParseUpdate != null
57 0 : ? dateParseUpdate!(map[modelUpdatedAtKey])
58 0 : : map[modelUpdatedAtKey];
59 : }
60 :
61 10 : if (map.containsKey(modelDeletedAtKey)) {
62 0 : deletedAt = dateParseDelete != null
63 0 : ? dateParseDelete!(map[modelDeletedAtKey])
64 0 : : map[modelDeletedAtKey];
65 : }
66 : }
67 :
68 : ///
69 : ///
70 : ///
71 5 : @mustCallSuper
72 : Map<String, dynamic> toMap() {
73 5 : Map<String, dynamic> map = <String, dynamic>{};
74 5 : if (id != null) {
75 15 : map[modelIdKey] = id;
76 : }
77 :
78 : return map;
79 : }
80 :
81 : ///
82 : ///
83 : ///
84 0 : Map<String, dynamic> toSave() => toMap();
85 :
86 : ///
87 : ///
88 : ///
89 5 : @override
90 15 : int get hashCode => hashIterable(toMap().values);
91 :
92 : ///
93 : ///
94 : ///
95 5 : @override
96 15 : bool operator ==(Object other) => hashCode == other.hashCode;
97 :
98 : ///
99 : ///
100 : ///
101 0 : String get listSearchTerm => toString();
102 :
103 : ///
104 : ///
105 : ///
106 0 : String get dropdownText => toString();
107 :
108 : ///
109 : ///
110 : ///
111 0 : static Map<String, dynamic> fromMultiMap(Map<String, dynamic> map) {
112 0 : Map<String, dynamic> newMap = <String, dynamic>{};
113 0 : for (final MapEntry<String, dynamic> entry in map.entries) {
114 0 : _multiMapEntry(entry, newMap);
115 : }
116 :
117 : return newMap;
118 : }
119 :
120 : ///
121 : ///
122 : ///
123 0 : static void _multiMapEntry(
124 : MapEntry<String, dynamic> entry,
125 : Map<String, dynamic> newMap,
126 : ) {
127 0 : List<String> parts = entry.key.split('_');
128 0 : if (parts.length > 1 && parts.first.isNotEmpty) {
129 : Map<String, dynamic> internalMap;
130 :
131 0 : internalMap = newMap.containsKey(parts.first)
132 0 : ? newMap[parts.first]
133 0 : : <String, dynamic>{};
134 :
135 0 : String internalKey = parts.getRange(1, parts.length).join('_');
136 :
137 0 : _multiMapEntry(
138 0 : MapEntry<String, dynamic>(internalKey, entry.value),
139 : internalMap,
140 : );
141 :
142 0 : newMap[parts.first] = internalMap;
143 : } else {
144 0 : newMap[entry.key] = entry.value;
145 : }
146 : }
147 : }
|