-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
230 lines (203 loc) · 6.43 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import 'package:flutter/material.dart';
import 'package:on_upgrade/on_upgrade.dart';
void main() {
runApp(const OnUpgradeExample());
}
class OnUpgradeExample extends StatelessWidget {
const OnUpgradeExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'On Upgrade Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MainScreen(title: 'On Upgrade Example'),
);
}
}
class MainScreen extends StatefulWidget {
const MainScreen({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
final _onUpgrade = OnUpgrade();
late OnUpgrade _onUpgradeCustom;
String _lastVersion = '';
String _customLastVersion = '';
String _currentVersion = '';
var _multipleUpgradeProgress = 0.0;
@override
void initState() {
super.initState();
_onUpgradeCustom = OnUpgrade(
customVersionUpdate: _customVersionSetter,
customVersionLookup: _customVersionGetter,
);
_showLastVersion();
_showCurrentVersion();
}
void _showLastVersion() {
_onUpgrade.getLastVersion().then((value) {
setState(() {
_lastVersion = value;
});
});
_onUpgradeCustom.getLastVersion().then((value) {
setState(() {
_customLastVersion = value;
});
});
}
void _showCurrentVersion() {
_onUpgrade.getCurrentVersion().then((value) {
setState(() {
_currentVersion = value;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Known version (survives app restart): $_lastVersion'),
Text('Current version: $_currentVersion'),
OutlinedButton(
onPressed: _checkAndUpdateVersion,
child: const Text('Check and update version'),
),
OutlinedButton(
onPressed: _executeMultipleUpgrades,
child: const Text('Check and execute multiple upgrades'),
),
Visibility(
visible: _multipleUpgradeProgress > 0.0,
child: LinearProgressIndicator(value: _multipleUpgradeProgress),
),
OutlinedButton(
onPressed: _resetLastVersion,
child: const Text('Reset last version'),
),
const Padding(
padding: EdgeInsets.all(16.0),
child: Divider(),
),
Text('Known custom version: $_customLastVersion'),
Text('Current version: $_currentVersion'),
OutlinedButton(
onPressed: _checkAndUpdateCustomVersion,
child: const Text('Check and update custom version'),
),
OutlinedButton(
onPressed: _resetCustomLastVersion,
child: const Text('Reset custom last version'),
),
],
),
),
);
}
// Default usage example
Future<void> _checkAndUpdateVersion() async {
final isNewVersion = await _onUpgrade.isNewVersion();
if (isNewVersion.state == UpgradeState.upgrade) {
await _onUpgrade.updateLastVersion();
_showLastVersion();
_showSnackbar('An upgrade was detected.');
} else {
_showSnackbar('No upgrade.');
}
}
Future<void> _executeMultipleUpgrades() async {
final upgrades = {
'0.0.5': () async {
setState(() => _multipleUpgradeProgress = 0.1);
await Future.delayed(const Duration(seconds: 2),
() => setState(() => _multipleUpgradeProgress = 0.5));
_showSnackbar('0.0.5 migration done');
},
'0.5.0': () async {
await Future.delayed(const Duration(seconds: 3),
() => setState(() => _multipleUpgradeProgress = 1));
_showSnackbar('0.5.0 migration done');
await Future.delayed(const Duration(seconds: 2), () {});
},
'1.0.0': () {
showDialog(
context: context,
builder: (context) => const AlertDialog(
title: Text('1.0.0 - Nothing new'),
content: Text('We just added more ads!'),
),
);
},
'2.0.0': () {
// Shouldn't get executed
throw AssertionError("This method shouldn't get executed");
},
};
final isNewVersion = await _onUpgrade.isNewVersion();
if (isNewVersion.state == UpgradeState.upgrade) {
await isNewVersion.executeUpgrades(upgrades);
await _onUpgrade.updateLastVersion();
_showLastVersion();
} else {
_showSnackbar('No upgrade.');
}
}
// Custom implementation example
// Loading the last version, e.g. from the database
Future<String> _customVersionGetter() async {
return Future.delayed(const Duration(milliseconds: 500), () {
return _customLastVersion;
});
}
// Saving the version, e.g. to a database
Future<bool> _customVersionSetter([String? version]) async {
return Future.delayed(const Duration(milliseconds: 500), () async {
if (version != null) {
_customLastVersion = version;
} else {
final currentVersion = await _onUpgradeCustom.getCurrentVersion();
_customLastVersion = currentVersion;
}
return true;
});
}
// Custom usage example
Future<void> _checkAndUpdateCustomVersion() async {
final isNewVersion = await _onUpgradeCustom.isNewVersion();
if (isNewVersion.state == UpgradeState.upgrade) {
await _onUpgradeCustom.updateLastVersion();
_showLastVersion();
_showSnackbar('A custom upgrade was detected.');
} else {
_showSnackbar('No custom upgrade.');
}
}
// Helpers
void _showSnackbar(String text) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
final snackBar = SnackBar(content: Text(text));
ScaffoldMessenger.of(context).showSnackBar((snackBar));
}
Future<void> _resetLastVersion() async {
await _onUpgrade.updateLastVersion('');
_showLastVersion();
_resetMultipleProgress();
}
void _resetMultipleProgress() =>
setState(() => _multipleUpgradeProgress = 0.0);
Future<void> _resetCustomLastVersion() async {
await _onUpgradeCustom.updateLastVersion('');
_showLastVersion();
}
}