Line data Source code
1 : part of apptive_grid_model;
2 :
3 : /// A Uri representation used for performing Grid based Api Calls
4 : class GridUri extends ApptiveGridUri {
5 : /// Creates a new [GridUri] based on known ids for [user], [space] and [grid]
6 6 : GridUri({
7 : required this.user,
8 : required this.space,
9 : required this.grid,
10 : });
11 :
12 : /// Creates a new [GridUri] based on a string [uri]
13 : /// Main usage of this is for [GridUri] retrieved through other Api Calls
14 : /// If the Uri passed in is a [GridViewUri] it will return that
15 5 : factory GridUri.fromUri(String uri) {
16 : try {
17 : // Try to parse as GridViewUri
18 5 : return GridViewUri.fromUri(uri);
19 3 : } on ArgumentError {
20 : const regex = r'/api/users/(\w+)/spaces/(\w+)/grids/(\w+)\b';
21 6 : final matches = RegExp(regex).allMatches(uri);
22 12 : if (matches.isEmpty || matches.elementAt(0).groupCount != 3) {
23 2 : throw ArgumentError('Could not parse GridUri $uri');
24 : }
25 3 : final match = matches.elementAt(0);
26 3 : return GridUri(
27 3 : user: match.group(1)!,
28 3 : space: match.group(2)!,
29 3 : grid: match.group(3)!,
30 : );
31 : }
32 : }
33 :
34 : /// Id of the User that owns this Grid
35 : final String user;
36 :
37 : /// Id of the Space this Grid is in
38 : final String space;
39 :
40 : /// Id of the Grid this [GridUri] is representing
41 : final String grid;
42 :
43 2 : @override
44 : String toString() {
45 8 : return 'GridUri(user: $user, space: $space grid: $grid)';
46 : }
47 :
48 : /// Generates the uriString used for ApiCalls referencing this
49 2 : @override
50 8 : String get uriString => '/api/users/$user/spaces/$space/grids/$grid';
51 :
52 2 : @override
53 : bool operator ==(Object other) {
54 2 : return other is GridUri &&
55 6 : grid == other.grid &&
56 6 : user == other.user &&
57 6 : space == other.space;
58 : }
59 :
60 1 : @override
61 2 : int get hashCode => toString().hashCode;
62 : }
63 :
64 : /// Model for GridData
65 : class Grid {
66 : /// Creates a GridData Object
67 4 : Grid({
68 : required this.name,
69 : required this.schema,
70 : required this.fields,
71 : required this.rows,
72 : this.filter,
73 : });
74 :
75 : /// Deserializes [json] into a [Grid] Object
76 4 : factory Grid.fromJson(Map<String, dynamic> json) {
77 4 : final ids = json['fieldIds'] as List;
78 4 : final names = json['fieldNames'] as List;
79 4 : final schema = json['schema'];
80 4 : final fields = List<GridField>.generate(
81 4 : ids.length,
82 8 : (i) => GridField(
83 4 : ids[i],
84 4 : names[i],
85 4 : dataTypeFromSchemaProperty(
86 16 : schemaProperty: schema['properties']['fields']['items'][i],
87 : ),
88 : ),
89 : );
90 4 : final entries = (json['entities'] as List)
91 12 : .map((e) => GridRow.fromJson(e, fields, schema))
92 4 : .toList();
93 4 : final filter = json['filter'];
94 4 : return Grid(
95 4 : name: json['name'],
96 : schema: schema,
97 : fields: fields,
98 : rows: entries,
99 : filter: filter,
100 : );
101 : }
102 :
103 : /// Name of the Form
104 : final String name;
105 :
106 : /// Schema used for deserializing and validating data send back to the server
107 : final dynamic schema;
108 :
109 : /// Filter applied to this GridView. If this is not null the Grid is actually a GridView
110 : final dynamic filter;
111 :
112 : /// List of [GridField] representing the Columns the Grid has
113 : final List<GridField> fields;
114 :
115 : /// Rows of the Grid
116 : final List<GridRow> rows;
117 :
118 : /// Serializes [Grid] into a json Map
119 6 : Map<String, dynamic> toJson() => {
120 6 : 'name': name,
121 6 : 'schema': schema,
122 18 : 'entities': rows.map((e) => e.toJson()).toList(),
123 18 : 'fieldIds': fields.map((e) => e.id).toList(),
124 18 : 'fieldNames': fields.map((e) => e.name).toList(),
125 7 : if (filter != null) 'filter': filter,
126 : };
127 :
128 1 : @override
129 : String toString() {
130 5 : return 'GridData(name: $name, fields: $fields, rows: $rows, filter: $filter)';
131 : }
132 :
133 1 : @override
134 : bool operator ==(Object other) {
135 1 : return other is Grid &&
136 3 : name == other.name &&
137 3 : schema == other.schema &&
138 3 : f.listEquals(fields, other.fields) &&
139 3 : f.listEquals(rows, other.rows);
140 : }
141 :
142 1 : @override
143 2 : int get hashCode => toString().hashCode;
144 : }
|