From 7c97a17f435f47668d9cb7a42e9e05ff000d4693 Mon Sep 17 00:00:00 2001 From: shmyga Date: Thu, 10 Sep 2020 16:09:35 +0300 Subject: [PATCH] [refactoring] restore started and completed buttons --- src/app/haxe/ru/m/puzzlez/PuzzlezApp.hx | 2 + .../ru/m/puzzlez/source/GameImageSource.hx | 24 ++++++++++ .../haxe/ru/m/puzzlez/storage/GameStorage.hx | 48 +++++++++++++++++++ src/app/haxe/ru/m/puzzlez/view/StartFrame.hx | 17 +++++++ .../haxe/ru/m/puzzlez/view/StartFrame.yaml | 10 ++++ .../ru/m/puzzlez/view/common/ImageIdView.yaml | 8 ++++ 6 files changed, 109 insertions(+) create mode 100644 src/app/haxe/ru/m/puzzlez/source/GameImageSource.hx create mode 100644 src/app/haxe/ru/m/puzzlez/storage/GameStorage.hx create mode 100644 src/app/haxe/ru/m/puzzlez/view/common/ImageIdView.yaml diff --git a/src/app/haxe/ru/m/puzzlez/PuzzlezApp.hx b/src/app/haxe/ru/m/puzzlez/PuzzlezApp.hx index ceaf41f..baf987f 100644 --- a/src/app/haxe/ru/m/puzzlez/PuzzlezApp.hx +++ b/src/app/haxe/ru/m/puzzlez/PuzzlezApp.hx @@ -8,6 +8,7 @@ import ru.m.puzzlez.render.part.IPartBuilder; import ru.m.puzzlez.settings.Settings; import ru.m.puzzlez.source.AssetImageSource; import ru.m.puzzlez.source.FileImageSource; +import ru.m.puzzlez.source.GameImageSource; import ru.m.puzzlez.source.PixabayImageSource; import ru.m.puzzlez.storage.GameStorage; import ru.m.puzzlez.view.PuzzlezAppView; @@ -26,6 +27,7 @@ class PuzzlezApp { sourceBundle.register(new AssetImageSource()); sourceBundle.register(new FileImageSource()); sourceBundle.register(new PixabayImageSource()); + sourceBundle.register(new GameImageSource()); L.push(new TraceLogger()); updater = new Updater(Const.instance.VERSION, "https://shmyga.ru/repo/puzzlez/packages.json"); var app = new App(); diff --git a/src/app/haxe/ru/m/puzzlez/source/GameImageSource.hx b/src/app/haxe/ru/m/puzzlez/source/GameImageSource.hx new file mode 100644 index 0000000..81500a3 --- /dev/null +++ b/src/app/haxe/ru/m/puzzlez/source/GameImageSource.hx @@ -0,0 +1,24 @@ +package ru.m.puzzlez.source; + +import ru.m.puzzlez.proto.game.ImageId; +import promhx.Promise; +import ru.m.data.DataSource; +import ru.m.puzzlez.image.ImageSource; +import ru.m.puzzlez.storage.GameStorage; + +class GameImageSource implements ImageSource { + public var id(default, never):String = "game"; + + @:provide private var storage:GameStorage; + + public function new() { + } + + public function getPage(page:Page):Promise> { + return storage.getIndexPage(page); + } + + public function load(id:String):Promise { + throw "Unsupported"; + } +} diff --git a/src/app/haxe/ru/m/puzzlez/storage/GameStorage.hx b/src/app/haxe/ru/m/puzzlez/storage/GameStorage.hx new file mode 100644 index 0000000..8573753 --- /dev/null +++ b/src/app/haxe/ru/m/puzzlez/storage/GameStorage.hx @@ -0,0 +1,48 @@ +package ru.m.puzzlez.storage; + +import hw.connect.PacketUtil; +import haxe.io.Bytes; +import ru.m.puzzlez.proto.game.GameState; +import ru.m.puzzlez.proto.game.ImageId; +import ru.m.storage.SharedObjectDataStorage; + +class GameStorageHelper implements DataHelper { + public function new() { + + } + + public function idKey(id:ImageId):String { + return '${id.source}:${id.id}'; + } + + public function keyId(key:String):ImageId { + var keyArray = key.split(":"); + return new ImageId().setSource(keyArray[0]).setId(keyArray[1]); + } + + public function dataId(data:GameState):ImageId { + return data.preset.image; + } + + public function dataBytes(data:GameState):Bytes { + return PacketUtil.toBytes(data); + } + + public function bytesData(bytes:Bytes):GameState { + return PacketUtil.fromBytes(bytes, GameState); + } + + public function dataMeta(data:GameState):DataMeta { + return [ + "status" => data.status, + "date" => Date.now(), + ]; + } +} + +@:provide class GameStorage extends SharedObjectDataStorage { + + public function new() { + super("game", new GameStorageHelper()); + } +} diff --git a/src/app/haxe/ru/m/puzzlez/view/StartFrame.hx b/src/app/haxe/ru/m/puzzlez/view/StartFrame.hx index 00129f4..87fe8f9 100644 --- a/src/app/haxe/ru/m/puzzlez/view/StartFrame.hx +++ b/src/app/haxe/ru/m/puzzlez/view/StartFrame.hx @@ -6,7 +6,9 @@ import hw.view.frame.FrameSwitcher; import hw.view.frame.FrameView; import ru.m.pixabay.PixabayApi; import ru.m.puzzlez.FileUtil; +import ru.m.puzzlez.proto.game.GameStatus; import ru.m.puzzlez.storage.FileStorage; +import ru.m.puzzlez.storage.GameStorage; import ru.m.puzzlez.view.ImageSourceFrame; import ru.m.update.Updater; @@ -14,13 +16,18 @@ import ru.m.update.Updater; public static var ID = "start"; @:view var sources:DataView; + @: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; private var fileSource:ImageSourceConfig = {title: "Files", sourceId: "file"}; + private var startedSource:ImageSourceConfig = {title: "Started", sourceId: "game", filter: ["status" => GameStatus.STARTED]}; + private var completedSource:ImageSourceConfig = {title: "Completed", sourceId: "game", filter: ["status" => GameStatus.COMPLETE]}; public function new() { super(ID); @@ -56,5 +63,15 @@ import ru.m.update.Updater; updateButton.text = 'Update ${info.version}'; } }).catchError(error -> L.w('Update', 'failed: ${error}')); + refresh(); + } + + private function refresh():Void { + gameStorage.getIndexPage({index:0, count:0, filter:startedSource.filter}).then(response -> { + startedButton.text = response.total > 0 ? 'Started (${response.total})' : 'Started'; + }); + gameStorage.getIndexPage({index:0, count:0, filter:completedSource.filter}).then(response -> { + completedButton.text = response.total > 0 ? 'Completed (${response.total})' : 'Completed'; + }); } } diff --git a/src/app/haxe/ru/m/puzzlez/view/StartFrame.yaml b/src/app/haxe/ru/m/puzzlez/view/StartFrame.yaml index bd370cd..92744a3 100644 --- a/src/app/haxe/ru/m/puzzlez/view/StartFrame.yaml +++ b/src/app/haxe/ru/m/puzzlez/view/StartFrame.yaml @@ -29,9 +29,19 @@ views: overflow.y: scroll - $type: hw.view.group.HGroupView geometry.width: 100% + layout.margin: 10 views: + - id: startedButton + $type: hw.view.form.ButtonView + text: Started + +onPress: ~showSource(startedSource) + - id: completedButton + $type: hw.view.form.ButtonView + text: Completed + +onPress: ~showSource(completedSource) - $type: hw.view.form.ButtonView text: Upload + geometry.margin.left: 30 +onPress: ~upload() - $type: hw.view.form.LabelView text: $r:text:app.version diff --git a/src/app/haxe/ru/m/puzzlez/view/common/ImageIdView.yaml b/src/app/haxe/ru/m/puzzlez/view/common/ImageIdView.yaml new file mode 100644 index 0000000..5f08480 --- /dev/null +++ b/src/app/haxe/ru/m/puzzlez/view/common/ImageIdView.yaml @@ -0,0 +1,8 @@ +--- +style: view +views: + - id: image + $type: hw.view.ImageView + geometry.stretch: true + stretch: false + fillType: COVER