Skip to content

Commit 00b66c7

Browse files
author
wigginsp
committed
Version 1.0.4 - Added clone test
1 parent 9e85e09 commit 00b66c7

File tree

6 files changed

+379
-268
lines changed

6 files changed

+379
-268
lines changed

.idea/workspace.xml

Lines changed: 341 additions & 226 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class _MyAppState extends State<MyApp> {
9292
// shows example of retrieving a pin
9393
var newDietPlanFromPin = DietPlan().fromPin('R5EonpUDWy');
9494

95-
if (newDietPlanFromPin != null) print('Saving generic value worked!');
95+
if (newDietPlanFromPin != null) print('Retreiving from pin worked!');
9696

9797
} else {
9898
print(ApplicationConstants.APP_NAME + ": " + response.error.message);
@@ -128,7 +128,8 @@ class _MyAppState extends State<MyApp> {
128128
user = await user.verificationEmailRequest();
129129

130130
user = await user.save();
131-
await user.destroy();
131+
var destroyResponse = await user.destroy();
132+
if (destroyResponse.success) print('object has been destroyed!');
132133

133134
// Returns type ParseResponse as its a query, not a single result
134135
var response = await ParseUser.all();

lib/src/objects/parse_base.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ abstract class ParseBase {
44

55
String className;
66

7+
setClassName(String className) => this.className = className;
8+
String getClassName() => className;
9+
710
/// Stores all the values of a class
811
Map _objectData = Map<String, dynamic>();
912

@@ -42,7 +45,11 @@ abstract class ParseBase {
4245
await ParseCoreData().getStore().setString(key, toJson());
4346
}
4447

45-
@protected fromJson(Map objectData) {}
48+
@protected fromJson(Map objectData) {
49+
if (getObjectData() == null) setObjectData(Map());
50+
getObjectData().addAll(objectData);
51+
return this;
52+
}
4653

4754
/// Sets type [T] from objectData
4855
///

lib/src/objects/parse_object.dart

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of flutter_parse_sdk;
22

33
class ParseObject extends ParseBase {
4-
@override
5-
final String className;
4+
5+
ParseObject.clone(String className): this('className');
66

77
String _path;
88
bool _debug;
@@ -13,7 +13,8 @@ class ParseObject extends ParseBase {
1313
/// [String] className refers to the Table Name in your Parse Server,
1414
/// [bool] debug will overwrite the current default debug settings and
1515
/// [ParseHttpClient] can be overwritten to create your own HTTP Client
16-
ParseObject(this.className, {bool debug: false}): super() {
16+
ParseObject(String className, {bool debug: false}): super() {
17+
setClassName(className);
1718
_path = "/classes/$className";
1819
setClient(ParseHTTPClient());
1920
setDebug(isDebugEnabled(_client, objectLevelDebug: debug));
@@ -27,14 +28,6 @@ class ParseObject extends ParseBase {
2728
_client = client;
2829
}
2930

30-
@override
31-
fromJson(objectData) {
32-
var object = ParseObject(objectData['className']);
33-
object.setObjectData(objectData);
34-
if (object.updatedAt == null) object.updatedAt = object.createdAt;
35-
return object;
36-
}
37-
3831
/// Gets an object from the server using it's [String] objectId
3932
getObject(String objectId) async {
4033
try {
@@ -120,7 +113,7 @@ class ParseObject extends ParseBase {
120113
/// Handles an API response and logs data if [bool] debug is enabled
121114
@protected
122115
ParseResponse handleException(Exception exception, ParseApiRQ type) {
123-
ParseResponse parseResponse = ParseResponse.handleException(this, exception);
116+
ParseResponse parseResponse = ParseResponse.handleException(exception);
124117

125118
if (_debug) {
126119
logger(ParseCoreData().appName, className, type.toString(), parseResponse);

lib/src/objects/parse_response.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class ParseResponse {
99
/// Handles all the ParseObject responses
1010
///
1111
/// There are 3 probable outcomes from a Parse API call,
12-
/// 1. Fail - [ParseResponse] will be returned with further details
13-
/// 2. Success but no results. [ParseResponse] is returned.
14-
/// 3. Success with results. Again [ParseResponse] is returned
12+
/// 1. Fail - [ParseResponse()] will be returned with further details
13+
/// 2. Success but no results. [ParseResponse()] is returned.
14+
/// 3. Success with results. Again [ParseResponse()] is returned
1515
static handleResponse(ParseBase object, Response apiResponse) {
1616
var parseResponse = ParseResponse();
1717

@@ -32,7 +32,7 @@ class ParseResponse {
3232
}
3333

3434
/// Handles exception instead of throwing an exception
35-
static handleException(ParseBase object, Exception exception) {
35+
static handleException(Exception exception) {
3636
var response = ParseResponse();
3737
response.error = ParseError(message: exception.toString(), isTypeOfException: true);
3838
return response;
@@ -42,6 +42,7 @@ class ParseResponse {
4242
static ParseResponse _handleError(ParseResponse response, Response apiResponse) {
4343
Map<String, dynamic> responseData = JsonDecoder().convert(apiResponse.body);
4444
response.error = ParseError(code: responseData['code'], message: responseData['error']);
45+
response.statusCode = responseData['code'];
4546
return response;
4647
}
4748

@@ -81,6 +82,8 @@ class ParseResponse {
8182

8283
/// Handles a response with a single result object
8384
static _handleSingleResult(ParseBase object, map) {
85+
if (object is ParseUser) object = ParseUser.clone(map);
86+
if (object is ParseObject) object = ParseObject.clone(object.className);
8487
return object.fromJson(map);
8588
}
8689
}

lib/src/objects/parse_user.dart

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
part of flutter_parse_sdk;
22

33
class ParseUser extends ParseBase {
4-
@override
5-
final String className = '_User';
64

5+
ParseUser.clone(Map map): this(map[USERNAME],map[PASSWORD],map[EMAIL]);
6+
7+
final String className = '_User';
78
static final String path = "/classes/_User";
89

910
bool _debug;
@@ -40,23 +41,13 @@ class ParseUser extends ParseBase {
4041
this.emailAddress = emailAddress;
4142
}
4243

43-
/// Returns a [User] from a [Map] object
44-
@override
45-
fromJson(objectData) {
46-
var user = ParseUser(null, null, null);
47-
user.setObjectData(objectData);
48-
if (user.updatedAt == null) user.updatedAt = user.createdAt;
49-
user.saveInStorage(PARSE_STORE_USER);
50-
return user;
51-
}
52-
5344
/// Returns a [String] that's human readable. Ideal for printing logs
5445
@override
55-
String toString() => "Username: $username \nEmail Address:$emailAddress";
46+
String toString() => "User ($objectId): Username: $username, Email Address:$emailAddress";
5647

57-
static const String USERNAME = 'Username';
58-
static const String EMAIL = 'Email';
59-
static const String PASSWORD = 'Password';
48+
static const String USERNAME = 'username';
49+
static const String EMAIL = 'email';
50+
static const String PASSWORD = 'password';
6051
static const String ACL = 'ACL';
6152

6253
create(String username, String password, [String emailAddress]) {
@@ -206,14 +197,13 @@ class ParseUser extends ParseBase {
206197
}
207198

208199
/// Removes a user from Parse Server locally and online
209-
destroy() async {
200+
Future<ParseResponse> destroy() async {
210201
if (objectId != null) {
211202
try {
212203
final response = await _client.delete(
213204
_client.data.serverUrl + "$path/$objectId",
214205
headers: {"X-Parse-Session-Token": _client.data.sessionId});
215-
_handleResponse(response, ParseApiRQ.destroy);
216-
return objectId;
206+
return _handleResponse(response, ParseApiRQ.destroy);
217207
} on Exception catch (e) {
218208
return _handleException(e, ParseApiRQ.destroy);
219209
}
@@ -237,7 +227,7 @@ class ParseUser extends ParseBase {
237227

238228
return parseResponse;
239229
} on Exception catch (e) {
240-
return ParseResponse.handleException(emptyUser, e);
230+
return ParseResponse.handleException(e);
241231
}
242232
}
243233

@@ -249,7 +239,8 @@ class ParseUser extends ParseBase {
249239

250240
if (userMap != null) {
251241
ParseCoreData().sessionId = userMap['sessionToken'];
252-
var user = ParseUser(null,null,null).fromJson(userMap);
242+
var user = ParseUser(null,null,null);
243+
user.fromJson(userMap);
253244
return user;
254245
}
255246
}
@@ -260,7 +251,7 @@ class ParseUser extends ParseBase {
260251
/// Handles an API response and logs data if [bool] debug is enabled
261252
@protected
262253
ParseResponse _handleException(Exception exception, ParseApiRQ type) {
263-
ParseResponse parseResponse = ParseResponse.handleException(this, exception);
254+
ParseResponse parseResponse = ParseResponse.handleException(exception);
264255

265256
if (_debug) {
266257
logger(ParseCoreData().appName, className, type.toString(), parseResponse);
@@ -283,9 +274,10 @@ class ParseUser extends ParseBase {
283274
_client.data.sessionId = responseData['sessionToken'];
284275
}
285276

286-
if (type == ParseApiRQ.getAll) {
277+
if (type == ParseApiRQ.getAll || type == ParseApiRQ.destroy) {
287278
return parseResponse;
288279
} else {
280+
saveInStorage(PARSE_STORE_USER);
289281
return this;
290282
}
291283
}

0 commit comments

Comments
 (0)