Skip to content

Commit cf07ef3

Browse files
RodrigoSMarquesphillwiggins
authored andcommitted
Many BugFix and New Functions ParseInstallation and ParseObject (#113)
* Bugfix: ParseEncoder, ParseFiles, New methods Array e Counters Bugfix: ParseEncoder, ParseFiles, New methods Array e Counters * bugfile ParseFile, ParseInstallation bugfile ParseFile, ParseInstallation * Update .gitignore Update .gitignore
1 parent 6747e77 commit cf07ef3

9 files changed

+251
-57
lines changed

lib/src/base/parse_constants.dart

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const String keyEndPointLogin = '/login';
1010
const String keyEndPointLogout = '/logout';
1111
const String keyEndPointUsers = '/users';
1212
const String keyEndPointSessions = '/sessions';
13+
const String keyEndPointInstallations = '/installations';
1314
const String keyEndPointVerificationEmail = '/verificationEmailRequest';
1415
const String keyEndPointRequestPasswordReset = '/requestPasswordReset';
1516
const String keyEndPointClasses = '/classes/';

lib/src/objects/parse_base.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ abstract class ParseBase {
5252
map.remove(keyVarAcl);
5353
map.remove(keyParamSessionToken);
5454
}
55-
55+
5656
return map;
5757
}
5858

lib/src/objects/parse_file.dart

+26-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ParseFile extends ParseObject {
3636

3737
if (file != null) {
3838
this.name = path.basename(file.path);
39-
this._path = 'files/$name';
39+
this._path = '/files/$name';
4040
} else {
4141
this.name = name;
4242
this.url = url;
@@ -79,20 +79,37 @@ class ParseFile extends ParseObject {
7979
}
8080

8181
/// Uploads a file to Parse Server
82-
upload() async {
82+
@override
83+
Future<ParseResponse> save() async {
84+
return upload();
85+
}
86+
87+
/// Uploads a file to Parse Server
88+
Future<ParseResponse> upload() async {
8389
if (saved) {
84-
return this;
90+
//Creates a Fake Response to return the correct result
91+
final response = {"url": this.url, "name": this.name};
92+
return handleResponse(this, Response(json.encode(response), 201),
93+
ParseApiRQ.upload, _debug, className);
8594
}
8695

8796
final ext = path.extension(file.path).replaceAll('.', '');
8897
final headers = <String, String>{
8998
HttpHeaders.contentTypeHeader: getContentType(ext)
9099
};
91-
92-
var uri = _client.data.serverUrl + "$_path";
93-
final body = await file.readAsBytes();
94-
final response = await _client.post(uri, headers: headers, body: body);
95-
return handleResponse<ParseFile>(
96-
this, response, ParseApiRQ.upload, _debug, className);
100+
try {
101+
var uri = _client.data.serverUrl + "$_path";
102+
final body = await file.readAsBytes();
103+
final response = await _client.post(uri, headers: headers, body: body);
104+
if (response.statusCode == 201) {
105+
final map = json.decode(response.body);
106+
this.url = map["url"].toString();
107+
this.name = map["name"].toString();
108+
}
109+
return handleResponse(
110+
this, response, ParseApiRQ.upload, _debug, className);
111+
} on Exception catch (e) {
112+
return handleException(e, ParseApiRQ.upload, _debug, className);
113+
}
97114
}
98115
}

lib/src/objects/parse_geo_point.dart

+7
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ class ParseGeoPoint extends ParseObject {
3636
assert(value >= -180.0 || value <= 180.0);
3737
_longitude = value;
3838
}
39+
40+
@override
41+
toJson({bool full: false, bool forApiRQ: false}) => <String, dynamic>{
42+
"__type": "GeoPoint",
43+
"latitude": _latitude,
44+
"longitude": _longitude
45+
};
3946
}

lib/src/objects/parse_installation.dart

+91-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class ParseInstallation extends ParseObject {
1010
static final String keyAppVersion = 'appVersion';
1111
static final String keyAppIdentifier = 'appIdentifier';
1212
static final String keyParseVersion = 'parseVersion';
13-
static final List<String> readOnlyKeys = [ //TODO
13+
static final List<String> readOnlyKeys = [
14+
//TODO
1415
keyDeviceToken, keyDeviceType, keyInstallationId,
1516
keyAppName, keyAppVersion, keyAppIdentifier, keyParseVersion
1617
];
@@ -24,13 +25,15 @@ class ParseInstallation extends ParseObject {
2425

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

27-
set deviceToken(String deviceToken) => set<String>(keyDeviceToken, deviceToken);
28+
set deviceToken(String deviceToken) =>
29+
set<String>(keyDeviceToken, deviceToken);
2830

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

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

33-
set _installationId(String installationId) => set<String>(keyInstallationId, installationId);
35+
set _installationId(String installationId) =>
36+
set<String>(keyInstallationId, installationId);
3437

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

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

4346
/// Creates an instance of ParseInstallation
4447
ParseInstallation(
45-
{bool debug,
46-
ParseHTTPClient client,
47-
bool autoSendSessionId})
48+
{bool debug, ParseHTTPClient client, bool autoSendSessionId})
4849
: super(keyClassInstallation) {
4950
_debug = isDebugEnabled(objectLevelDebug: debug);
5051
_client = client ??
@@ -60,7 +61,8 @@ class ParseInstallation extends ParseObject {
6061
if (_currentInstallationId == null) {
6162
_currentInstallationId = (await _getFromLocalStore()).installationId;
6263
}
63-
return _currentInstallationId != null && installation.installationId == _currentInstallationId;
64+
return _currentInstallationId != null &&
65+
installation.installationId == _currentInstallationId;
6466
}
6567

6668
/// Gets the current installation from storage
@@ -75,9 +77,12 @@ class ParseInstallation extends ParseObject {
7577
/// Updates the installation with current device data
7678
_updateInstallation() async {
7779
//Device type
78-
if (Platform.isAndroid) set<String>(keyDeviceType, "android");
79-
else if (Platform.isIOS) set<String>(keyDeviceType, "ios");
80-
else throw Exception("Unsupported platform/operating system");
80+
if (Platform.isAndroid)
81+
set<String>(keyDeviceType, "android");
82+
else if (Platform.isIOS)
83+
set<String>(keyDeviceType, "ios");
84+
else
85+
throw Exception("Unsupported platform/operating system");
8186

8287
//Locale
8388
String locale = await Devicelocale.currentLocale;
@@ -99,7 +104,8 @@ class ParseInstallation extends ParseObject {
99104
Future<ParseResponse> create() async {
100105
var isCurrent = await ParseInstallation.isCurrent(this);
101106
if (isCurrent) await _updateInstallation();
102-
ParseResponse parseResponse = await super.create();
107+
//ParseResponse parseResponse = await super.create();
108+
ParseResponse parseResponse = await _create();
103109
if (parseResponse.success && isCurrent) {
104110
saveInStorage(keyParseStoreInstallation);
105111
}
@@ -110,7 +116,8 @@ class ParseInstallation extends ParseObject {
110116
Future<ParseResponse> save() async {
111117
var isCurrent = await ParseInstallation.isCurrent(this);
112118
if (isCurrent) await _updateInstallation();
113-
ParseResponse parseResponse = await super.save();
119+
//ParseResponse parseResponse = await super.save();
120+
ParseResponse parseResponse = await _save();
114121
if (parseResponse.success && isCurrent) {
115122
saveInStorage(keyParseStoreInstallation);
116123
}
@@ -145,4 +152,76 @@ class ParseInstallation extends ParseObject {
145152
await installation._updateInstallation();
146153
return installation;
147154
}
155+
156+
/// Creates a new object and saves it online
157+
Future<ParseResponse> _create() async {
158+
try {
159+
var uri = _client.data.serverUrl + "$keyEndPointInstallations";
160+
var body = json.encode(toJson(forApiRQ: true));
161+
if (_debug) {
162+
logRequest(ParseCoreData().appName, className,
163+
ParseApiRQ.create.toString(), uri, body);
164+
}
165+
var result = await _client.post(uri, body: body);
166+
167+
//Set the objectId on the object after it is created.
168+
//This allows you to perform operations on the object after creation
169+
if (result.statusCode == 201) {
170+
final map = json.decode(result.body);
171+
this.objectId = map["objectId"].toString();
172+
}
173+
174+
return handleResponse(this, result, ParseApiRQ.create, _debug, className);
175+
} on Exception catch (e) {
176+
return handleException(e, ParseApiRQ.create, _debug, className);
177+
}
178+
}
179+
180+
/// Saves the current object online
181+
Future<ParseResponse> _save() async {
182+
if (getObjectData()[keyVarObjectId] == null) {
183+
return create();
184+
} else {
185+
try {
186+
var uri =
187+
"${ParseCoreData().serverUrl}$keyEndPointInstallations/$objectId";
188+
var body = json.encode(toJson(forApiRQ: true));
189+
if (_debug) {
190+
logRequest(ParseCoreData().appName, className,
191+
ParseApiRQ.save.toString(), uri, body);
192+
}
193+
var result = await _client.put(uri, body: body);
194+
return handleResponse(this, result, ParseApiRQ.save, _debug, className);
195+
} on Exception catch (e) {
196+
return handleException(e, ParseApiRQ.save, _debug, className);
197+
}
198+
}
199+
}
200+
201+
///Subscribes the device to a channel of push notifications.
202+
void subscribeToChannel(String value) {
203+
final List<dynamic> channel = [value];
204+
this.addUnique("channels", channel);
205+
}
206+
207+
///Unsubscribes the device to a channel of push notifications.
208+
void unsubscribeFromChannel(String value) {
209+
final List<dynamic> channel = [value];
210+
this.removeAll("channels", channel);
211+
}
212+
213+
///Returns an <List<String>> containing all the channel names this device is subscribed to.
214+
Future<List<dynamic>> getSubscribedChannels() async {
215+
print("getSubscribedChannels");
216+
final apiResponse =
217+
await ParseObject(keyClassInstallation).getObject(this.objectId);
218+
219+
if (apiResponse.success) {
220+
var installation = apiResponse.result as ParseObject;
221+
print("achou installation");
222+
return Future.value(installation.get<List<dynamic>>("channels"));
223+
} else {
224+
return null;
225+
}
226+
}
148227
}

0 commit comments

Comments
 (0)