-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
251 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import '../utils/intent_utils.dart'; | ||
|
||
class AppUpdateDialog extends StatelessWidget { | ||
final String content; | ||
final String apkUrl; | ||
final String htmlUrl; | ||
final String version; | ||
|
||
const AppUpdateDialog( | ||
{super.key, | ||
required this.content, | ||
required this.apkUrl, | ||
required this.version, | ||
required this.htmlUrl}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return AlertDialog( | ||
title: const Text("发现新版本"), | ||
content: Column(mainAxisSize: MainAxisSize.min, children: [ | ||
Text("v$version"), | ||
Text(content), | ||
]), | ||
actions: <Widget>[ | ||
TextButton( | ||
child: const Text('取消'), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
}, | ||
), | ||
TextButton( | ||
child: const Text('发布页面'), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
IntentUtils.getUrlIntent(htmlUrl).launchChooser("发布页面"); | ||
}, | ||
), | ||
TextButton( | ||
child: const Text('下载APK'), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
IntentUtils.getUrlIntent(apkUrl).launchChooser("下载APK"); | ||
|
||
}, | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,85 @@ | ||
import 'dart:js_interop_unsafe'; | ||
import 'dart:convert'; | ||
import 'dart:core'; | ||
import 'dart:developer'; | ||
import 'dart:io'; | ||
|
||
import 'package:get/get_connect/http/src/response/response.dart'; | ||
import 'package:alist_flutter/generated_api.dart'; | ||
|
||
class UpdateChecker { | ||
static Future<bool> checkForUpdate() async { | ||
final version = Android().getVersion | ||
final PackageInfo info = await PackageInfo.fromPlatform(); | ||
final String currentVersion = info.version; | ||
final String url = | ||
'https://raw.githubusercontent.com/iamSahdeep/liquid_swipe_flutter/master/pubspec.yaml'; | ||
try { | ||
final http.Response response = await http.get(url); | ||
if (response.statusCode == 200) { | ||
final String body = response.body; | ||
final List<String> list = body.split('\n'); | ||
final String latestVersion = list | ||
.firstWhere((String element) => element.contains('version')) | ||
.split(': ')[1]; | ||
if (currentVersion.compareTo(latestVersion) < 0) { | ||
return true; | ||
} | ||
String owner; | ||
String repo; | ||
|
||
Map<String, dynamic>? _data; | ||
|
||
UpdateChecker({required this.owner, required this.repo}); | ||
|
||
String _versionName = ""; | ||
String _systemABI = ""; | ||
|
||
downloadData() async { | ||
_data = await _getLatestRelease(owner, repo); | ||
_versionName = await Android().getVersionName(); | ||
_systemABI = await Android().getDeviceCPUABI(); | ||
} | ||
|
||
Map<String, dynamic> get data { | ||
if (_data == null) { | ||
throw Exception('Data not downloaded'); | ||
} | ||
return _data!; | ||
} | ||
|
||
static Future<Map<String, dynamic>> _getLatestRelease( | ||
String owner, String repo) async { | ||
HttpClient client = HttpClient(); | ||
final req = await client.getUrl( | ||
Uri.parse('https://api.github.com/repos/$owner/$repo/releases/latest')); | ||
final response = await req.close(); | ||
|
||
if (response.statusCode == HttpStatus.ok) { | ||
final body = await response.transform(utf8.decoder).join(); | ||
return json.decode(body); | ||
} else { | ||
throw Exception( | ||
'Failed to get latest release, status code: ${response.statusCode}'); | ||
} | ||
} | ||
|
||
String getTag() { | ||
return data['tag_name']; | ||
} | ||
|
||
Future<bool> hasNewVersion() async { | ||
final latestVersion = getTag(); | ||
final currentVersion = _versionName; | ||
|
||
log('latestVersion: $latestVersion, currentVersion: $currentVersion'); | ||
// return true; | ||
return _extractNumbers(latestVersion) > _extractNumbers(currentVersion); | ||
} | ||
|
||
String getApkDownloadUrl() { | ||
final assets = data['assets']; | ||
for (var asset in assets) { | ||
if (asset['name'].contains(_systemABI)) { | ||
return asset['browser_download_url']; | ||
} | ||
} catch (e) { | ||
print(e); | ||
} | ||
return false; | ||
|
||
throw Exception('Failed to get apk download url'); | ||
} | ||
|
||
String getUpdateContent() { | ||
return data['body']; | ||
} | ||
|
||
String getHtmlUrl() { | ||
return data['html_url']; | ||
} | ||
|
||
// 1.24.011609 to Int | ||
static int _extractNumbers(String input) { | ||
final s = input.replaceAll(RegExp(r'[^0-9]'), ''); | ||
return int.parse(s); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:android_intent_plus/android_intent.dart'; | ||
|
||
class IntentUtils { | ||
static AndroidIntent getUrlIntent(String url) { | ||
return AndroidIntent(action: "action_view", data: url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters