add IDataSource
This commit is contained in:
20
src/haxe/ru/m/data/IDataSource.hx
Normal file
20
src/haxe/ru/m/data/IDataSource.hx
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package ru.m.data;
|
||||||
|
|
||||||
|
import promhx.Promise;
|
||||||
|
|
||||||
|
typedef Page = {
|
||||||
|
var index:Int;
|
||||||
|
var count:Int;
|
||||||
|
@:optional var order:String;
|
||||||
|
@:optional var filter:String;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef DataPage<T> = {
|
||||||
|
var page:Page;
|
||||||
|
var total:Int;
|
||||||
|
var data:Array<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IDataSource<T> {
|
||||||
|
public function getPage(page:Page):Promise<DataPage<T>>;
|
||||||
|
}
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package ru.m.puzzlez.storage;
|
package ru.m.puzzlez.storage;
|
||||||
|
|
||||||
|
import promhx.Promise;
|
||||||
import flash.net.SharedObject;
|
import flash.net.SharedObject;
|
||||||
import haxe.DynamicAccess;
|
import haxe.DynamicAccess;
|
||||||
import haxe.Serializer;
|
import haxe.Serializer;
|
||||||
import haxe.Unserializer;
|
import haxe.Unserializer;
|
||||||
|
import ru.m.data.IDataSource;
|
||||||
import ru.m.puzzlez.core.GameState;
|
import ru.m.puzzlez.core.GameState;
|
||||||
import ru.m.puzzlez.core.Id;
|
import ru.m.puzzlez.core.Id;
|
||||||
|
|
||||||
@:provide class GameStorage {
|
@:provide class GameStorage implements IDataSource<ImageId> {
|
||||||
|
|
||||||
private static var path = "game_1";
|
private static var path = "game_1";
|
||||||
private var statusData:SharedObject;
|
private var statusData:SharedObject;
|
||||||
@@ -16,6 +18,16 @@ import ru.m.puzzlez.core.Id;
|
|||||||
statusData = SharedObject.getLocal('${path}/status');
|
statusData = SharedObject.getLocal('${path}/status');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPage(page:Page):Promise<DataPage<ImageId>> {
|
||||||
|
var allData = list(page.filter);
|
||||||
|
allData.sort((a, b) -> Reflect.compare(a.toString().toLowerCase(), b.toString().toLowerCase()));
|
||||||
|
return Promise.promise({
|
||||||
|
page: page,
|
||||||
|
total: allData.length,
|
||||||
|
data: allData.slice(page.index * page.count, page.count),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public function save(state:GameState):Void {
|
public function save(state:GameState):Void {
|
||||||
statusData.setProperty(state.preset.imageId, Std.string(state.status));
|
statusData.setProperty(state.preset.imageId, Std.string(state.status));
|
||||||
statusData.flush();
|
statusData.flush();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import haxework.view.data.DataView;
|
|||||||
import haxework.view.frame.FrameSwitcher;
|
import haxework.view.frame.FrameSwitcher;
|
||||||
import haxework.view.frame.FrameView;
|
import haxework.view.frame.FrameView;
|
||||||
import haxework.view.popup.ConfirmView;
|
import haxework.view.popup.ConfirmView;
|
||||||
|
import ru.m.data.IDataSource;
|
||||||
import ru.m.puzzlez.core.GameState;
|
import ru.m.puzzlez.core.GameState;
|
||||||
import ru.m.puzzlez.core.Id.ImageId;
|
import ru.m.puzzlez.core.Id.ImageId;
|
||||||
import ru.m.puzzlez.storage.GameStorage;
|
import ru.m.puzzlez.storage.GameStorage;
|
||||||
@@ -19,14 +20,17 @@ import ru.m.puzzlez.view.PuzzleImageView;
|
|||||||
@:provide var storage:GameStorage;
|
@:provide var storage:GameStorage;
|
||||||
|
|
||||||
private var status:GameStatus;
|
private var status:GameStatus;
|
||||||
|
private var page:Page;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super(ID);
|
super(ID);
|
||||||
|
page = {index: 0, count: 10};
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function onShow(data:GameStatus):Void {
|
override public function onShow(data:GameStatus):Void {
|
||||||
status = data;
|
status = data;
|
||||||
images.data = storage.list(status);
|
page.filter = status;
|
||||||
|
storage.getPage(page).then(page -> images.data = page.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function start(id:ImageId):Void {
|
private function start(id:ImageId):Void {
|
||||||
@@ -39,7 +43,7 @@ import ru.m.puzzlez.view.PuzzleImageView;
|
|||||||
ConfirmView.confirm("Delete state?").then(result -> {
|
ConfirmView.confirm("Delete state?").then(result -> {
|
||||||
if (result) {
|
if (result) {
|
||||||
storage.delete(imageId);
|
storage.delete(imageId);
|
||||||
images.data = storage.list(status);
|
storage.getPage(page).then(page -> images.data = page.data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
case _:
|
case _:
|
||||||
|
|||||||
Reference in New Issue
Block a user