Skip to content

Support Relation #179

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 7 commits into from
May 31, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ build/
.idea
example/ios/Frameworks/
example/lib/ui/

.vscode/
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ Parse().initialize(
ApplicationConstants.keyApplicationId,
ApplicationConstants.keyParseServerUrl);
```
if you want to use secure storage also that's allow using sdk on desktop application
```dart

Parse().initialize(keyParseApplicationId, keyParseServerUrl,
masterKey: keyParseMasterKey,
debug: true,
coreStore: CoreStoreImp.getInstance());
```
It's possible to add other params, such as ...

```dart
Expand Down Expand Up @@ -530,6 +537,21 @@ final Map<String, String> params = <String, String>{'plan': 'paid'};
function.execute(parameters: params);
```

## Relation
The SDK supports Relation.

To Retrive a relation instance for user, call:
```dart
final relation = user.getRelation('dietPlans');
```

and then you can add a relation to the passed in object.

```dart
relation.add(dietPlan);
final result = await user.save();
```

## Other Features of this library
Main:
* Installation (View the example application)
Expand All @@ -552,5 +574,5 @@ Objects:
## Author:-
This project was authored by Phill Wiggins. You can contact me at [email protected]
<!--stackedit_data:
eyJoaXN0b3J5IjpbNzE4NjUwNDIwXX0=
-->
eyJoaXN0b3J5IjpbLTU4MDA4MDUwNCw3MTg2NTA0MjBdfQ==
-->
Binary file added example/assets/parse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
24DF2572E6AEEB9F7CE180C9 /* libPods-Runner.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Runner.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
5804EFBD11740E02FC51BC3E /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
96499D95196B10F296043703 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
Expand Down
4 changes: 2 additions & 2 deletions example/lib/data/model/diet_plan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class DietPlan extends ParseObject implements ParseCloneable {
num get fat => get<num>(keyFat);
set fat(num fat) => set<num>(keyFat, fat);

int get status => get<int>(keyStatus);
set status(int status) => set<int>(keyStatus, status);
bool get status => get<bool>(keyStatus);
set status(bool status) => set<bool>(keyStatus, status);
}
97 changes: 97 additions & 0 deletions example/lib/pages/decision_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter_plugin_example/data/repositories/diet_plan/provider_api_diet_plan.dart';
import 'package:flutter_plugin_example/domain/constants/application_constants.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

import 'home_page.dart';
import 'login_page.dart';

class DecisionPage extends StatefulWidget {
@override
_DecisionPageState createState() => _DecisionPageState();
}

class _DecisionPageState extends State<DecisionPage> {
String _parseServerState = 'Checking Parse Server...';

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_initParse();
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_showLogo(),
const SizedBox(
height: 20,
),
Center(
child: Text(_parseServerState),
),
],
),
),
),
);
}

Widget _showLogo() {
return Hero(
tag: 'hero',
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 70.0, 0.0, 0.0),
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: 48.0,
child: Image.asset('assets/parse.png'),
),
),
);
}

Future<void> _initParse() async {
try {
Parse().initialize(keyParseApplicationId, keyParseServerUrl,
masterKey: keyParseMasterKey, debug: true);
final ParseResponse response = await Parse().healthCheck();
if (response.success) {
final ParseUser user = await ParseUser.currentUser();
if (user != null) {
_redirectToPage(context, HomePage(DietPlanProviderApi()));
} else {
_redirectToPage(context, LoginPage());
}
} else {
setState(() {
_parseServerState =
'Parse Server Not avaiable\n due to ${response.error.toString()}';
});
}
} catch (e) {
setState(() {
_parseServerState = e.toString();
});
}
}

Future<void> _redirectToPage(BuildContext context, Widget page) async {
final MaterialPageRoute<bool> newRoute =
MaterialPageRoute<bool>(builder: (BuildContext context) => page);

bool nav = await Navigator.of(context)
.pushAndRemoveUntil<bool>(newRoute, ModalRoute.withName('/'));
if (nav == true) {
_initParse();
}
}
}
133 changes: 133 additions & 0 deletions example/lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_plugin_example/data/base/api_response.dart';
import 'package:flutter_plugin_example/data/model/diet_plan.dart';
import 'package:flutter_plugin_example/data/repositories/diet_plan/contract_provider_diet_plan.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

class HomePage extends StatefulWidget {
HomePage(this._dietPlanProvider);
final DietPlanProviderContract _dietPlanProvider;

@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
List<DietPlan> randomDietPlans = [];

@override
void initState() {
super.initState();
final List<dynamic> json = const JsonDecoder().convert(dietPlansToAdd);
for (final Map<String, dynamic> element in json) {
final DietPlan dietPlan = DietPlan().fromJson(element);
randomDietPlans.add(dietPlan);
}
}

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const Text('Parse Server demo'),
actions: <Widget>[
FlatButton(
child: Text('Logout',
style: TextStyle(fontSize: 17.0, color: Colors.white)),
onPressed: () async {
final ParseUser user = await ParseUser.currentUser();
user.logout(deleteLocalUserData: true);
Navigator.pop(context, true);
})
],
),
body: _showDietList(),
floatingActionButton: FloatingActionButton(
onPressed: () async {
DietPlan dietPlan =
randomDietPlans[Random().nextInt(randomDietPlans.length - 1)];
ParseUser user = await ParseUser.currentUser();
dietPlan.set('user', user);
await widget._dietPlanProvider.add(dietPlan);
setState(() {});
},
tooltip: 'Add Diet Plans',
child: const Icon(Icons.add),
)),
);
}

Widget _showDietList() {
return FutureBuilder<ApiResponse>(
future: widget._dietPlanProvider.getAll(),
builder: (BuildContext context, AsyncSnapshot<ApiResponse> snapshot) {
if (snapshot.hasData) {
if (snapshot.data.success) {
if (snapshot.data.results == null ||
snapshot.data.results.isEmpty) {
return Center(
child: const Text('No Data'),
);
}
}
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.results.length,
itemBuilder: (BuildContext context, int index) {
DietPlan dietPlan = snapshot.data.results[index];
String id = dietPlan.objectId;
String name = dietPlan.name;
String description = dietPlan.description;
bool status = dietPlan.status;
return Dismissible(
key: Key(id),
background: Container(color: Colors.red),
onDismissed: (direction) async {
widget._dietPlanProvider.remove(dietPlan);
},
child: ListTile(
title: Text(
name,
style: TextStyle(fontSize: 20.0),
),
subtitle: Text(description),
trailing: IconButton(
icon: status
? const Icon(
Icons.done_outline,
color: Colors.green,
size: 20.0,
)
: const Icon(Icons.done,
color: Colors.grey, size: 20.0),
onPressed: () async {
dietPlan.status = !dietPlan.status;
await dietPlan.save();
setState(() {});
}),
),
);
});
} else {
return Center(
child: const Text('No Data'),
);
}
});
}

String dietPlansToAdd =
'[{"className":"Diet_Plans","Name":"Textbook","Description":"For an active lifestyle and a straight forward macro plan, we suggest this plan.","Fat":25,"Carbs":50,"Protein":25,"Status":false},'
'{"className":"Diet_Plans","Name":"Body Builder","Description":"Default Body Builders Diet","Fat":20,"Carbs":40,"Protein":40,"Status":true},'
'{"className":"Diet_Plans","Name":"Zone Diet","Description":"Popular with CrossFit users. Zone Diet targets similar macros.","Fat":30,"Carbs":40,"Protein":30,"Status":true},'
'{"className":"Diet_Plans","Name":"Low Fat","Description":"Low fat diet.","Fat":15,"Carbs":60,"Protein":25,"Status":false},'
'{"className":"Diet_Plans","Name":"Low Carb","Description":"Low Carb diet, main focus on quality fats and protein.","Fat":35,"Carbs":25,"Protein":40,"Status":true},'
'{"className":"Diet_Plans","Name":"Paleo","Description":"Paleo diet.","Fat":60,"Carbs":25,"Protein":10,"Status":false},'
'{"className":"Diet_Plans","Name":"Ketogenic","Description":"High quality fats, low carbs.","Fat":65,"Carbs":5,"Protein":30,"Status":true}]';
}
Loading