Files
puzzlez/src/app/haxe/ru/m/puzzlez/view/StartFrame.hx

103 lines
3.3 KiB
Haxe

package ru.m.puzzlez.view;
import hw.view.data.DataView;
import hw.view.form.ButtonView;
import hw.view.frame.FrameSwitcher;
import hw.view.frame.FrameView;
import ru.m.puzzlez.FileUtil;
import ru.m.puzzlez.image.ImageSourceBundle;
import ru.m.puzzlez.proto.game.GameStatus;
import ru.m.puzzlez.storage.FileStorage;
import ru.m.puzzlez.storage.GameStorage;
import ru.m.puzzlez.view.GameListFrame;
import ru.m.puzzlez.view.ImageListFrame;
import ru.m.update.Updater;
@:template class StartFrame extends FrameView<Dynamic> {
public static var ID = "start";
@:view var sources:DataView<ImageListConfig, ButtonView>;
@:view var startedButton:ButtonView;
@:view var completedButton:ButtonView;
@:view var updateButton:ButtonView;
@:provide var switcher:FrameSwitcher;
@:provide static var appUpdater:Updater;
@:provide var fileStorage:FileStorage;
@:provide var gameStorage:GameStorage;
@:provide var sourceBundle:ImageSourceBundle;
private var fileSource:ImageListConfig = {title: "Files", sourceId: "file"};
private var startedGames:GameListConfig = {
title: "Started",
source: gameStorage,
filter: ["status" => GameStatus.STARTED]
};
private var completedGames:GameListConfig = {
title: "Completed",
source: gameStorage,
filter: ["status" => GameStatus.COMPLETE]
};
public function new() {
super(ID);
var data:Array<ImageListConfig> = [];
sourceBundle.get(fileSource.sourceId).getPage({index: 0, count: 0}).then((page) -> {
if (page.total > 0) {
data.unshift(fileSource);
sources.data = data;
}
});
sources.data = data;
sourceBundle.get("nginx").getCategories().then((categories) -> {
for (category in categories) {
data.push({title: category, sourceId: "nginx", filter: ["category" => category]});
}
sources.data = data;
});
}
private function sourceViewFactory(index:Int, source:ImageListConfig):ButtonView {
var result = new ButtonView();
result.style = "button.source";
result.text = source.title;
return result;
}
public function upload():Void {
FileUtil.browse().pipe((data:FileContent) -> fileStorage.save(data.content)).then(_ -> showSource(fileSource));
}
public function showSource(config:ImageListConfig):Void {
switcher.change(ImageListFrame.ID, config);
}
public function showGames(config:GameListConfig):Void {
switcher.change(GameListFrame.ID, config);
}
override public function onShow(data:Dynamic):Void {
L.d("Frame", '$ID');
appUpdater.check().then((info:Null<PackageInfo>) -> {
if (info != null) {
updateButton.visible = true;
updateButton.text = 'Update ${info.version}';
}
}).catchError(error -> L.w('Update', 'failed: ${error}'));
refresh();
}
private function refresh():Void {
gameStorage.getIndexPage({index: 0, count: 0, filter: startedGames.filter}).then(response -> {
startedButton.text = response.total > 0 ? 'Started (${response.total})' : 'Started';
startedButton.disabled = response.total == 0;
});
gameStorage.getIndexPage({index: 0, count: 0, filter: completedGames.filter}).then(response -> {
completedButton.text = response.total > 0 ? 'Completed (${response.total})' : 'Completed';
completedButton.disabled = response.total == 0;
});
}
}