Skip to content

Commit

Permalink
Version 1.0.5 - Fixed saving issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phillwiggins committed Jan 13, 2019
1 parent 62be758 commit eecc751
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 333 deletions.
471 changes: 244 additions & 227 deletions .idea/workspace.xml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class _MyAppState extends State<MyApp> {
}

runTestQueries() {
createItem();
getAllItems();
getAllItemsByName();
getSingleItem();
Expand All @@ -59,6 +60,19 @@ class _MyAppState extends State<MyApp> {
initUser();
}

void createItem() async {

var newObject = ParseObject('TestObjectForApi');
newObject.set<String>('name', 'testItem');
newObject.set<int>('age', 26);

var apiResponse = await newObject.create();

if (apiResponse.success && apiResponse.result != null) {
print(ApplicationConstants.keyAppName + ": " + apiResponse.result.toString());
}
}

void getAllItemsByName() async {
var apiResponse = await ParseObject('ParseTableName').getAll();

Expand Down
4 changes: 0 additions & 4 deletions lib/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ part 'src/utils/parse_logger.dart';

part 'src/utils/parse_utils.dart';

part 'src/utils/parse_utils_date.dart';

part 'src/utils/parse_utils_objects.dart';

class Parse {
ParseCoreData data;
final ParseHTTPClient client = new ParseHTTPClient();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const String keyEndPointHealth = '/health';
const String keyVarClassName = 'className';
const String keyVarObjectId = 'objectId';
const String keyVarCreatedAt = 'createdAt';
const String keyVarUpdatedAt = 'updatedAT';
const String keyVarUpdatedAt = 'updatedAt';
const String keyVarUsername = 'username';
const String keyVarEmail = 'email';
const String keyVarPassword = 'password';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/network/parse_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ParseHTTPClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) {
request.headers[keyHeaderUserAgent] = _userAgent;
request.headers[keyHeaderApplicationId] = data.applicationId;
request.headers[keyHeaderContentType] = keyHeaderContentTypeJson;
//request.headers[keyHeaderContentType] = keyHeaderContentTypeJson;
if (data.masterKey != null) request.headers[keyHeaderMasterKey] = data.masterKey;
return _client.send(request);
}
Expand Down
36 changes: 17 additions & 19 deletions lib/src/objects/parse_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ abstract class ParseBase {
set objectId(String objectId) => set<String>(keyVarObjectId, objectId);

/// Returns [DateTime] createdAt
DateTime get createdAt => stringToDateTime(get<String>(keyVarCreatedAt));

set createdAt(DateTime createdAt) =>
set<String>(keyVarCreatedAt, dateTimeToString(createdAt));
DateTime get createdAt => get<DateTime>(keyVarCreatedAt);

/// Returns [DateTime] updatedAt
DateTime get updatedAt => stringToDateTime(get<String>(keyVarUpdatedAt));

set updatedAt(DateTime updatedAt) =>
set<String>(keyVarUpdatedAt, dateTimeToString(updatedAt));
DateTime get updatedAt => get<DateTime>(keyVarUpdatedAt);

/// Converts object to [String] in JSON format
@protected
Expand All @@ -39,28 +33,30 @@ abstract class ParseBase {
}

if (createdAt != null) {
map[keyVarCreatedAt] = dateTimeToString(createdAt);
map[keyVarCreatedAt] = createdAt.toIso8601String();
}

if (updatedAt != null) {
map[keyVarUpdatedAt] = dateTimeToString(updatedAt);
map[keyVarUpdatedAt] = updatedAt.toIso8601String();
}

getObjectData().forEach((key, value) {
if (!map.containsKey(key)) map[key] = parseEncode(value);
});

if (forApiRQ) {
map.remove(keyVarClassName);
map.remove(keyVarAcl);
map.remove(keyParamSessionToken);
map.remove(keyVarCreatedAt);
map.remove(keyVarUpdatedAt);
map.remove(keyVarClassName);
map.remove(keyVarAcl);
map.remove(keyParamSessionToken);
}

return map;
}

@override
String toString() => JsonEncoder().convert(toJson());
String toString() => json.encode(toJson());

@protected
fromJson(Map objectData) {
Expand All @@ -70,9 +66,9 @@ abstract class ParseBase {
} else if (key == keyVarObjectId) {
objectId = value;
} else if (key == keyVarCreatedAt) {
createdAt = stringToDateTime(value);
set<DateTime>(keyVarCreatedAt, DateTime.parse(value));
} else if (key == keyVarUpdatedAt) {
updatedAt = stringToDateTime(value);
set<DateTime>(keyVarUpdatedAt, DateTime.parse(value));
} else {
getObjectData()[key] = parseDecode(value);
}
Expand All @@ -83,7 +79,7 @@ abstract class ParseBase {

/// Creates a copy of this class
@protected
copy() => fromJson(JsonDecoder().convert(toJson()));
copy() => fromJson(json.decode(toJson()));

/// Sets all the objects variables
@protected
Expand Down Expand Up @@ -135,7 +131,9 @@ abstract class ParseBase {
/// Replicates Android SDK pin process and saves object to storage
Future<bool> pin() async {
if (objectId != null) {
await ParseCoreData().getStore().setString(objectId, JsonEncoder().convert(toJson()));
await ParseCoreData()
.getStore()
.setString(objectId, json.encode(toJson()));
return true;
} else {
return false;
Expand Down Expand Up @@ -163,7 +161,7 @@ abstract class ParseBase {
var itemFromStore = ParseCoreData().getStore().getString(objectId);

if (itemFromStore != null) {
var map = JsonDecoder().convert(itemFromStore);
var map = json.decode(itemFromStore);

if (map != null) {
return fromJson(map);
Expand Down
21 changes: 12 additions & 9 deletions lib/src/objects/parse_object.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
part of flutter_parse_sdk;

class ParseObject extends ParseBase implements ParseCloneable {

ParseObject.clone(String className): this('className');
ParseObject.clone(String className) : this('className');

@override
clone(Map map) => ParseObject.clone(className)..fromJson(map);
Expand All @@ -16,18 +15,18 @@ class ParseObject extends ParseBase implements ParseCloneable {
/// [String] className refers to the Table Name in your Parse Server,
/// [bool] debug will overwrite the current default debug settings and
/// [ParseHttpClient] can be overwritten to create your own HTTP Client
ParseObject(String className, {bool debug: false}): super() {
ParseObject(String className, {bool debug: false}) : super() {
setClassName(className);
_path = "$keyEndPointClasses$className";
setClient(ParseHTTPClient());
setDebug(isDebugEnabled(objectLevelDebug: debug));
}

void setDebug(bool debug){
void setDebug(bool debug) {
_debug = debug;
}

void setClient(ParseHTTPClient client){
void setClient(ParseHTTPClient client) {
_client = client;
}

Expand Down Expand Up @@ -57,7 +56,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
Future<ParseResponse> create() async {
try {
var uri = _client.data.serverUrl + "$_path";
var result = await _client.post(uri, body: toJson(forApiRQ: true));
var body = json.encode(toJson(forApiRQ: true));
var result = await _client.post(uri, body: body);
return handleResponse(result, ParseApiRQ.create);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.create);
Expand All @@ -71,7 +71,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
} else {
try {
var uri = "${ParseCoreData().serverUrl}$_path/$objectId";
var result = await _client.put(uri, body: toJson(forApiRQ: true));
var body = json.encode(toJson(forApiRQ: true));
var result = await _client.put(uri, body: body);
return handleResponse(result, ParseApiRQ.save);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save);
Expand Down Expand Up @@ -107,7 +108,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
ParseResponse parseResponse = ParseResponse.handleResponse(this, response);

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

return parseResponse;
Expand All @@ -119,7 +121,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
ParseResponse parseResponse = ParseResponse.handleException(exception);

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

return parseResponse;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/parse_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ParseResponse {
if (apiResponse != null) {
parseResponse.statusCode = apiResponse.statusCode;

if (apiResponse.statusCode != 200) {
if (apiResponse.statusCode != 200 && apiResponse.statusCode != 201) {
return _handleError(parseResponse, apiResponse);
} else if (apiResponse.body == "{\"results\":[]}"){
return _handleSuccessWithNoResults(parseResponse, 1, "Successful request, but no results found");
Expand Down
14 changes: 6 additions & 8 deletions lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ParseUser extends ParseBase implements ParseCloneable {
headers: {
keyHeaderRevocableSession: "1",
},
body: JsonEncoder().convert(bodyData));
body: json.encode(bodyData));

_handleResponse(response, ParseApiRQ.signUp);
return this;
Expand Down Expand Up @@ -151,8 +151,7 @@ class ParseUser extends ParseBase implements ParseCloneable {
try {
final response = await _client.post(
"${_client.data.serverUrl}$keyEndPointVerificationEmail",
body: JsonEncoder().convert({keyVarEmail: emailAddress}));

body: json.encode({keyVarEmail: emailAddress}));
return _handleResponse(response, ParseApiRQ.verificationEmailRequest);
} on Exception catch (e) {
return _handleException(e, ParseApiRQ.verificationEmailRequest);
Expand All @@ -164,7 +163,7 @@ class ParseUser extends ParseBase implements ParseCloneable {
try {
final response = await _client.post(
"${_client.data.serverUrl}$keyEndPointRequestPasswordReset",
body: JsonEncoder().convert({keyVarEmail: emailAddress}));
body: json.encode({keyVarEmail: emailAddress}));
return _handleResponse(response, ParseApiRQ.requestPasswordReset);
} on Exception catch (e) {
return _handleException(e, ParseApiRQ.requestPasswordReset);
Expand All @@ -180,10 +179,9 @@ class ParseUser extends ParseBase implements ParseCloneable {
return signUp();
} else {
try {
Map map = toJson(forApiRQ: true);
final response = await _client.put(
_client.data.serverUrl + "$path/$objectId",
body: map);
var uri = _client.data.serverUrl + "$path/$objectId";
var body = json.encode(toJson(forApiRQ: true), toEncodable: dateTimeEncoder);
final response = await _client.put(uri, body: body);
return _handleResponse(response, ParseApiRQ.save);
} on Exception catch (e) {
return _handleException(e, ParseApiRQ.save);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/parse_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dynamic parseDecode(dynamic value) {
switch (map["__type"]) {
case "Date":
String iso = map["iso"];
return stringToDateTime(iso);
return DateTime.parse(iso);
case "Bytes":
String val = map["base64"];
return base64.decode(val);
Expand Down
16 changes: 14 additions & 2 deletions lib/src/utils/parse_encoder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
part of flutter_parse_sdk;

/// Custom encoder for DateTime
dynamic dateTimeEncoder(dynamic item) {
if(item is DateTime) {
return item.toIso8601String();
}
return item;
}

bool isValidType(dynamic value) {
return value == null ||
value is String ||
Expand All @@ -24,7 +32,7 @@ dynamic parseEncode(dynamic value) {
}

if (value is ParseObject) {
return value.toJson;
return _encodeObject(value);
}

if (value is ParseUser) {
Expand All @@ -38,6 +46,10 @@ dynamic parseEncode(dynamic value) {
return value;
}

String _encodeObject(ParseObject object){
return "{'__type': 'Pointer', $keyVarClassName: ${object.className}, $keyVarObjectId: ${object.objectId}}";
}

Map<String, dynamic> _encodeDate(DateTime date) {
return <String, dynamic>{"__type": "Date", "iso": dateTimeToString(date)};
return <String, dynamic>{"__type": "Date", "iso": date.toIso8601String()};
}
51 changes: 0 additions & 51 deletions lib/src/utils/parse_utils_date.dart

This file was deleted.

9 changes: 0 additions & 9 deletions lib/src/utils/parse_utils_objects.dart

This file was deleted.

0 comments on commit eecc751

Please sign in to comment.