Skip to content

Commit 7ecf758

Browse files
committed
Show update notifications
1 parent f461526 commit 7ecf758

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

lib/models/db/prefs_store.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class PrefsStore {
1818
static const CURRENCY_RATE_PERIOD = "CURRENCY_RATE_PERIOD";
1919
static const AMOUNT_VIEW_TYPE = "AMOUNT_VIEW_TYPE";
2020
static const DOJO = "DOJO";
21+
static const SHOW_UPDATE_NOTIFICATION = "SHOW_UPDATE_NOTIFICATION";
2122

2223

2324
static PrefsStore get instance => _singleton;
@@ -72,11 +73,15 @@ class PrefsStore {
7273
}
7374
}
7475

75-
Future<bool> getBool(String key) async {
76+
Future<bool> getBool(String key,{bool defaultValue}) async {
7677
try {
7778
var _value = await store.record(key).get(database) as bool;
7879
if (_value == null) {
79-
return Future.value(false);
80+
if(defaultValue == null){
81+
return Future.value(false);
82+
}else{
83+
return Future.value(defaultValue);
84+
}
8085
}
8186
return Future.value(_value);
8287
} catch (e) {

lib/screens/home.dart

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
66
import 'package:provider/provider.dart';
77
import 'package:sentinelx/channels/network_channel.dart';
88
import 'package:sentinelx/channels/system_channel.dart';
9+
import 'package:sentinelx/models/db/prefs_store.dart';
910
import 'package:sentinelx/models/tx.dart';
1011
import 'package:sentinelx/screens/Receive/receive_screen.dart';
1112
import 'package:sentinelx/screens/dojo_configure.dart';
@@ -333,15 +334,18 @@ class _HomeState extends State<Home> {
333334
}
334335

335336
void checkUpdate() async {
336-
try {
337-
Map<String, dynamic> update = await AppState().checkUpdate();
338-
if (update.containsKey("isUpToDate")) {
339-
if (update['isUpToDate'] as bool == false) {
340-
SystemChannel().showUpdateNotification(update['newVersion']);
337+
bool check = await PrefsStore().getBool(PrefsStore.SHOW_UPDATE_NOTIFICATION, defaultValue: true);
338+
if (check) {
339+
try {
340+
Map<String, dynamic> update = await AppState().checkUpdate();
341+
if (update.containsKey("isUpToDate")) {
342+
if (update['isUpToDate'] as bool == false) {
343+
SystemChannel().showUpdateNotification(update['newVersion']);
344+
}
341345
}
346+
} catch (e) {
347+
print(e);
342348
}
343-
} catch (e) {
344-
print(e);
345349
}
346350
}
347351

lib/screens/settings/update_screen.dart

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import 'dart:convert';
22
import 'dart:math';
33
import 'dart:ui';
44

5+
import 'package:flutter/cupertino.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/services.dart';
78
import 'package:flutter_markdown/flutter_markdown.dart';
89
import 'package:pub_semver/pub_semver.dart';
910
import 'package:sentinelx/channels/api_channel.dart';
1011
import 'package:sentinelx/channels/system_channel.dart';
12+
import 'package:sentinelx/models/db/prefs_store.dart';
1113
import 'package:sentinelx/shared_state/app_state.dart';
1214
import 'package:sentinelx/widgets/appbar_bottom_progress.dart';
15+
import 'package:sentinelx/widgets/circular_check_box.dart';
1316
import 'package:sentinelx/widgets/confirm_modal.dart';
1417

1518
class UpdateCheck extends StatefulWidget {
@@ -26,6 +29,7 @@ class _UpdateCheckState extends State<UpdateCheck> {
2629
bool isUpToDate = false;
2730
List<Assets> downloadAssets = [];
2831
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
32+
bool showUpdateNotification = false;
2933

3034
@override
3135
Widget build(BuildContext context) {
@@ -44,11 +48,37 @@ class _UpdateCheckState extends State<UpdateCheck> {
4448
title: Text("Version: $version "),
4549
subtitle: Text("Build: $buildNumber ", style: Theme.of(context).textTheme.caption),
4650
),
51+
Divider(),
4752
ListTile(
4853
onTap: this.checkVersion,
4954
title: Text("Check for Update"),
5055
subtitle: Text("This action will use github api to check new releases", style: Theme.of(context).textTheme.caption),
5156
),
57+
Divider(),
58+
ListTile(
59+
onTap: () async {
60+
bool val = !showUpdateNotification;
61+
await PrefsStore().put(PrefsStore.SHOW_UPDATE_NOTIFICATION, val);
62+
setState(() {
63+
showUpdateNotification = val;
64+
});
65+
},
66+
title: Text("Notify new updates"),
67+
trailing: Switch(
68+
value: showUpdateNotification,
69+
onChanged: (val) async {
70+
await PrefsStore().put(PrefsStore.SHOW_UPDATE_NOTIFICATION, val);
71+
setState(() {
72+
showUpdateNotification = val;
73+
});
74+
},
75+
),
76+
subtitle: Text(
77+
"Show notification when new update released.\n"
78+
"note: app will only check updates every start up",
79+
style: Theme.of(context).textTheme.caption),
80+
),
81+
Divider(),
5282
ListTile(
5383
onTap: () async {
5484
await SystemChannel().openURL("https://github.com/InvertedX/sentinelx");
@@ -58,7 +88,19 @@ class _UpdateCheckState extends State<UpdateCheck> {
5888
style: Theme.of(context).textTheme.subhead,
5989
),
6090
subtitle: Text("github.com/InvertedX/sentinelx", style: Theme.of(context).textTheme.caption),
61-
)
91+
),
92+
(changeLog.isNotEmpty && downloadAssets.length == 0)
93+
? ListTile(
94+
onTap: () async {
95+
this.showChangeLog(version);
96+
},
97+
title: Text(
98+
"Show Change Log",
99+
style: Theme.of(context).textTheme.subhead,
100+
),
101+
subtitle: Text("View current change log", style: Theme.of(context).textTheme.caption),
102+
)
103+
: SizedBox.shrink()
62104
]),
63105
),
64106
SliverToBoxAdapter(
@@ -111,7 +153,7 @@ class _UpdateCheckState extends State<UpdateCheck> {
111153
"Open change log",
112154
style: Theme.of(context).textTheme.subtitle,
113155
),
114-
onTap: this.showChangeLog,
156+
onTap: () => this.showChangeLog(newVersion),
115157
),
116158
Divider(
117159
thickness: 2,
@@ -126,17 +168,17 @@ class _UpdateCheckState extends State<UpdateCheck> {
126168
children: <Widget>[
127169
Divider(),
128170
Padding(
129-
padding: EdgeInsets.all(12),
171+
padding: EdgeInsets.all(9),
130172
),
131173
ClipRRect(
132174
child: Container(
133175
child: Icon(
134176
Icons.check,
135177
color: Colors.white,
136-
size: 24,
178+
size: 22,
137179
),
138180
padding: EdgeInsets.all(12),
139-
color: Colors.greenAccent[700],
181+
color: Colors.greenAccent[700].withOpacity(0.8),
140182
),
141183
borderRadius: BorderRadius.circular(50),
142184
),
@@ -168,10 +210,13 @@ class _UpdateCheckState extends State<UpdateCheck> {
168210

169211
void init() async {
170212
Map<String, dynamic> packageInfo = await SystemChannel().getPackageInfo();
213+
bool enabledNotifications = await PrefsStore().getBool(PrefsStore.SHOW_UPDATE_NOTIFICATION, defaultValue: true);
171214
setState(() {
172215
version = packageInfo["version"];
216+
showUpdateNotification = enabledNotifications;
173217
buildNumber = packageInfo['buildNumber'];
174218
});
219+
checkVersion();
175220
}
176221

177222
void checkVersion() async {
@@ -245,14 +290,15 @@ class _UpdateCheckState extends State<UpdateCheck> {
245290
}
246291
}
247292

248-
void showChangeLog() {
293+
void showChangeLog(String version) {
294+
String log = "## version $version \n\n ${changeLog}";
249295
showModalBottomSheet(
250296
context: context,
251297
builder: (context) {
252298
return Card(
253299
child: Container(
254300
child: Markdown(
255-
data: changeLog,
301+
data: log,
256302
),
257303
),
258304
);

lib/shared_state/app_state.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class AppState extends ChangeNotifier {
159159
Map<String, dynamic> latest = jsonArray[0];
160160
String latestVersion = latest['tag_name'].replaceFirst("v", "");
161161
String changeLogBody = latest['body'];
162-
Version current = Version.parse("0.1.3");
162+
// Version current = Version.parse("0.1.3");
163+
Version current = Version.parse(packageInfo["version"]);
163164
if (current.compareTo(Version.parse(latestVersion)) < 0) {
164165
return {
165166
"newVersion": latestVersion,
@@ -169,7 +170,7 @@ class AppState extends ChangeNotifier {
169170
"downloadAssets": latest.containsKey("assets") ? latest['assets'] : []
170171
};
171172
} else {
172-
return {"newVersion": "", "isUpToDate": true, "changeLog": "", "isUpToDate": true, "downloadAssets": []};
173+
return {"newVersion": "", "isUpToDate": true, "changeLog": changeLogBody, "isUpToDate": true, "downloadAssets": []};
173174
}
174175
}
175176

0 commit comments

Comments
 (0)