package ru.m.update; import hw.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; } 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 { return bundle != null ? Promise.promise(bundle) : new JsonLoader().GET(url).then(function(bundle) { this.bundle = bundle; return this.bundle; }); } public function check():Promise { 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 { 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; }); } }