[add] ModernFileReference
This commit is contained in:
38
src/haxe/ru/m/ModernFileReference.hx
Normal file
38
src/haxe/ru/m/ModernFileReference.hx
Normal file
@@ -0,0 +1,38 @@
|
||||
package ru.m;
|
||||
|
||||
import flash.net.FileFilter;
|
||||
import flash.net.FileReference;
|
||||
|
||||
class Callback<T> {
|
||||
private var callback:T -> Void;
|
||||
|
||||
public function new(callback: T -> Void) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public function execute(result:T):Void {
|
||||
this.callback(result);
|
||||
}
|
||||
}
|
||||
|
||||
class ModernFileReference extends FileReference {
|
||||
#if android
|
||||
private static var fileUtilBrowse = lime.system.JNI.createStaticMethod(
|
||||
"ru.m.android.FileUtil",
|
||||
"browse",
|
||||
"(Lorg/haxe/lime/HaxeObject;)V"
|
||||
);
|
||||
#end
|
||||
|
||||
override function browse(?typeFilter:Array<FileFilter>):Bool {
|
||||
#if android
|
||||
fileUtilBrowse(new Callback<haxe.io.BytesData>(function(result:haxe.io.BytesData):Void {
|
||||
data = result;
|
||||
dispatchEvent(new flash.events.Event(flash.events.Event.COMPLETE));
|
||||
}));
|
||||
return true;
|
||||
#else
|
||||
return super.browse(typeFilter);
|
||||
#end
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,7 @@ package ru.m.puzzlez;
|
||||
import flash.events.Event;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.net.FileReference;
|
||||
import haxe.io.Bytes;
|
||||
import haxe.io.BytesData;
|
||||
import promhx.Deferred;
|
||||
import promhx.Promise;
|
||||
|
||||
@@ -14,32 +12,13 @@ typedef FileContent = {
|
||||
var content:Bytes;
|
||||
}
|
||||
|
||||
class Callback<T> {
|
||||
private var callback:T -> Void;
|
||||
|
||||
public function new(callback: T -> Void) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public function execute(result:T):Void {
|
||||
this.callback(result);
|
||||
}
|
||||
}
|
||||
|
||||
class FileUtil {
|
||||
#if android
|
||||
private static var fileUtilBrowse = lime.system.JNI.createStaticMethod(
|
||||
"ru.m.android.FileUtil",
|
||||
"browse",
|
||||
"(Lorg/haxe/lime/HaxeObject;)V"
|
||||
);
|
||||
#end
|
||||
|
||||
public static function browse():Promise<FileContent> {
|
||||
var d = new Deferred<FileContent>();
|
||||
var file = new FileReference();
|
||||
var file = new ModernFileReference();
|
||||
file.addEventListener(Event.SELECT, function(event:Event) {
|
||||
cast(event.target, FileReference).load();
|
||||
cast(event.target, ModernFileReference).load();
|
||||
});
|
||||
file.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent) {
|
||||
d.throwError(event);
|
||||
@@ -48,26 +27,18 @@ class FileUtil {
|
||||
//trace('progress', '${event}');
|
||||
});
|
||||
file.addEventListener(Event.COMPLETE, function(event:Event) {
|
||||
var f:FileReference = cast event.target;
|
||||
var f:ModernFileReference = cast event.target;
|
||||
d.resolve({
|
||||
name: f.name,
|
||||
content: Bytes.ofData(f.data),
|
||||
});
|
||||
});
|
||||
file.browse();
|
||||
#if android
|
||||
fileUtilBrowse(new Callback<BytesData>(function(result:BytesData):Void {
|
||||
d.resolve({
|
||||
name: "",
|
||||
content: Bytes.ofData(result),
|
||||
});
|
||||
}));
|
||||
#end
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
public static function save(content:FileContent):Void {
|
||||
var file = new FileReference();
|
||||
var file = new ModernFileReference();
|
||||
file.save(content.content.getData(), content.name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user