Skip to content

Commit fea5fb4

Browse files
authored
Merge pull request #11 from phillwiggins/develop
Debug feature, cody tidy, remove object futures
2 parents 80a9d00 + beff874 commit fea5fb4

File tree

9 files changed

+242
-174
lines changed

9 files changed

+242
-174
lines changed

example/lib/main.dart

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,67 @@ class MyApp extends StatefulWidget {
1616
}
1717

1818
class _MyAppState extends State<MyApp> {
19+
1920
@override
2021
void initState() {
2122
super.initState();
2223
initParse();
23-
getAllItems();
24-
getSingleItem();
25-
//query();
26-
//queryByContainedIn();
27-
initUser();
24+
runTestQueries();
2825
}
2926

30-
Future<void> initParse() async {
27+
@override
28+
Widget build(BuildContext context) {
29+
return new MaterialApp(
30+
home: new Scaffold(
31+
appBar: new AppBar(
32+
title: const Text('Plugin example app'),
33+
),
34+
body: new Center(
35+
child: new Text('Running Parse init'),
36+
),
37+
floatingActionButton: new FloatingActionButton(onPressed: runTestQueries),
38+
),
39+
);
40+
}
3141

42+
initParse() async {
3243
// Initialize parse
3344
Parse().initialize(
3445
ApplicationConstants.PARSE_APPLICATION_ID,
3546
ApplicationConstants.PARSE_SERVER_URL,
36-
masterKey: ApplicationConstants.PARSE_MASTER_KEY);
47+
masterKey: ApplicationConstants.PARSE_MASTER_KEY,
48+
appName: ApplicationConstants.APP_NAME,
49+
debug: true
50+
);
3751
}
3852

39-
void getAllItems() {
40-
DietPlan().getAll().then((response) {
41-
if (response.success){
53+
runTestQueries(){
54+
getAllItems();
55+
getSingleItem();
56+
query();
57+
initUser();
58+
}
59+
60+
void getAllItems() async {
61+
var dietPlans = await DietPlan().getAll();
4262

43-
for (var plan in response.result) {
63+
if (dietPlans.success) {
64+
for (var plan in dietPlans.result) {
4465
print(ApplicationConstants.APP_NAME + ": " + (plan as DietPlan).name);
4566
}
46-
4767
} else {
48-
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
68+
print(ApplicationConstants.APP_NAME + ": " + dietPlans.exception.message);
4969
}
50-
});
5170
}
5271

53-
void getSingleItem() {
54-
DietPlan().get('R5EonpUDWy').then((response) {
55-
if (response.success){
56-
print(ApplicationConstants.APP_NAME + ": " + (response.result as DietPlan).toString());
57-
} else {
58-
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
59-
}
60-
});
72+
void getSingleItem() async {
73+
var dietPlan = await DietPlan().get('R5EonpUDWy');
74+
75+
if (dietPlan.success) {
76+
print(ApplicationConstants.APP_NAME + ": " + (dietPlan.result as DietPlan).toString());
77+
} else {
78+
print(ApplicationConstants.APP_NAME + ": " + dietPlan.exception.message);
79+
}
6180
}
6281

6382
void query() {
@@ -66,51 +85,25 @@ class _MyAppState extends State<MyApp> {
6685
..object = DietPlan()
6786
..field = DietPlan.NAME
6887
..equals = ['Paleo']
69-
..query().then((response){
70-
71-
if (response.success){
72-
print(ApplicationConstants.APP_NAME + ": " + ((response.result as List<ParseObject>)[0] as DietPlan).toString());
73-
} else {
74-
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
75-
}
76-
});
77-
}
78-
79-
void queryByContainedIn() {
80-
// Query for an object by name
81-
QueryBuilder()
82-
..object = DietPlan()
83-
..field = DietPlan.NAME
84-
..contains = ['Diet']
85-
..query().then((response){
86-
87-
if (response.success){
88-
print(ApplicationConstants.APP_NAME + ": " + ((response.result as List<ParseObject>)[0] as DietPlan).toString());
88+
..query().then((response) {
89+
if (response.success) {
90+
print(ApplicationConstants.APP_NAME +
91+
": " +
92+
((response.result as List<ParseObject>)[0] as DietPlan)
93+
.toString());
8994
} else {
90-
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
95+
print(ApplicationConstants.APP_NAME +
96+
": " +
97+
response.exception.message);
9198
}
9299
});
93100
}
94101

95-
Future<void> initUser() async {
96-
User().createNewUser("TestFlutter", "TestPassword123", "TestEmail@Email.com");
102+
initUser() async {
103+
User().createNewUser("TestFlutter", "TestPassword123", "TestEmail@Email.com");
97104

98105
User().login().then((val) {
99106
print(val);
100107
});
101108
}
102-
103-
@override
104-
Widget build(BuildContext context) {
105-
return new MaterialApp(
106-
home: new Scaffold(
107-
appBar: new AppBar(
108-
title: const Text('Plugin example app'),
109-
),
110-
body: new Center(
111-
child: new Text('Running Parse init'),
112-
),
113-
),
114-
);
115-
}
116109
}

lib/data/parse_data_server.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
class ParseDataServer {
22
static ParseDataServer _instance;
3-
43
static ParseDataServer get instance => _instance;
54

6-
static void init(appId, serverUrl, {liveQueryUrl, masterKey, sessionId}) =>
7-
_instance ??= ParseDataServer._init(appId, serverUrl, liveQueryURL: liveQueryUrl, masterKey: masterKey, sessionId: sessionId);
5+
static void init(appId, serverUrl, {debug, appName, liveQueryUrl, masterKey, sessionId}){
6+
_instance ??= ParseDataServer._init(appId, serverUrl);
7+
8+
if (debug != null) _instance..debug = debug;
9+
if (appName != null) _instance..appName = appName;
10+
if (liveQueryUrl != null) _instance..liveQueryURL = liveQueryUrl;
11+
if (masterKey != null) _instance..masterKey = masterKey;
12+
if (sessionId != null) _instance..sessionId = sessionId;
13+
}
814

15+
String appName;
916
String applicationId;
1017
String serverUrl;
1118
String liveQueryURL;
1219
String masterKey;
1320
String sessionId;
21+
bool debug;
1422

15-
ParseDataServer._init(this.applicationId, this.serverUrl,
16-
{this.liveQueryURL, this.masterKey, this.sessionId});
23+
ParseDataServer._init(
24+
this.applicationId,
25+
this.serverUrl,
26+
{this.debug: false,
27+
this.appName: "ParseApplication",
28+
this.liveQueryURL,
29+
this.masterKey,
30+
this.sessionId});
1731

1832
factory ParseDataServer() => _instance;
1933

lib/network/parse_query.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import 'package:parse_server_sdk/network/parse_http_client.dart';
66
import 'package:parse_server_sdk/objects/parse_object.dart';
77
import 'package:parse_server_sdk/objects/parse_response.dart';
88

9-
class QueryBuilder implements ParseBaseObject {
10-
// BaseParams
11-
String _className;
9+
class QueryBuilder extends ParseBaseObject {
10+
1211
ParseObject object;
1312
final ParseHTTPClient client = ParseHTTPClient();
1413
String path;
@@ -40,7 +39,7 @@ class QueryBuilder implements ParseBaseObject {
4039
String get objectId => null;
4140
Map<String, dynamic> objectData = {};
4241

43-
QueryBuilder();
42+
QueryBuilder() : super();
4443

4544
void ascending(String attribute) {}
4645

@@ -55,7 +54,7 @@ class QueryBuilder implements ParseBaseObject {
5554
}
5655

5756
Future<ParseResponse> query() async {
58-
return object.getQuery(_buildQuery());
57+
return object.query(_buildQuery());
5958
}
6059

6160
String _buildQuery() {

lib/objects/parse_base.dart

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,106 @@
1+
import 'dart:convert';
2+
3+
import 'package:meta/meta.dart';
14
import 'package:parse_server_sdk/network/parse_http_client.dart';
25

36
abstract class ParseBaseObject {
4-
final String _className;
5-
final ParseHTTPClient _client;
6-
String _path;
7-
Map<String, dynamic> _objectData;
7+
ParseHTTPClient _client;
88

9+
Map<String, dynamic> _objectData;
910
String get objectId => _objectData['objectId'];
11+
DateTime get createdAt => _objectData['createdAt'];
12+
DateTime get updatedAt => _objectData['updatedAt'];
13+
14+
ParseBaseObject([this._client]){
15+
_objectData = Map<String, dynamic>();
16+
}
17+
18+
@protected
19+
setClient(ParseHTTPClient client) => _client = client;
20+
@protected
21+
getDebugStatus() => _client.data.debug;
22+
@protected
23+
getAppName() => _client.data.appName;
24+
@protected
25+
getObjectData() => _objectData;
26+
@protected
27+
fromJson(Map<String, dynamic> objectData) => objectData;
28+
29+
toJson() => JsonEncoder().convert(getObjectData());
30+
31+
copy() {
32+
var copy = fromJson(_objectData);
33+
return JsonDecoder().convert(copy);
34+
}
35+
36+
_getBasePath(String path) => "${_client.data.serverUrl}$path";
37+
38+
setValue(String key, dynamic value, {bool forceUpdate: true}) {
39+
if (value != null) {
40+
if (_objectData.containsKey(key)) {
41+
if (forceUpdate) _objectData[key] = value;
42+
} else {
43+
_objectData[key] = value;
44+
}
45+
}
46+
}
47+
48+
getValue(String key, {dynamic defaultValue, bool fromServer}) {
49+
if (_objectData.containsKey(key)) {
50+
return _objectData[key];
51+
} else {
52+
return defaultValue;
53+
}
54+
}
55+
56+
_get(String objectId, String path) async {
57+
var uri = _getBasePath(path);
58+
if (objectId != null) uri += "/$objectId";
59+
return _client.get(uri);
60+
}
61+
62+
_getAll(String path) async {
63+
return _client.get(_getBasePath(path));
64+
}
65+
66+
@protected
67+
parseGetAll(String path) => _getAll(path);
68+
@protected
69+
parseGetObjectById(String objectId, String path) => _get(objectId, path);
70+
71+
_create(String path) async {
72+
var uri = _client.data.serverUrl + "$path";
73+
return _client.post(uri, body: JsonEncoder().convert(_objectData));
74+
}
75+
76+
@protected
77+
parseCreate(String path, Map objectData) => _create(path);
78+
79+
_save(String path) {
80+
if (_objectData == null) {
81+
return _create(path);
82+
} else {
83+
var uri = "${_getBasePath(path)}/$objectId";
84+
return _client.put(uri, body: JsonEncoder().convert(_objectData));
85+
}
86+
}
87+
88+
@protected
89+
parseSave(String path) => _save(path);
90+
91+
_query(String path, String query) async {
92+
var uri = "${_getBasePath(path)}?$query";
93+
return _client.get(uri);
94+
}
95+
96+
@protected
97+
parseQuery(String path, String query) => _query(path, query);
1098

11-
// ignore: unused_element
12-
void _handleResponse(Map<String, dynamic> response) {}
99+
_delete(String path, String objectId){
100+
var uri = "${_getBasePath(path)}/$objectId";
101+
return _client.delete(uri);
102+
}
13103

14-
ParseBaseObject(this._className, [this._client]);
104+
@protected
105+
parseDelete(String path, String query) => _delete(path, objectId);
15106
}

0 commit comments

Comments
 (0)