Skip to content

Update from upstream #10

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 2 commits into from
Mar 12, 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
1 change: 1 addition & 0 deletions lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const String keyEndPointLogin = '/login';
const String keyEndPointLogout = '/logout';
const String keyEndPointUsers = '/users';
const String keyEndPointSessions = '/sessions';
const String keyEndPointInstallations = '/installations';
const String keyEndPointVerificationEmail = '/verificationEmailRequest';
const String keyEndPointRequestPasswordReset = '/requestPasswordReset';
const String keyEndPointClasses = '/classes/';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/parse_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class ParseBase {
map.remove(keyVarAcl);
map.remove(keyParamSessionToken);
}

return map;
}

Expand Down
35 changes: 26 additions & 9 deletions lib/src/objects/parse_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ParseFile extends ParseObject {

if (file != null) {
this.name = path.basename(file.path);
this._path = 'files/$name';
this._path = '/files/$name';
} else {
this.name = name;
this.url = url;
Expand Down Expand Up @@ -79,20 +79,37 @@ class ParseFile extends ParseObject {
}

/// Uploads a file to Parse Server
upload() async {
@override
Future<ParseResponse> save() async {
return upload();
}

/// Uploads a file to Parse Server
Future<ParseResponse> upload() async {
if (saved) {
return this;
//Creates a Fake Response to return the correct result
final response = {"url": this.url, "name": this.name};
return handleResponse(this, Response(json.encode(response), 201),
ParseApiRQ.upload, _debug, className);
}

final ext = path.extension(file.path).replaceAll('.', '');
final headers = <String, String>{
HttpHeaders.contentTypeHeader: getContentType(ext)
};

var uri = _client.data.serverUrl + "$_path";
final body = await file.readAsBytes();
final response = await _client.post(uri, headers: headers, body: body);
return handleResponse<ParseFile>(
this, response, ParseApiRQ.upload, _debug, className);
try {
var uri = _client.data.serverUrl + "$_path";
final body = await file.readAsBytes();
final response = await _client.post(uri, headers: headers, body: body);
if (response.statusCode == 201) {
final map = json.decode(response.body);
this.url = map["url"].toString();
this.name = map["name"].toString();
}
return handleResponse(
this, response, ParseApiRQ.upload, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.upload, _debug, className);
}
}
}
7 changes: 7 additions & 0 deletions lib/src/objects/parse_geo_point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ class ParseGeoPoint extends ParseObject {
assert(value >= -180.0 || value <= 180.0);
_longitude = value;
}

@override
toJson({bool full: false, bool forApiRQ: false}) => <String, dynamic>{
"__type": "GeoPoint",
"latitude": _latitude,
"longitude": _longitude
};
}
103 changes: 91 additions & 12 deletions lib/src/objects/parse_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class ParseInstallation extends ParseObject {
static final String keyAppVersion = 'appVersion';
static final String keyAppIdentifier = 'appIdentifier';
static final String keyParseVersion = 'parseVersion';
static final List<String> readOnlyKeys = [ //TODO
static final List<String> readOnlyKeys = [
//TODO
keyDeviceToken, keyDeviceType, keyInstallationId,
keyAppName, keyAppVersion, keyAppIdentifier, keyParseVersion
];
Expand All @@ -24,13 +25,15 @@ class ParseInstallation extends ParseObject {

String get deviceToken => super.get<String>(keyDeviceToken);

set deviceToken(String deviceToken) => set<String>(keyDeviceToken, deviceToken);
set deviceToken(String deviceToken) =>
set<String>(keyDeviceToken, deviceToken);

String get deviceType => super.get<String>(keyDeviceType);

String get installationId => super.get<String>(keyInstallationId);

set _installationId(String installationId) => set<String>(keyInstallationId, installationId);
set _installationId(String installationId) =>
set<String>(keyInstallationId, installationId);

String get appName => super.get<String>(keyAppName);

Expand All @@ -42,9 +45,7 @@ class ParseInstallation extends ParseObject {

/// Creates an instance of ParseInstallation
ParseInstallation(
{bool debug,
ParseHTTPClient client,
bool autoSendSessionId})
{bool debug, ParseHTTPClient client, bool autoSendSessionId})
: super(keyClassInstallation) {
_debug = isDebugEnabled(objectLevelDebug: debug);
_client = client ??
Expand All @@ -60,7 +61,8 @@ class ParseInstallation extends ParseObject {
if (_currentInstallationId == null) {
_currentInstallationId = (await _getFromLocalStore()).installationId;
}
return _currentInstallationId != null && installation.installationId == _currentInstallationId;
return _currentInstallationId != null &&
installation.installationId == _currentInstallationId;
}

/// Gets the current installation from storage
Expand All @@ -75,9 +77,12 @@ class ParseInstallation extends ParseObject {
/// Updates the installation with current device data
_updateInstallation() async {
//Device type
if (Platform.isAndroid) set<String>(keyDeviceType, "android");
else if (Platform.isIOS) set<String>(keyDeviceType, "ios");
else throw Exception("Unsupported platform/operating system");
if (Platform.isAndroid)
set<String>(keyDeviceType, "android");
else if (Platform.isIOS)
set<String>(keyDeviceType, "ios");
else
throw Exception("Unsupported platform/operating system");

//Locale
String locale = await Devicelocale.currentLocale;
Expand All @@ -99,7 +104,8 @@ class ParseInstallation extends ParseObject {
Future<ParseResponse> create() async {
var isCurrent = await ParseInstallation.isCurrent(this);
if (isCurrent) await _updateInstallation();
ParseResponse parseResponse = await super.create();
//ParseResponse parseResponse = await super.create();
ParseResponse parseResponse = await _create();
if (parseResponse.success && isCurrent) {
saveInStorage(keyParseStoreInstallation);
}
Expand All @@ -110,7 +116,8 @@ class ParseInstallation extends ParseObject {
Future<ParseResponse> save() async {
var isCurrent = await ParseInstallation.isCurrent(this);
if (isCurrent) await _updateInstallation();
ParseResponse parseResponse = await super.save();
//ParseResponse parseResponse = await super.save();
ParseResponse parseResponse = await _save();
if (parseResponse.success && isCurrent) {
saveInStorage(keyParseStoreInstallation);
}
Expand Down Expand Up @@ -145,4 +152,76 @@ class ParseInstallation extends ParseObject {
await installation._updateInstallation();
return installation;
}

/// Creates a new object and saves it online
Future<ParseResponse> _create() async {
try {
var uri = _client.data.serverUrl + "$keyEndPointInstallations";
var body = json.encode(toJson(forApiRQ: true));
if (_debug) {
logRequest(ParseCoreData().appName, className,
ParseApiRQ.create.toString(), uri, body);
}
var result = await _client.post(uri, body: body);

//Set the objectId on the object after it is created.
//This allows you to perform operations on the object after creation
if (result.statusCode == 201) {
final map = json.decode(result.body);
this.objectId = map["objectId"].toString();
}

return handleResponse(this, result, ParseApiRQ.create, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.create, _debug, className);
}
}

/// Saves the current object online
Future<ParseResponse> _save() async {
if (getObjectData()[keyVarObjectId] == null) {
return create();
} else {
try {
var uri =
"${ParseCoreData().serverUrl}$keyEndPointInstallations/$objectId";
var body = json.encode(toJson(forApiRQ: true));
if (_debug) {
logRequest(ParseCoreData().appName, className,
ParseApiRQ.save.toString(), uri, body);
}
var result = await _client.put(uri, body: body);
return handleResponse(this, result, ParseApiRQ.save, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save, _debug, className);
}
}
}

///Subscribes the device to a channel of push notifications.
void subscribeToChannel(String value) {
final List<dynamic> channel = [value];
this.addUnique("channels", channel);
}

///Unsubscribes the device to a channel of push notifications.
void unsubscribeFromChannel(String value) {
final List<dynamic> channel = [value];
this.removeAll("channels", channel);
}

///Returns an <List<String>> containing all the channel names this device is subscribed to.
Future<List<dynamic>> getSubscribedChannels() async {
print("getSubscribedChannels");
final apiResponse =
await ParseObject(keyClassInstallation).getObject(this.objectId);

if (apiResponse.success) {
var installation = apiResponse.result as ParseObject;
print("achou installation");
return Future.value(installation.get<List<dynamic>>("channels"));
} else {
return null;
}
}
}
Loading