Skip to content

Object is not dirty after fromJson. forceUpdate is not ignored. async/await fixes from master. #254

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 5 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 12 additions & 12 deletions lib/src/objects/parse_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,23 @@ abstract class ParseBase {
if (key == parseClassName || key == '__type') {
// NO OP
} else if (key == keyVarObjectId) {
objectId = value;
_getObjectData()[keyVarObjectId] = value;
} else if (key == keyVarCreatedAt) {
if (keyVarCreatedAt is String) {
set<DateTime>(keyVarCreatedAt, _parseDateFormat.parse(value));
_getObjectData()[keyVarCreatedAt] = _parseDateFormat.parse(value);
} else {
set<DateTime>(keyVarCreatedAt, value);
_getObjectData()[keyVarCreatedAt] = value;
}
} else if (key == keyVarUpdatedAt) {
if (keyVarUpdatedAt is String) {
set<DateTime>(keyVarUpdatedAt, _parseDateFormat.parse(value));
_getObjectData()[keyVarUpdatedAt] = _parseDateFormat.parse(value);
} else {
set<DateTime>(keyVarUpdatedAt, value);
_getObjectData()[keyVarUpdatedAt] = _parseDateFormat.parse(value);
}
} else if (key == keyVarAcl) {
this[keyVarAcl] = ParseACL().fromJson(value);
_getObjectData()[keyVarAcl] = ParseACL().fromJson(value);
} else {
this[key] = parseDecode(value);
_getObjectData()[key] = parseDecode(value);
}
});

Expand Down Expand Up @@ -177,18 +177,18 @@ abstract class ParseBase {
/// Saves in storage
Future<void> saveInStorage(String key) async {
final String objectJson = json.encode(toJson(full: true));
ParseCoreData().getStore()..setString(key, objectJson);
await ParseCoreData().getStore().setString(key, objectJson);
}

/// Sets type [T] from objectData
///
/// To set an int, call setType<int> and an int will be saved
/// [bool] forceUpdate is always true, if unsure as to whether an item is
/// needed or not, set to false
void set<T>(String key, T value, {bool forceUpdate = true}) {
void set<T>(String key, T value, {bool forceUpdate = false}) {
if (value != null) {
if (_getObjectData().containsKey(key)) {
if (_getObjectData()[key] == value) {
if (_getObjectData()[key] == value && !forceUpdate) {
return;
}
_getObjectData()[key] =
Expand Down Expand Up @@ -240,7 +240,7 @@ abstract class ParseBase {
await unpin();
final Map<String, dynamic> objectMap = parseEncode(this, full: true);
final String json = jsonEncode(objectMap);
ParseCoreData().getStore()..setString(objectId, json);
await ParseCoreData().getStore().setString(objectId, json);
return true;
} else {
return false;
Expand All @@ -252,7 +252,7 @@ abstract class ParseBase {
/// Replicates Android SDK pin process and saves object to storage
Future<bool> unpin({String key}) async {
if (objectId != null) {
ParseCoreData().getStore()..remove(key ?? objectId);
await ParseCoreData().getStore().remove(key ?? objectId);
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/objects/parse_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ class ParseInstallation extends ParseObject {
}

///Subscribes the device to a channel of push notifications.
void subscribeToChannel(String value) {
Future<void> subscribeToChannel(String value) async {
final List<dynamic> channel = <String>[value];
setAddAllUnique('channels', channel);
save();
await save();
}

///Unsubscribes the device to a channel of push notifications.
void unsubscribeFromChannel(String value) {
Future<void> unsubscribeFromChannel(String value) async {
final List<dynamic> channel = <String>[value];
setRemove('channels', channel);
save();
await save();
}

///Returns an <List<String>> containing all the channel names this device is subscribed to.
Expand Down
39 changes: 24 additions & 15 deletions lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
try {
final Uri url = getSanitisedUri(_client, '$keyEndPointUserName');
final Response response = await _client.get(url, headers: headers);
return _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
return await _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
_debug, _getEmptyUser().parseClassName);
} on Exception catch (e) {
return handleException(
Expand Down Expand Up @@ -146,7 +146,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
},
body: body);

return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.signUp, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.signUp, _debug, parseClassName);
Expand All @@ -172,7 +172,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
keyHeaderRevocableSession: '1',
});

return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.login, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.login, _debug, parseClassName);
Expand All @@ -195,7 +195,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
}
}));

return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.loginAnonymous, _debug, parseClassName);
} on Exception catch (e) {
return handleException(
Expand All @@ -222,7 +222,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
'authData': <String, dynamic>{provider: authData}
}));

return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.loginWith, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.loginWith, _debug, parseClassName);
Expand All @@ -248,7 +248,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
final Response response = await _client.post(url,
headers: <String, String>{keyHeaderSessionToken: sessionId});

return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.logout, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.logout, _debug, parseClassName);
Expand All @@ -261,7 +261,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
final Response response = await _client.post(
'${_client.data.serverUrl}$keyEndPointVerificationEmail',
body: json.encode(<String, dynamic>{keyVarEmail: emailAddress}));
return _handleResponse(this, response,
return await _handleResponse(this, response,
ParseApiRQ.verificationEmailRequest, _debug, parseClassName);
} on Exception catch (e) {
return handleException(
Expand All @@ -275,8 +275,9 @@ class ParseUser extends ParseObject implements ParseCloneable {
final Response response = await _client.post(
'${_client.data.serverUrl}$keyEndPointRequestPasswordReset',
body: json.encode(<String, dynamic>{keyVarEmail: emailAddress}));
return _handleResponse(this, response, ParseApiRQ.requestPasswordReset,
_debug, parseClassName);
return await _handleResponse(
this, response, ParseApiRQ.requestPasswordReset, _debug,
parseClassName);
} on Exception catch (e) {
return handleException(
e, ParseApiRQ.requestPasswordReset, _debug, parseClassName);
Expand All @@ -290,19 +291,27 @@ class ParseUser extends ParseObject implements ParseCloneable {
@override
Future<ParseResponse> save() async {
if (objectId == null) {
return signUp();
return await signUp();
} else {
return super.save();
final ParseResponse response = await super.save();
if (response.success) {
await _onResponseSuccess();
}
return response;
}
}

Future<void> _onResponseSuccess() async {
await saveInStorage(keyParseStoreUser);
}

/// Removes a user from Parse Server locally and online
Future<ParseResponse> destroy() async {
if (objectId != null) {
try {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final Response response = await _client.delete(url);
return _handleResponse(
return await _handleResponse(
this, response, ParseApiRQ.destroy, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.destroy, _debug, parseClassName);
Expand Down Expand Up @@ -352,8 +361,8 @@ class ParseUser extends ParseObject implements ParseCloneable {
}

/// Handles all the response data for this class
static ParseResponse _handleResponse(ParseUser user, Response response,
ParseApiRQ type, bool debug, String className) {
static Future<ParseResponse> _handleResponse(ParseUser user, Response response,
ParseApiRQ type, bool debug, String className) async {
final ParseResponse parseResponse =
handleResponse<ParseUser>(user, response, type, debug, className);

Expand All @@ -372,7 +381,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
return parseResponse;
} else {
final ParseUser user = parseResponse.result;
user?.saveInStorage(keyParseStoreUser);
await user?._onResponseSuccess();
return parseResponse;
}
}
Expand Down