87 lines
2.1 KiB
Dart
87 lines
2.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
class UpdateInfo {
|
|
final bool update;
|
|
final String version;
|
|
final String notes;
|
|
final bool mandatory;
|
|
final String sha256;
|
|
final String download;
|
|
|
|
UpdateInfo({
|
|
required this.update,
|
|
required this.version,
|
|
required this.notes,
|
|
required this.mandatory,
|
|
required this.sha256,
|
|
required this.download,
|
|
});
|
|
|
|
factory UpdateInfo.fromJson(Map<String, dynamic> json) {
|
|
return UpdateInfo(
|
|
update: json["update"] ?? false,
|
|
version: json["version"] ?? "",
|
|
notes: json["notes"] ?? "",
|
|
mandatory: json["mandatory"] ?? false,
|
|
sha256: json["sha256"] ?? "",
|
|
download: json["download"] ?? "",
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
identical(this, other) ||
|
|
other is UpdateInfo &&
|
|
update == other.update &&
|
|
version == other.version &&
|
|
notes == other.notes &&
|
|
mandatory == other.mandatory &&
|
|
sha256 == other.sha256 &&
|
|
download == other.download;
|
|
|
|
@override
|
|
int get hashCode =>
|
|
Object.hash(update, version, notes, mandatory, sha256, download);
|
|
}
|
|
|
|
class AppUpdateUtil {
|
|
final String serverUrl;
|
|
|
|
AppUpdateUtil({required this.serverUrl});
|
|
|
|
/// Gets the installed app version
|
|
Future<String> currentVersion() async {
|
|
final info = await PackageInfo.fromPlatform();
|
|
debugPrint("Current app version: ${info.version}");
|
|
return info.version;
|
|
}
|
|
|
|
/// Checks the update server
|
|
Future<UpdateInfo?> checkForUpdate() async {
|
|
final version = await currentVersion();
|
|
|
|
final url = Uri.parse("$serverUrl/api/update?version=$version");
|
|
|
|
final response = await http.get(url);
|
|
|
|
debugPrint("Response from update server: ${response.body}");
|
|
|
|
if (response.statusCode != 200) {
|
|
throw Exception("Update server unavailable");
|
|
}
|
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
if (data["update"] != true) {
|
|
return null;
|
|
}
|
|
|
|
return UpdateInfo.fromJson(data);
|
|
}
|
|
}
|