diff --git a/src/haxe/ru/m/puzzlez/view/GameFrame.hx b/src/haxe/ru/m/puzzlez/view/GameFrame.hx index 75df905..f18ac5f 100644 --- a/src/haxe/ru/m/puzzlez/view/GameFrame.hx +++ b/src/haxe/ru/m/puzzlez/view/GameFrame.hx @@ -89,17 +89,10 @@ import ru.m.puzzlez.view.popup.PreviewPopup; } private function back():Void { - (if (game != null && game.state.status == COMPLETE) { - Promise.promise(true); - } else { - ConfirmView.confirm("Exit?"); - }).then(result -> { + (game.state.status == COMPLETE ? Promise.promise(true) : ConfirmView.confirm("Exit?")) + .then(result -> { if (result) { - if (game.state.status == COMPLETE) { - switcher.change(GameListFrame.ID, COMPLETE); - } else { - switcher.back(); - } + switcher.change(GameListFrame.ID, game.state.status); } }); } diff --git a/src/haxe/ru/m/puzzlez/view/GameListFrame.hx b/src/haxe/ru/m/puzzlez/view/GameListFrame.hx index ca4b3a5..da89816 100644 --- a/src/haxe/ru/m/puzzlez/view/GameListFrame.hx +++ b/src/haxe/ru/m/puzzlez/view/GameListFrame.hx @@ -1,83 +1,29 @@ package ru.m.puzzlez.view; -import haxework.view.data.DataView; -import haxework.view.form.ToggleButtonView; import haxework.view.frame.FrameSwitcher; import haxework.view.frame.FrameView; -import haxework.view.popup.ConfirmView; -import ru.m.data.IDataSource; import ru.m.puzzlez.core.GameState; -import ru.m.puzzlez.core.Id.ImageId; import ru.m.puzzlez.storage.GameStorage; -import ru.m.puzzlez.view.common.PaginatorView; -import ru.m.puzzlez.view.PuzzleImageView; +import ru.m.puzzlez.view.common.ImageDataList; @:template class GameListFrame extends FrameView { public static var ID(default, never) = "game_list"; - @:view("images") var imagesView:ActionDataView; - @:view("pages") var pagesView:PaginatorView; - private var loading:LoadingWrapper; + @:view("images") var imagesView:ImageDataList; @:provide var switcher:FrameSwitcher; @:provide var storage:GameStorage; - private var status:GameStatus; - private var page:Page; - - private var data(default, set):DataPage; - - private function set_data(value:DataPage):DataPage { - data = value; - imagesView.data = data.data; - pagesView.page = value; - return data; - } - public function new() { super(ID); - loading = new LoadingWrapper(imagesView); - page = {index: 0, count: 6, order: [{key: "date", reverse: true}]}; - } - - private function pageFactory(index:Int, value:Int):ToggleButtonView { - var result = new ToggleButtonView(); - result.text = '${value}'; - result.on = data.page.index == value; - return result; + imagesView.source = storage; } override public function onShow(data:GameStatus):Void { - page.index = 0; - this.data = { - page: page, - total: 0, - data: [], - }; - status = data; - page.filter = ["status" => status]; - refresh(); - } - - private function start(id:ImageId):Void { - storage.get(id).then(state -> switcher.change(GameFrame.ID, state)); - } - - private function refresh():Void { - loading.promise = storage.getIndexPage(page).then(data -> this.data = data); - } - - private function onAction(imageId:ImageId, action:Action):Void { - switch action { - case CLEAN: - ConfirmView.confirm("Delete state?").then(result -> { - if (result) { - storage.delete(imageId).then(_ -> refresh()); - } - }); - case _: - } + imagesView.reset(); + imagesView.page.filter = ["status" => data]; + imagesView.refresh(); } private function back():Void { diff --git a/src/haxe/ru/m/puzzlez/view/GameListFrame.yaml b/src/haxe/ru/m/puzzlez/view/GameListFrame.yaml index 7c9f7d2..48390ae 100644 --- a/src/haxe/ru/m/puzzlez/view/GameListFrame.yaml +++ b/src/haxe/ru/m/puzzlez/view/GameListFrame.yaml @@ -2,26 +2,8 @@ style: frame views: - id: images - $type: haxework.view.data.ActionDataView - layout: - $type: haxework.view.layout.TailLayout - rowSize: 3 - margin: 5 - vAlign: middle + $type: ru.m.puzzlez.view.common.ImageDataList geometry.stretch: true - factory: ~ru.m.puzzlez.view.PuzzleImageView.factory - +onDataSelect: ~start - +onDataAction: ~onAction - geometry.margin: 5 - overflow.y: scroll - - id: pages - $type: ru.m.puzzlez.view.common.PaginatorView - geometry.width: 100% - +onPageSelect: | - ~(index) -> { - page.index = index; - refresh(); - } - $type: haxework.view.form.ButtonView text: Back geometry.position: absolute diff --git a/src/haxe/ru/m/puzzlez/view/ImageListFrame.hx b/src/haxe/ru/m/puzzlez/view/ImageListFrame.hx index ce537f9..939646e 100644 --- a/src/haxe/ru/m/puzzlez/view/ImageListFrame.hx +++ b/src/haxe/ru/m/puzzlez/view/ImageListFrame.hx @@ -1,110 +1,40 @@ package ru.m.puzzlez.view; -import haxework.view.data.DataView; import haxework.view.form.ButtonView; -import haxework.view.form.ToggleButtonView; import haxework.view.frame.FrameSwitcher; import haxework.view.frame.FrameView; -import haxework.view.popup.ConfirmView; -import ru.m.data.IDataSource; -import ru.m.puzzlez.core.Id; import ru.m.puzzlez.FileUtil; import ru.m.puzzlez.source.FileSource; -import ru.m.puzzlez.storage.GameStorage; import ru.m.puzzlez.storage.ImageStorage; -import ru.m.puzzlez.view.common.PaginatorView; -import ru.m.puzzlez.view.PuzzleImageView; +import ru.m.puzzlez.view.common.ImageDataList; @:template class ImageListFrame extends FrameView { public static var ID = "image_list"; - @:view("images") var imagesView:ActionDataView; - @:view("pages") var pagesView:PaginatorView; + @:view("images") var imagesView:ImageDataList; @:view var select:ButtonView; @:provide var imageStorage:ImageStorage; - @:provide var gameStorage:GameStorage; @:provide var switcher:FrameSwitcher; - private var source:ImageListSource; - private var loading:LoadingWrapper; - - private var page:Page; - private var data(default, set):DataPage; - - private function set_data(value:DataPage):DataPage { - data = value; - imagesView.data = data.data; - pagesView.page = data; - return data; - } - public function new() { super(ID); - page = {index: 0, count: 6, order: [{key: "date", reverse: true}]}; - loading = new LoadingWrapper(imagesView); - } - - private function pageFactory(index:Int, value:Int):ToggleButtonView { - var result = new ToggleButtonView(); - result.text = '${value}'; - result.on = data.page.index == value; - return result; } override public function onShow(data:ImageListSource):Void { - page.index = 0; - this.data = { - page: page, - total: 0, - data: [], - }; + imagesView.reset(); if (data != null) { - source = data; - select.visible = source.source.id == FileSource.ID; - page.filter = ["type" => data.type]; - refresh(); + select.visible = data.source.id == FileSource.ID; + imagesView.page.filter = ["type" => data.type]; + imagesView.source = data.source; + imagesView.refresh(); } } private function selectFile():Void { FileUtil.browse().then((data:FileContent) -> { - var fileSource:FileSource = cast source.source; - fileSource.append(data.content).then(_ -> refresh()); - }); - } - - private function onAction(imageId:ImageId, action:Action):Void { - switch action { - case REMOVE: - var fileSource:FileSource = Std.instance(source.source, FileSource); - if (fileSource != null) { - ConfirmView.confirm("Delete image?").then(result -> { - if (result) { - fileSource.remove(imageId).then(_ -> refresh()); - } - }); - } - case CLEAN: - ConfirmView.confirm("Delete state?").then(result -> { - if (result) { - gameStorage.delete(imageId).then(_ -> refresh()); - } - }); - } - } - - private function refresh():Void { - loading.promise = source.source.getIndexPage(page).then(data -> this.data = data); - } - - private function start(imageId:ImageId):Void { - gameStorage.get(imageId).then(state -> { - if (state != null) { - switcher.change(GameFrame.ID, state); - } else { - switcher.change(PresetFrame.ID, imageId); - } + var fileSource:FileSource = cast imageStorage.sources.get(FileSource.ID); + fileSource.append(data.content).then(_ -> imagesView.refresh()); }); } diff --git a/src/haxe/ru/m/puzzlez/view/ImageListFrame.yaml b/src/haxe/ru/m/puzzlez/view/ImageListFrame.yaml index 8cecca4..a61f9a2 100644 --- a/src/haxe/ru/m/puzzlez/view/ImageListFrame.yaml +++ b/src/haxe/ru/m/puzzlez/view/ImageListFrame.yaml @@ -2,26 +2,8 @@ style: frame views: - id: images - $type: haxework.view.data.ActionDataView - layout: - $type: haxework.view.layout.TailLayout - rowSize: 3 - margin: 5 - vAlign: middle + $type: ru.m.puzzlez.view.common.ImageDataList geometry.stretch: true - factory: ~ru.m.puzzlez.view.PuzzleImageView.factory - +onDataSelect: ~start - +onDataAction: ~onAction - geometry.margin: 5 - overflow.y: scroll - - id: pages - $type: ru.m.puzzlez.view.common.PaginatorView - geometry.width: 100% - +onPageSelect: | - ~(index) -> { - page.index = index; - refresh(); - } - id: select $type: haxework.view.form.ButtonView text: Select... diff --git a/src/haxe/ru/m/puzzlez/view/common/ImageDataList.hx b/src/haxe/ru/m/puzzlez/view/common/ImageDataList.hx new file mode 100644 index 0000000..31b2be2 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/view/common/ImageDataList.hx @@ -0,0 +1,92 @@ +package ru.m.puzzlez.view.common; + +import haxework.view.data.DataView; +import haxework.view.form.ToggleButtonView; +import haxework.view.frame.FrameSwitcher; +import haxework.view.group.VGroupView; +import haxework.view.popup.ConfirmView; +import ru.m.data.IDataSource; +import ru.m.puzzlez.core.Id; +import ru.m.puzzlez.source.FileSource; +import ru.m.puzzlez.storage.GameStorage; +import ru.m.puzzlez.storage.ImageStorage; +import ru.m.puzzlez.view.common.PuzzleImageView; + +@:template class ImageDataList extends VGroupView { + + public var source:IDataIndex; + public var page:Page; + + @:view("images") var imagesView:ActionDataView; + @:view("paginator") var paginatorView:PaginatorView; + private var loading:LoadingWrapper; + + @:provide var switcher:FrameSwitcher; + @:provide var gameStorage:GameStorage; + @:provide var imageStorage:ImageStorage; + + public var data(default, set):DataPage; + + private function set_data(value:DataPage):DataPage { + data = value; + imagesView.data = data.data; + paginatorView.page = data; + return data; + } + + public function new() { + super(); + loading = new LoadingWrapper(imagesView); + page = {index: 0, count: 6, order: [{key: "date", reverse: true}]}; + } + + private function pageFactory(index:Int, value:Int):ToggleButtonView { + var result = new ToggleButtonView(); + result.text = '${value}'; + result.on = data.page.index == value; + return result; + } + + private function start(imageId:ImageId):Void { + gameStorage.get(imageId).then(state -> { + if (state != null) { + switcher.change(GameFrame.ID, state); + } else { + switcher.change(PresetFrame.ID, imageId); + } + }); + } + + private function onAction(imageId:ImageId, action:Action):Void { + switch action { + case REMOVE: + var fileSource:FileSource = cast imageStorage.sources.get(FileSource.ID); + if (fileSource != null) { + ConfirmView.confirm("Delete image?").then(result -> { + if (result) { + fileSource.remove(imageId).then(_ -> refresh()); + } + }); + } + case CLEAN: + ConfirmView.confirm("Delete state?").then(result -> { + if (result) { + gameStorage.delete(imageId).then(_ -> refresh()); + } + }); + } + } + + public function refresh():Void { + loading.promise = source.getIndexPage(page).then(data -> this.data = data); + } + + public function reset():Void { + page.index = 0; + data = { + page: page, + total: 0, + data: [], + }; + } +} diff --git a/src/haxe/ru/m/puzzlez/view/common/ImageDataList.yaml b/src/haxe/ru/m/puzzlez/view/common/ImageDataList.yaml new file mode 100644 index 0000000..97c0a15 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/view/common/ImageDataList.yaml @@ -0,0 +1,23 @@ +--- +views: + - id: images + $type: haxework.view.data.ActionDataView + layout: + $type: haxework.view.layout.TailLayout + rowSize: 3 + margin: 5 + vAlign: middle + geometry.stretch: true + factory: ~ru.m.puzzlez.view.common.PuzzleImageView.factory + +onDataSelect: ~start + +onDataAction: ~onAction + geometry.margin: 5 + overflow.y: scroll + - id: paginator + $type: ru.m.puzzlez.view.common.PaginatorView + geometry.width: 100% + +onPageSelect: | + ~(index) -> { + page.index = index; + refresh(); + } diff --git a/src/haxe/ru/m/puzzlez/view/PuzzleImageView.hx b/src/haxe/ru/m/puzzlez/view/common/PuzzleImageView.hx similarity index 98% rename from src/haxe/ru/m/puzzlez/view/PuzzleImageView.hx rename to src/haxe/ru/m/puzzlez/view/common/PuzzleImageView.hx index 4ed491a..241a57a 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzleImageView.hx +++ b/src/haxe/ru/m/puzzlez/view/common/PuzzleImageView.hx @@ -1,4 +1,4 @@ -package ru.m.puzzlez.view; +package ru.m.puzzlez.view.common; import haxework.view.data.DataView; import haxework.view.form.ButtonView; diff --git a/src/haxe/ru/m/puzzlez/view/PuzzleImageView.yaml b/src/haxe/ru/m/puzzlez/view/common/PuzzleImageView.yaml similarity index 100% rename from src/haxe/ru/m/puzzlez/view/PuzzleImageView.yaml rename to src/haxe/ru/m/puzzlez/view/common/PuzzleImageView.yaml