Skip to content

Debug feature, cody tidy, remove object futures #11

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 1 commit into from
Dec 30, 2018
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
111 changes: 52 additions & 59 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,67 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {

@override
void initState() {
super.initState();
initParse();
getAllItems();
getSingleItem();
//query();
//queryByContainedIn();
initUser();
runTestQueries();
}

Future<void> initParse() async {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Running Parse init'),
),
floatingActionButton: new FloatingActionButton(onPressed: runTestQueries),
),
);
}

initParse() async {
// Initialize parse
Parse().initialize(
ApplicationConstants.PARSE_APPLICATION_ID,
ApplicationConstants.PARSE_SERVER_URL,
masterKey: ApplicationConstants.PARSE_MASTER_KEY);
masterKey: ApplicationConstants.PARSE_MASTER_KEY,
appName: ApplicationConstants.APP_NAME,
debug: true
);
}

void getAllItems() {
DietPlan().getAll().then((response) {
if (response.success){
runTestQueries(){
getAllItems();
getSingleItem();
query();
initUser();
}

void getAllItems() async {
var dietPlans = await DietPlan().getAll();

for (var plan in response.result) {
if (dietPlans.success) {
for (var plan in dietPlans.result) {
print(ApplicationConstants.APP_NAME + ": " + (plan as DietPlan).name);
}

} else {
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
print(ApplicationConstants.APP_NAME + ": " + dietPlans.exception.message);
}
});
}

void getSingleItem() {
DietPlan().get('R5EonpUDWy').then((response) {
if (response.success){
print(ApplicationConstants.APP_NAME + ": " + (response.result as DietPlan).toString());
} else {
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
}
});
void getSingleItem() async {
var dietPlan = await DietPlan().get('R5EonpUDWy');

if (dietPlan.success) {
print(ApplicationConstants.APP_NAME + ": " + (dietPlan.result as DietPlan).toString());
} else {
print(ApplicationConstants.APP_NAME + ": " + dietPlan.exception.message);
}
}

void query() {
Expand All @@ -66,51 +85,25 @@ class _MyAppState extends State<MyApp> {
..object = DietPlan()
..field = DietPlan.NAME
..equals = ['Paleo']
..query().then((response){

if (response.success){
print(ApplicationConstants.APP_NAME + ": " + ((response.result as List<ParseObject>)[0] as DietPlan).toString());
} else {
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
}
});
}

void queryByContainedIn() {
// Query for an object by name
QueryBuilder()
..object = DietPlan()
..field = DietPlan.NAME
..contains = ['Diet']
..query().then((response){

if (response.success){
print(ApplicationConstants.APP_NAME + ": " + ((response.result as List<ParseObject>)[0] as DietPlan).toString());
..query().then((response) {
if (response.success) {
print(ApplicationConstants.APP_NAME +
": " +
((response.result as List<ParseObject>)[0] as DietPlan)
.toString());
} else {
print(ApplicationConstants.APP_NAME + ": " + response.exception.message);
print(ApplicationConstants.APP_NAME +
": " +
response.exception.message);
}
});
}

Future<void> initUser() async {
User().createNewUser("TestFlutter", "TestPassword123", "[email protected]");
initUser() async {
User().createNewUser("TestFlutter", "TestPassword123", "[email protected]");

User().login().then((val) {
print(val);
});
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Running Parse init'),
),
),
);
}
}
24 changes: 19 additions & 5 deletions lib/data/parse_data_server.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
class ParseDataServer {
static ParseDataServer _instance;

static ParseDataServer get instance => _instance;

static void init(appId, serverUrl, {liveQueryUrl, masterKey, sessionId}) =>
_instance ??= ParseDataServer._init(appId, serverUrl, liveQueryURL: liveQueryUrl, masterKey: masterKey, sessionId: sessionId);
static void init(appId, serverUrl, {debug, appName, liveQueryUrl, masterKey, sessionId}){
_instance ??= ParseDataServer._init(appId, serverUrl);

if (debug != null) _instance..debug = debug;
if (appName != null) _instance..appName = appName;
if (liveQueryUrl != null) _instance..liveQueryURL = liveQueryUrl;
if (masterKey != null) _instance..masterKey = masterKey;
if (sessionId != null) _instance..sessionId = sessionId;
}

String appName;
String applicationId;
String serverUrl;
String liveQueryURL;
String masterKey;
String sessionId;
bool debug;

ParseDataServer._init(this.applicationId, this.serverUrl,
{this.liveQueryURL, this.masterKey, this.sessionId});
ParseDataServer._init(
this.applicationId,
this.serverUrl,
{this.debug: false,
this.appName: "ParseApplication",
this.liveQueryURL,
this.masterKey,
this.sessionId});

factory ParseDataServer() => _instance;

Expand Down
9 changes: 4 additions & 5 deletions lib/network/parse_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import 'package:parse_server_sdk/network/parse_http_client.dart';
import 'package:parse_server_sdk/objects/parse_object.dart';
import 'package:parse_server_sdk/objects/parse_response.dart';

class QueryBuilder implements ParseBaseObject {
// BaseParams
String _className;
class QueryBuilder extends ParseBaseObject {

ParseObject object;
final ParseHTTPClient client = ParseHTTPClient();
String path;
Expand Down Expand Up @@ -40,7 +39,7 @@ class QueryBuilder implements ParseBaseObject {
String get objectId => null;
Map<String, dynamic> objectData = {};

QueryBuilder();
QueryBuilder() : super();

void ascending(String attribute) {}

Expand All @@ -55,7 +54,7 @@ class QueryBuilder implements ParseBaseObject {
}

Future<ParseResponse> query() async {
return object.getQuery(_buildQuery());
return object.query(_buildQuery());
}

String _buildQuery() {
Expand Down
105 changes: 98 additions & 7 deletions lib/objects/parse_base.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,106 @@
import 'dart:convert';

import 'package:meta/meta.dart';
import 'package:parse_server_sdk/network/parse_http_client.dart';

abstract class ParseBaseObject {
final String _className;
final ParseHTTPClient _client;
String _path;
Map<String, dynamic> _objectData;
ParseHTTPClient _client;

Map<String, dynamic> _objectData;
String get objectId => _objectData['objectId'];
DateTime get createdAt => _objectData['createdAt'];
DateTime get updatedAt => _objectData['updatedAt'];

ParseBaseObject([this._client]){
_objectData = Map<String, dynamic>();
}

@protected
setClient(ParseHTTPClient client) => _client = client;
@protected
getDebugStatus() => _client.data.debug;
@protected
getAppName() => _client.data.appName;
@protected
getObjectData() => _objectData;
@protected
fromJson(Map<String, dynamic> objectData) => objectData;

toJson() => JsonEncoder().convert(getObjectData());

copy() {
var copy = fromJson(_objectData);
return JsonDecoder().convert(copy);
}

_getBasePath(String path) => "${_client.data.serverUrl}$path";

setValue(String key, dynamic value, {bool forceUpdate: true}) {
if (value != null) {
if (_objectData.containsKey(key)) {
if (forceUpdate) _objectData[key] = value;
} else {
_objectData[key] = value;
}
}
}

getValue(String key, {dynamic defaultValue, bool fromServer}) {
if (_objectData.containsKey(key)) {
return _objectData[key];
} else {
return defaultValue;
}
}

_get(String objectId, String path) async {
var uri = _getBasePath(path);
if (objectId != null) uri += "/$objectId";
return _client.get(uri);
}

_getAll(String path) async {
return _client.get(_getBasePath(path));
}

@protected
parseGetAll(String path) => _getAll(path);
@protected
parseGetObjectById(String objectId, String path) => _get(objectId, path);

_create(String path) async {
var uri = _client.data.serverUrl + "$path";
return _client.post(uri, body: JsonEncoder().convert(_objectData));
}

@protected
parseCreate(String path, Map objectData) => _create(path);

_save(String path) {
if (_objectData == null) {
return _create(path);
} else {
var uri = "${_getBasePath(path)}/$objectId";
return _client.put(uri, body: JsonEncoder().convert(_objectData));
}
}

@protected
parseSave(String path) => _save(path);

_query(String path, String query) async {
var uri = "${_getBasePath(path)}?$query";
return _client.get(uri);
}

@protected
parseQuery(String path, String query) => _query(path, query);

// ignore: unused_element
void _handleResponse(Map<String, dynamic> response) {}
_delete(String path, String objectId){
var uri = "${_getBasePath(path)}/$objectId";
return _client.delete(uri);
}

ParseBaseObject(this._className, [this._client]);
@protected
parseDelete(String path, String query) => _delete(path, objectId);
}
Loading