Line data Source code
1 : part of apptive_grid_model; 2 : 3 : /// A Uri representation used for performing Space based Api Calls 4 : class SpaceUri extends ApptiveGridUri { 5 : /// Creates a new [SpaceUri] based on known ids for [user] and [space] 6 3 : SpaceUri({ 7 : required this.user, 8 : required this.space, 9 : }); 10 : 11 : /// Creates a new [SpaceUri] based on a string [uri] 12 : /// Main usage of this is for [SpaceUri] retrieved through other Api Calls 13 3 : factory SpaceUri.fromUri(String uri) { 14 : const regex = r'/api/users/(\w+)/spaces/(\w+)\b'; 15 6 : final matches = RegExp(regex).allMatches(uri); 16 12 : if (matches.isEmpty || matches.elementAt(0).groupCount != 2) { 17 2 : throw ArgumentError('Could not parse SpaceUri $uri'); 18 : } 19 3 : final match = matches.elementAt(0); 20 9 : return SpaceUri(user: match.group(1)!, space: match.group(2)!); 21 : } 22 : 23 : /// Id of the User that owns this Grid 24 : final String user; 25 : 26 : /// Id of the Space this [SpaceUri] is representing 27 : final String space; 28 : 29 2 : @override 30 : String toString() { 31 6 : return 'SpaceUri(user: $user, space: $space)'; 32 : } 33 : 34 : /// Generates the uriString used for ApiCalls referencing this [space] 35 2 : @override 36 6 : String get uriString => '/api/users/$user/spaces/$space'; 37 : 38 2 : @override 39 : bool operator ==(Object other) { 40 14 : return other is SpaceUri && space == other.space && user == other.user; 41 : } 42 : 43 1 : @override 44 2 : int get hashCode => toString().hashCode; 45 : } 46 : 47 : /// Model for a Space 48 : class Space { 49 : /// Creates a new Space Model with a certain [id] and [name] 50 : /// [grids] is [List<GridUri>] pointing to the [Grid]s contained in this [Space] 51 1 : Space({ 52 : required this.id, 53 : required this.name, 54 : required this.grids, 55 : }); 56 : 57 : /// Deserializes [json] into a [Space] Object 58 2 : Space.fromJson(Map<String, dynamic> json) 59 2 : : name = json['name'], 60 2 : id = json['id'], 61 : grids = 62 10 : (json['gridUris'] as List).map((e) => GridUri.fromUri(e)).toList(); 63 : 64 : /// Name of this space 65 : final String name; 66 : 67 : /// Id of this space 68 : final String id; 69 : 70 : /// [GridUri]s pointing to [Grid]s contained in this [Space] 71 : final List<GridUri> grids; 72 : 73 : /// Serializes this [Space] into a json Map 74 2 : Map<String, dynamic> toJson() => { 75 1 : 'name': name, 76 1 : 'id': id, 77 5 : 'gridUris': grids.map((e) => e.uriString).toList(), 78 : }; 79 : 80 1 : @override 81 : String toString() { 82 5 : return 'Space(name: $name, id: $id, spaces: ${grids.toString()})'; 83 : } 84 : 85 1 : @override 86 : bool operator ==(Object other) { 87 1 : return other is Space && 88 3 : id == other.id && 89 3 : name == other.name && 90 3 : f.listEquals(grids, other.grids); 91 : } 92 : 93 1 : @override 94 2 : int get hashCode => toString().hashCode; 95 : }