Skip to content

Commit

Permalink
Merge pull request #30 from zino-app/patch-6
Browse files Browse the repository at this point in the history
Patch 6
  • Loading branch information
HofmannZ authored Jul 13, 2018
2 parents ff1cc7e + b4725be commit ff6ee91
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## [0.5.3] - July 13 2018

### Breaking change

n/a

#### Fixes / Enhancements

- Added polling timer as a variable for easy deletion on dispose
- Fixed bug when Query timer is still active when the Query is disposed
- Added instant query fetch when the query variables are updated

#### Docs

n/a

## [0.5.2] - July 11 2018

### Breaking change
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ First depend on the library by adding this to your packages `pubspec.yaml`:

```yaml
dependencies:
graphql_flutter: ^0.5.2
graphql_flutter: ^0.5.3
```
Now inside your Dart code you can import it.
Expand Down
62 changes: 56 additions & 6 deletions lib/src/widgets/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,40 @@ class Query extends StatefulWidget {
QueryState createState() => new QueryState();
}

class QueryState extends State<Query> with WidgetsBindingObserver {
class QueryState extends State<Query> {
bool loading = true;
Map<String, dynamic> data = {};
String error = '';
Duration pollInterval;
Timer pollTimer;
bool initialFetch = false;
Map currentVariables = new Map();

@override
void initState() {
super.initState();

if (widget.pollInterval != null) {
if (widget.pollInterval is int) {
pollInterval = new Duration(seconds: widget.pollInterval);
}

getQueryResult();
}

@override
void dispose() {
_deleteTimer();

super.dispose();
}

void _deleteTimer() {
if (pollTimer is Timer) {
pollTimer.cancel();
pollTimer = null;
}
}

void getQueryResult() async {
try {
final Map<String, dynamic> result = client.readQuery(
Expand Down Expand Up @@ -71,8 +88,8 @@ class QueryState extends State<Query> with WidgetsBindingObserver {
data = result;
});

if (widget.pollInterval != null) {
new Timer(pollInterval, getQueryResult);
if (pollInterval is Duration && !(pollTimer is Timer)) {
pollTimer = new Timer(pollInterval, () => getQueryResult());
}
} catch (e) {
if (data == {}) {
Expand All @@ -82,16 +99,49 @@ class QueryState extends State<Query> with WidgetsBindingObserver {
});
}

if (widget.pollInterval != null) {
new Timer(pollInterval, getQueryResult);
if (pollInterval is Duration && !(pollTimer is Timer)) {
pollTimer = new Timer(pollInterval, () => getQueryResult());
}

// TODO: handle error
print(e.toString());
}
}

bool _areDifferentMaps(Map a, Map b) {
if (a.length != b.length) {
return true;
}

bool areDifferent = false;

a.forEach((key, value) {
if (b[key] != a[key] || (!b.containsKey(key))) {
areDifferent = true;
}
});

return areDifferent;
}

Widget build(BuildContext context) {
if (!initialFetch) {
initialFetch = true;

currentVariables = widget.variables;

getQueryResult();
}

if (_areDifferentMaps(currentVariables, widget.variables)) {
currentVariables = widget.variables;

loading = true;
_deleteTimer();

getQueryResult();
}

return widget.builder(
loading: loading,
error: error,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: graphql_flutter
description: A GraphQL client for Flutter.
version: 0.5.2
version: 0.5.3
authors:
- Eus Dima <[email protected]>
- Zino Hofmann <[email protected]>
Expand Down

0 comments on commit ff6ee91

Please sign in to comment.