Skip to content

Commit

Permalink
feat: Implement version check
Browse files Browse the repository at this point in the history
  • Loading branch information
4o3F committed Nov 25, 2023
1 parent ba05d63 commit 52e31ba
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@
"copy_button": "Copy",
"copied": "Copied"
}
},
"update": {
"title": "New version ",
"ok": "Update",
"cancel": "Not now"
}
}
5 changes: 5 additions & 0 deletions assets/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@
"copy_button": "复制",
"copied": "已复制"
}
},
"update": {
"title": "新版本 ",
"ok": "更新",
"cancel": "暂不更新"
}
}
3 changes: 3 additions & 0 deletions lib/global_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import 'package:mixpanel_flutter/mixpanel_flutter.dart';
import 'package:path_provider/path_provider.dart';

class GlobalState {
static const version = "2.1.0";

static const platform = MethodChannel('cafe.f403.ascent/main');
static Rx<String> currentRoute = "/home".obs;
static late final Directory dataDir;
static const String localizationAssetPath = "assets/translations";
static Rx<bool> hasCert = false.obs;
static late Mixpanel mixpanel;
static StreamSubscription? intentSubscription;
static String? locale;

static const List<Locale> supportedLocale = [
Locale('en', 'US'),
Expand Down
50 changes: 48 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';

import 'package:ascent/ffi.dart';
Expand All @@ -9,9 +10,12 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:get/get.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:receive_intent/receive_intent.dart' as intent;
import 'package:uri_to_file/uri_to_file.dart';
import 'package:url_launcher/url_launcher.dart';
import 'components/bottom_navigation_bar/view.dart';
import 'package:http/http.dart' as http;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -31,6 +35,40 @@ void main() async {
);
}

Future<void> checkUpdate() async {
String url = GlobalState.locale == "zh_CN"
? "https://gist.gitmirror.com/4o3F/d44252ab04227a81b8270fe85a50691a/raw"
: "https://gist.github.com/4o3F/d44252ab04227a81b8270fe85a50691a/raw";
http.Response response = await http.get(Uri.parse(url));
Map parsed = json.decode(response.body);
String version = parsed['version'];
Version newVersion = Version.parse(version);
Version currentVersion = Version.parse(GlobalState.version);
if (newVersion > currentVersion) {
String updateInfo =
parsed['info'][GlobalState.locale] ?? parsed['info']['en_US'];
Uri url = Uri.parse(GlobalState.locale == "zh_CN" ? parsed['url']['backup'] : parsed['url']['main']);
BrnEnhanceOperationDialog dialog = BrnEnhanceOperationDialog(
context: Get.context!,
titleText: tr('update.title') + version,
descText: updateInfo,
mainButtonText: tr('update.ok'),
secondaryButtonText: tr('update.cancel'),
onMainButtonClick: () async {
if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
await launchUrl(url);
}
},
onSecondaryButtonClick: () {
Get.back(closeOverlays: true);
},
);
dialog.show();
}
}

Future<void> doUpdate(String url) async {}

class AscentApp extends StatelessWidget {
const AscentApp({super.key});

Expand Down Expand Up @@ -60,7 +98,10 @@ class AscentApp extends StatelessWidget {
));
}
}
} on PlatformException catch (_, e) {}
} on PlatformException catch (_, e) {
GlobalState.mixpanel
.track('Platform error', properties: {'error': e.toString()});
}

GlobalState.intentSubscription ??= intent.ReceiveIntent.receivedIntentStream
.listen((intent.Intent? receivedIntent) async {
Expand All @@ -87,12 +128,17 @@ class AscentApp extends StatelessWidget {
));
}
}
}, onError: (err) {});
}, onError: (err) {
GlobalState.mixpanel
.track('Platform error', properties: {'error': err.toString()});
});
}

@override
Widget build(BuildContext context) {
initReceiveIntent();
GlobalState.locale = context.deviceLocale.toString();
checkUpdate();
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ packages:
source: hosted
version: "2.3.1"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
Expand Down Expand Up @@ -639,7 +639,7 @@ packages:
source: hosted
version: "1.5.1"
pub_semver:
dependency: transitive
dependency: "direct main"
description:
name: pub_semver
sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c"
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ dependencies:
url_launcher: ^6.2.1
uri_to_file: ^1.0.0
receive_intent: ^0.2.4
http: ^1.1.0
pub_semver: ^2.1.4

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 52e31ba

Please sign in to comment.