diff --git a/core/src/cz/nic/tablexia/screen/camera/CameraScreen.java b/core/src/cz/nic/tablexia/screen/camera/CameraScreen.java index 686fc983fc1d3ff6450c17adfd5082ae1924352d..18808c1822818189740b79b22c27423cf4b8b5dc 100644 --- a/core/src/cz/nic/tablexia/screen/camera/CameraScreen.java +++ b/core/src/cz/nic/tablexia/screen/camera/CameraScreen.java @@ -14,28 +14,45 @@ import com.google.zxing.PlanarYUVLuminanceSource; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import cz.nic.tablexia.Tablexia; import cz.nic.tablexia.TablexiaApplication; import cz.nic.tablexia.TablexiaSettings; import cz.nic.tablexia.bus.ApplicationBus; +import cz.nic.tablexia.loader.application.ApplicationInternalTextureManager; import cz.nic.tablexia.loader.application.ApplicationTextManager; import cz.nic.tablexia.menu.user.UserMenu; import cz.nic.tablexia.screen.AbstractTablexiaScreen; +import cz.nic.tablexia.shared.model.User; +import cz.nic.tablexia.sync.RestSynchronizationService; +import cz.nic.tablexia.sync.work.DownloadUser; +import cz.nic.tablexia.sync.work.SyncWork; import cz.nic.tablexia.util.Log; import cz.nic.tablexia.util.camera.CameraCallback; import cz.nic.tablexia.util.camera.DeviceCameraControl; +import cz.nic.tablexia.util.ui.button.StandardTablexiaButton; import cz.nic.tablexia.util.ui.dialog.TablexiaComponentDialog; import cz.nic.tablexia.util.ui.dialog.TablexiaComponentDialogFactory; +import cz.nic.tablexia.util.ui.dialog.components.AdaptiveSizeDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.AlertOnShowDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.CenterPositionDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.DimmerDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.FixedSpaceContentDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.ResizableSpaceContentDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.TablexiaDialogComponentAdapter; +import cz.nic.tablexia.util.ui.dialog.components.TextContentDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.TwoButtonContentDialogComponent; +import cz.nic.tablexia.util.ui.dialog.components.ViewportMaximumSizeComponent; /** * Created by Vitaliy Vashchenko on 7.3.16. */ public class CameraScreen extends AbstractTablexiaScreen<Void> implements CameraCallback { - private static final int DIALOG_WIDTH = 450; - private static final int DIALOG_HEIGHT = 300; + private static final float SYNC_FAILED_DIALOG_BOTTOM_PADDING = 1 / 40f; private Mode mode; private static DeviceCameraControl deviceCameraControl; @@ -110,7 +127,6 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera resetState(); } shapeRenderer = new ShapeRenderer(); - } @Override @@ -118,12 +134,10 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera if (!decoded) { deviceCameraControl.stopPreviewAsync(); } - Log.debug(getClass(), "screenPaused"); } @Override protected void screenResumed() { - Log.debug(getClass(), "screenResumed"); if (!decoded) { deviceCameraControl.startPreviewAsync(); } @@ -131,14 +145,12 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera @Override public void resume() { - Log.debug(getClass(), "resume"); super.performScreenResumed(); } @Override protected void screenRender(float delta) { Mode prev = mode; - switch (mode) { case preview: if (deviceCameraControl.isPreviewReady()) { @@ -161,33 +173,106 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera if (prev != mode) { Log.debug(getClass(), "Mode : " + prev.name() + " --> " + mode.name()); } - } // TODO: 15.3.16 add proper dialog text private void showConfirmDialog() { - Log.debug(getClass(), "Showing dialog with " + decodedData); dialog = TablexiaComponentDialogFactory.getInstance().createYesNoDialog( ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_QR_REQUEST_ON_DECODED_DIALOG_TEXT) + decodedData, new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { - super.clicked(event, x, y); - Log.debug(getClass(), "Decoded: "+ decodedData); + Log.debug(getClass(), "Decoded: " + decodedData); + if (decodedData.length() > 0 && decodedData.toLowerCase().matches(UserMenu.CODE_REGEX)) { + DownloadUser syncWork = new DownloadUser(decodedData); + syncWork.registerListener(new SyncWork.RestSyncListener() { + @Override + public void onSuccess(User user) { + ApplicationBus.getInstance().post(new UserMenu.RefreshUserMenu()).asynchronously(); + } + + @Override + public void onFailure(Throwable t) { + showSyncFailedDialog(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_ERROR)); + } + + @Override + public void onWrongResponseCode(int responseCode) { + String failReason = ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_ERROR; + + if (responseCode == DownloadUser.USER_NOT_FOUND_STATUS_CODE) + failReason = ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_WRONG_ID; + + showSyncFailedDialog(ApplicationTextManager.getInstance().getText(failReason)); + } + + @Override + public void onCancelled() { + //no action + } + }); + RestSynchronizationService.doSyncWork(syncWork); + } backToMenu(); + super.clicked(event, x, y); } }, new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { - super.clicked(event, x, y); resetState(); + backToMenu(); + super.clicked(event, x, y); + } + }); + dialog.show(Tablexia.ERROR_DIALOG_WIDTH, Tablexia.ERROR_DIALOG_HEIGHT); + } + + private void showSyncFailedDialog(final String reason) { + Gdx.app.postRunnable(new Runnable() { + @Override + public void run() { + List<TablexiaDialogComponentAdapter> components = new ArrayList<TablexiaDialogComponentAdapter>() { + { + add(new CenterPositionDialogComponent()); + add(new DimmerDialogComponent()); + add(new ViewportMaximumSizeComponent()); + add(new AdaptiveSizeDialogComponent()); + + add(new ResizableSpaceContentDialogComponent()); + add(new TextContentDialogComponent(reason)); + + add(new ResizableSpaceContentDialogComponent()); + add(new TwoButtonContentDialogComponent( + ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYSTEM_DECLINE), + ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYSTEM_RETRY), + new Image(ApplicationInternalTextureManager.getInstance().getTexture(ApplicationInternalTextureManager.BUTTON_NO_ICON)), + new Image(ApplicationInternalTextureManager.getInstance().getTexture(ApplicationInternalTextureManager.BUTTON_YES_ICON)), + StandardTablexiaButton.TablexiaButtonType.RED, + StandardTablexiaButton.TablexiaButtonType.GREEN, + null, //Cancel Button + new ClickListener() { //Try again + @Override + public void clicked(InputEvent event, float x, float y) { + showConfirmDialog(); + } + } + )); + add(new FixedSpaceContentDialogComponent(SYNC_FAILED_DIALOG_BOTTOM_PADDING)); + + add(new AlertOnShowDialogComponent()); + add(new AdaptiveSizeDialogComponent()); + add(new CenterPositionDialogComponent()); } - }); - dialog.show(DIALOG_WIDTH, DIALOG_HEIGHT); + }; + + dialog = TablexiaComponentDialogFactory.getInstance().createDialog(components.toArray(new TablexiaDialogComponentAdapter[]{})); + dialog.show(Tablexia.ERROR_DIALOG_WIDTH, Tablexia.ERROR_DIALOG_HEIGHT, false); + } + }); } - private void resetState(){ + private void resetState() { hasData = false; decodedData = null; decoded = false; @@ -199,17 +284,15 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera if (!sizeIsSet) { this.width = width; this.height = height; - innerWidth = width/2; - innerHeight = height/2; - left = width/4; - top = height/4; + innerWidth = width / 2; + innerHeight = height / 2; + left = width / 4; + top = height / 4; sizeIsSet = true; } } private void backToMenu() { - Log.debug(getClass(), "backToMenu"); - ApplicationBus.getInstance().post(new UserMenu.RefreshUserMenu()).asynchronously(); ApplicationBus.getInstance().post(new Tablexia.ChangeScreenEvent(TablexiaSettings.LOADER_SCREEN, TablexiaApplication.ScreenTransaction.FADE)).asynchronously(); } @@ -217,12 +300,9 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera protected void screenVisible(Map<String, String> screenState) { super.screenVisible(screenState); } - - @Override protected void screenResized(int width, int height) { - Log.debug(getClass(), "screenResized"); if (deviceCameraControl == null) { deviceCameraControl = Tablexia.getDeviceCameraControl(); } @@ -238,18 +318,15 @@ public class CameraScreen extends AbstractTablexiaScreen<Void> implements Camera sizeIsSet = false; glass.setPosition(getSceneWidth() / 2 - glass.getWidth() / 2, getSceneInnerHeight() / 2 - glass.getHeight() / 2); } - @Override protected void screenDisposed() { - Log.debug(getClass(), "screenDisposed"); deviceCameraControl.stopPreviewAsync(); shapeRenderer.dispose(); } @Override public void backButtonPressed() { - Log.debug(getClass(), "backButtonPressed"); if (dialog != null) { dialog.hide(); }