Skip to content

Support Relation. #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ build/
.idea
example/ios/Frameworks/
example/lib/ui/

.vscode/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ final Map<String, String> params = <String, String>{'plan': 'paid'};
function.execute(parameters: params);
```

## Relation
The SDK supports Relation.

To Retrive a relation instance for user, call:
```dart
final relation = user.getRelation('dietPlans');
```

and then you can add a relation to the passed in object.

```dart
relation.add(dietPlan);
final result = await user.save();
```

## Other Features of this library
Main:
* Installation (View the example application)
Expand Down
14 changes: 7 additions & 7 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences/ios"

SPEC CHECKSUMS:
devicelocale: c1be0ad8a4bc50c184faf25bf8abe318e9bd69ba
Flutter: 9d0fac939486c9aba2809b7982dfdbb47a7b0296
flutter_stetho: 21464137ea9bb03914912de8916a7acfdcbfc0a5
package_info: d7c98b64f60add4c2908b9d94d82a45d3c8827ad
path_provider: 09407919825bfe3c2deae39453b7a5b44f467873
shared_preferences: 5a1d487c427ee18fcd3ea1f2a131569481834b53
devicelocale: feebbe5e7a30adb8c4f83185de1b50ff19b44f00
Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
flutter_stetho: 0e844616b41b86d6429e86a518ae3a46e83af7cd
package_info: 78cabb3c322943c55d39676f4a5bfc748c01d055
path_provider: f96fff6166a8867510d2c25fdcc346327cc4b259
shared_preferences: 1feebfa37bb57264736e16865e7ffae7fc99b523

PODFILE CHECKSUM: aff02bfeed411c636180d6812254b2daeea14d09

COCOAPODS: 1.5.3
COCOAPODS: 1.6.1
8 changes: 6 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
24DF2572E6AEEB9F7CE180C9 /* libPods-Runner.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Runner.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
5804EFBD11740E02FC51BC3E /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
96499D95196B10F296043703 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -75,6 +77,8 @@
4BCFFDB2D551EB587ABE1DF7 /* Pods */ = {
isa = PBXGroup;
children = (
5804EFBD11740E02FC51BC3E /* Pods-Runner.debug.xcconfig */,
96499D95196B10F296043703 /* Pods-Runner.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -255,7 +259,7 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
Expand All @@ -264,7 +268,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
9740EEB61CF901F6004384FC /* Run Script */ = {
Expand Down
1 change: 1 addition & 0 deletions lib/parse_server_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ part 'src/objects/parse_function.dart';
part 'src/objects/parse_geo_point.dart';
part 'src/objects/parse_installation.dart';
part 'src/objects/parse_object.dart';
part 'src/objects/parse_relation.dart';
part 'src/objects/parse_response.dart';
part 'src/objects/parse_session.dart';
part 'src/objects/parse_user.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const String keyClassSession = '_Session';
const String keyClassInstallation = '_Installation';
const String keyGeoPoint = 'GeoPoint';
const String keyFile = 'File';
const String keyRelation = 'Relation';

// Headers
const String keyHeaderSessionToken = 'X-Parse-Session-Token';
Expand Down
13 changes: 13 additions & 0 deletions lib/src/objects/parse_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class ParseObject extends ParseBase implements ParseCloneable {
}
}

/// Get the instance of ParseRelation class associated with the given key.
ParseRelation getRelation(String key) {
return ParseRelation(parent: this, key: key);
}

/// Removes an element from an Array
@Deprecated('Prefer to use the setRemove() method in save()')
Future<ParseResponse> remove(String key, dynamic values) async {
Expand Down Expand Up @@ -174,6 +179,14 @@ class ParseObject extends ParseBase implements ParseCloneable {
_arrayOperation('Add', key, values);
}

void addRelation(String key, List<dynamic> values) {
_arrayOperation('AddRelation', key, values);
}

void removeRelation(String key, List<dynamic> values) {
_arrayOperation('RemoveRelation', key, values);
}

/// Can be used to add arrays to a given type
Future<ParseResponse> _sortArrays(ParseApiRQ apiRQType, String arrayAction,
String key, List<dynamic> values) async {
Expand Down
44 changes: 44 additions & 0 deletions lib/src/objects/parse_relation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
part of flutter_parse_sdk;

class ParseRelation<T extends ParseObject> {
ParseRelation({ParseObject parent, String key}) {
_parent = parent;
_key = key;
}

String _targetClass;
ParseObject _parent;
String _key;
Set<T> _objects = Set<T>();

QueryBuilder getQuery() {
return QueryBuilder(ParseObject(_targetClass));
}

void add(T object) {
if (object != null) {
_targetClass = object.getClassName();
_objects.add(object);
_parent.addRelation(_key, _objects.map((T value) {
return value.toPointer();
}).toList());
}
}

void remove(T object) {
if (object != null) {
_targetClass = object.getClassName();
_objects.remove(object);
_parent.removeRelation(_key, _objects.map((T value) {
return value.toPointer();
}).toList());
}
}

Map<String, dynamic> toJson() =>
<String, String>{'__type': keyRelation, 'className': _objects?.first?.className, 'objects': parseEncode(_objects?.toList())};

ParseRelation<T> fromJson(Map<String, dynamic> map) => ParseRelation<T>()
.._objects = parseDecode(map['objects'])
.._targetClass = map['className'];
}
2 changes: 2 additions & 0 deletions lib/src/utils/parse_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ dynamic parseDecode(dynamic value) {
final num longitude = map['longitude'] ?? 0.0;
return ParseGeoPoint(
latitude: latitude.toDouble(), longitude: longitude.toDouble());
case 'Relation':
return ParseRelation().fromJson(map);
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/utils/parse_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ dynamic parseEncode(dynamic value, {bool full}) {
return value;
}

if (value is ParseRelation) {
return value.toJson();
}

if (value is ParseObject || value is ParseUser) {
if (full) {
return value.toJson(full: full);
Expand Down