[client] compare versions in updater

This commit is contained in:
2019-09-30 21:42:33 +03:00
parent 1e79fcfacb
commit cfa1475cf6
6 changed files with 71 additions and 12 deletions

View File

@@ -0,0 +1,86 @@
package ru.m.update;
import haxework.net.JsonLoader;
import openfl.Lib;
import openfl.net.URLRequest;
import promhx.Promise;
import ru.m.Device;
import ru.m.update.Version;
@:enum abstract PackageType(String) from String to String {
var APK = "apk";
var DEB = "deb";
var EXE = "exe";
var ARCHIVE = "archive";
}
typedef PackageInfo = {
var platform:Platform;
var type:PackageType;
var path:String;
var filename:String;
var url:String;
var version:String;
}
typedef PackagesBundle = {
var name:String;
var version:String;
var packages:Array<PackageInfo>;
}
class Updater {
private static inline var TAG = "Update";
public var type(get, null):PackageType;
public var bundle(default, null):PackagesBundle;
private var url:String;
private var version:Version;
public function new(version:String, url:String) {
this.url = url;
this.version = version;
}
private function get_type():PackageType {
return switch Device.platform {
case ANDROID: APK;
case LINUX: DEB;
case WINDOWS: EXE;
case _: null;
}
}
private function resolveBundle():Promise<PackagesBundle> {
return bundle != null ? Promise.promise(bundle) : new JsonLoader().GET(url).then(function(bundle) {
this.bundle = bundle;
return this.bundle;
});
}
public function check():Promise<Bool> {
return resolveBundle().then(function(bundle:PackagesBundle):Bool {
this.bundle = bundle;
return Lambda.exists(bundle.packages, function(item) return (
item.platform == Device.platform &&
item.type == type &&
version < Version.fromString(item.version)
));
});
}
public function download():Promise<Bool> {
return resolveBundle().then(function(bundle:PackagesBundle) {
for (item in bundle.packages) {
if (item.platform == Device.platform && item.type == type) {
L.i(TAG, 'download: ${item.url}');
Lib.getURL(new URLRequest(item.url));
return true;
}
}
return false;
});
}
}