-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathcache_provider.dart
85 lines (65 loc) · 1.91 KB
/
cache_provider.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'package:flutter/material.dart';
import 'package:graphql/client.dart';
import 'package:graphql_flutter/src/widgets/graphql_provider.dart';
class CacheProvider extends StatefulWidget {
const CacheProvider({
final Key? key,
required this.child,
}) : super(key: key);
final Widget child;
@override
_CacheProviderState createState() => _CacheProviderState();
}
class _CacheProviderState extends State<CacheProvider>
with WidgetsBindingObserver {
GraphQLClient? client;
/// This allows a value of type T or T?
/// to be treated as a value of type T?.
///
/// We use this so that APIs that have become
/// non-nullable can still be used with `!` and `?`
/// to support older versions of the API as well.
T? _ambiguate<T>(T? value) => value;
@override
void initState() {
super.initState();
_ambiguate(WidgetsBinding.instance)!.addObserver(this);
}
@override
void didChangeDependencies() {
/// Gets the client from the closest wrapping [GraphqlProvider].
client = GraphQLProvider.of(context).value;
assert(client != null);
// client.cache?.restore();
super.didChangeDependencies();
}
@override
void dispose() {
super.dispose();
_ambiguate(WidgetsBinding.instance)!.removeObserver(this);
}
/*
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
assert(client != null);
switch (state) {
// TODO: from @degroote22 in #175: reconsider saving on `inactive`
// When the app is 'cold-started', save won't be called and
// restore will run ok.
case AppLifecycleState.inactive:
client.cache?.save();
break;
case AppLifecycleState.paused:
client.cache?.save();
break;
case AppLifecycleState.resumed:
client.cache?.restore();
break;
default:
break;
}
}
*/
@override
Widget build(BuildContext context) => widget.child;
}