From b6d7781666f4a7d771f4df86656c88d5bdeed97d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Drahom=C3=ADr=20Karch=C5=88=C3=A1k?=
 <drahomir.karchnak@nic.cz>
Date: Mon, 12 Sep 2016 10:40:40 +0200
Subject: [PATCH] #448 Shows dialog when user download fails or user enters
 wrong UUID.

---
 .../application/application_cs.properties     |  5 +-
 .../application/application_de.properties     |  5 +-
 .../application/application_sk.properties     |  5 +-
 .../cz/nic/tablexia/TablexiaApplication.java  |  2 +-
 .../application/ApplicationTextManager.java   |  4 +-
 .../cz/nic/tablexia/menu/user/UserMenu.java   | 62 ++++++++++++++++++-
 .../sync/work/ConfirmDataReceived.java        |  2 +-
 .../nic/tablexia/sync/work/DownloadUser.java  |  3 +-
 .../tablexia/sync/work/PushDataToServer.java  |  2 +-
 .../cz/nic/tablexia/sync/work/SyncWork.java   | 15 ++---
 10 files changed, 87 insertions(+), 18 deletions(-)

diff --git a/android/assets/text/application/application_cs.properties b/android/assets/text/application/application_cs.properties
index 5d93bbd0b..3dbac7a65 100644
--- a/android/assets/text/application/application_cs.properties
+++ b/android/assets/text/application/application_cs.properties
@@ -10,6 +10,7 @@ system_decline=Zrušit
 system_exit=Konec
 system_retry=Znovu
 system_back=Zpět
+system_understand=RozumĂ­m
 
 zipassetloader_error=Chyba: Nemohu stáhnout dodatečná data! Zkontrolujte prosím připojení k internetu.
 zipassetloader_download_request: Právě využíváte mobilní připojení k internetu. Tablexia potřebuje stáhnout ~150 MB herních dat. Přejete si je stáhnout nyní ?
@@ -39,7 +40,6 @@ usermenu_dialog_no = Ne
 user_logout_message=Opravdu chcete odhlásit stávajícího uživatele?
 
 preloader_title=Jak hrát?
-confirm_button=RozumĂ­m
 
 gamedifficulty_name=ObtĂ­Ĺľnost:
 gamedifficulty_tutorial=CviÄŤenĂ­
@@ -207,6 +207,9 @@ user_rank_11=VrchnĂ­ rada
 
 sync_request_dialog_text=Zadej ÄŤĂ­slo prĹŻkazu:
 sync_request_button=Synchronizovat uĹľivatele
+sync_request_wrong_id=Ĺ patnÄ› zadanĂ˝ kĂłd.
+sync_request_error=Synchronizace se nezdařila, zkuste to prosím později.
+
 game_runes_preloader1=Podívej se na symboly v\u00A0pásu vpravo.
 game_runes_preloader2_easy=Najdi je na domech a\u00A0označ dřív, než ti dojde čas.
 game_runes_preloader2_medium=Najdi na domech jejich zrcadlově otočené varianty a\u00A0označ je dřív, než ti dojde čas.
diff --git a/android/assets/text/application/application_de.properties b/android/assets/text/application/application_de.properties
index a4f8418be..e79c408fd 100644
--- a/android/assets/text/application/application_de.properties
+++ b/android/assets/text/application/application_de.properties
@@ -10,6 +10,7 @@ system_decline=Beenden
 system_exit=Verlassen
 system_retry=Wieder
 system_back=ZurĂĽck
+system_understand=Ich verstehe
 
 zipassetloader_error=Fehler: Nicht, um zusätzliche Daten Download! Bitte überprüfen Sie Ihre Internetverbindung.
 zipassetloader_download_request=Sie verwenden im Moment das mobile Internet. Tablexia lädt ~ 150 MB an Spieldaten herunter. Möchten Sie die Daten wirklich jetzt runterladen?
@@ -39,7 +40,6 @@ usermenu_dialog_yes=Ja
 usermenu_dialog_no=Nein
 user_logout_message=Möchtest du abmelden?
 preloader_title=Wie spielt man?
-confirm_button=Ich verstehe
 
 gamedifficulty_name=Schwierigkeitsgrad:
 gamedifficulty_tutorial=Lernprogramm
@@ -207,6 +207,9 @@ user_rank_11=Oberrat
 
 sync_request_dialog_text=Trage deine Ausweisnummer ein\:
 sync_request_button=Account synchronisieren
+sync_request_wrong_id=Falsch geschriebener Code.
+sync_request_error=Die synchronisierung ist fehlgeschlagen, bitte versuche es später noch einmal.
+
 game_runes_preloader1=**Podívej se na symboly v pásu vpravo.[DE]**
 game_runes_preloader2_easy=**Najdi je na domech a označ dřív, než ti dojde čas.[DE]**
 game_runes_preloader2_medium=**Najdi na domech jejich [BLACK]zrcadlově otočené varianty[] a označ je dřív, než ti dojde čas.[DE]**\
diff --git a/android/assets/text/application/application_sk.properties b/android/assets/text/application/application_sk.properties
index 8de784dc7..e21beab6b 100644
--- a/android/assets/text/application/application_sk.properties
+++ b/android/assets/text/application/application_sk.properties
@@ -10,6 +10,7 @@ system_decline=Zrušiť
 system_exit=Koniec
 system_retry=Znovu
 system_back=Späť
+system_understand=Rozumiem
 
 zipassetloader_error=Chyba: Nemôžem stiahnuť dodatočná dáta! Skontrolujte prosím pripojenie k internetu.
 zipassetloader_download_request: [SK upravit v application_sk.properties] Právě využíváte mobilní připojení k internetu. Tablexia potřebuje stáhnout ~150 MB herních dat. Přejete si je stáhnout nyní ?
@@ -41,7 +42,6 @@ usermenu_dialog_no=Nie
 user_logout_message=Naozaj chceš odhlásiť súčasného užívateľa?
 
 preloader_title=Ako hraĹĄ?
-confirm_button=Rozumiem
 
 gamedifficulty_name=ObtiaĹľnosĹĄ:
 gamedifficulty_tutorial=CviÄŤenie
@@ -210,6 +210,9 @@ user_rank_11=Ĺ efmajster
 
 sync_request_dialog_text=Zadaj ÄŤĂ­slo preukazu:
 sync_request_button=Synchronizovať užívateľa
+sync_request_wrong_id=Zle zadanĂ˝ kĂłd.
+sync_request_error=Synchronizácia sa nepodarila, skúste to prosím neskôr.
+
 game_runes_preloader1=**Podívej se na symboly v pásu vpravo.[SK]**
 game_runes_preloader2_easy=**Najdi je na domech a označ dřív, než ti dojde čas.[DE]**
 game_runes_preloader2_medium=**Najdi na domech jejich zrcadlově otočené varianty a označ je dřív, než ti dojde čas.[SK]**\
diff --git a/core/src/cz/nic/tablexia/TablexiaApplication.java b/core/src/cz/nic/tablexia/TablexiaApplication.java
index 080318baf..4f161ee9b 100644
--- a/core/src/cz/nic/tablexia/TablexiaApplication.java
+++ b/core/src/cz/nic/tablexia/TablexiaApplication.java
@@ -462,7 +462,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
         screen.preparePreloaderContent(PRELOADER_DIALOG_WIDTH, PRELOADER_DIALOG_HEIGHT, preloaderAssetsManager, screenComponents);
 
         // build dialog from generic and screen specific components
-        preloaderCloseButtonComponent = new CloseButtonContentDialogComponent(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.CONFIRM_BUTTON))
+        preloaderCloseButtonComponent = new CloseButtonContentDialogComponent(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYSTEM_UNDERSTAND))
                 .setEnabled(false)
                 .useOnce(true)
                 .setInputListener(new ClickListener() {
diff --git a/core/src/cz/nic/tablexia/loader/application/ApplicationTextManager.java b/core/src/cz/nic/tablexia/loader/application/ApplicationTextManager.java
index abd0e5e4f..cd13b5c46 100644
--- a/core/src/cz/nic/tablexia/loader/application/ApplicationTextManager.java
+++ b/core/src/cz/nic/tablexia/loader/application/ApplicationTextManager.java
@@ -30,7 +30,6 @@ public class ApplicationTextManager extends TablexiaDataManager<I18NBundle> impl
         public static final String USER_LOGOUT_MESSAGE              = "user_logout_message";
 
 		public static final String PRELOADER_TITLE					= "preloader_title";
-		public static final String CONFIRM_BUTTON  					= "confirm_button";
 
 		public static final String SYSTEM_CONFIRM					= "system_confirm";
 		public static final String SYSTEM_DECLINE					= "system_decline";
@@ -39,6 +38,7 @@ public class ApplicationTextManager extends TablexiaDataManager<I18NBundle> impl
 		public static final String SYSTEM_EXIT 						= "system_exit";
 		public static final String SYSTEM_RETRY						= "system_retry";
 		public static final String SYSTEM_BACK						= "system_back";
+		public static final String SYSTEM_UNDERSTAND				= "system_understand";
 
 		public static final String ZIPASSETLOADER_ERROR				= "zipassetloader_error";
 		public static final String ZIPASSETLOADER_DOWNLOAD_REQUEST	= "zipassetloader_download_request";
@@ -107,6 +107,8 @@ public class ApplicationTextManager extends TablexiaDataManager<I18NBundle> impl
 
 		public static final String SYNC_REQUEST_DIALOG_TEXT				= "sync_request_dialog_text";
 		public static final String SYNC_REQUEST_BUTTON					= "sync_request_button";
+		public static final String SYNC_REQUEST_WRONG_ID				= "sync_request_wrong_id";
+		public static final String SYNC_REQUEST_ERROR					= "sync_request_error";
 
 		public static final String USER_RANK_1							= "user_rank_1";
 		public static final String USER_RANK_2							= "user_rank_2";
diff --git a/core/src/cz/nic/tablexia/menu/user/UserMenu.java b/core/src/cz/nic/tablexia/menu/user/UserMenu.java
index 7a22d759e..81cdc79bc 100644
--- a/core/src/cz/nic/tablexia/menu/user/UserMenu.java
+++ b/core/src/cz/nic/tablexia/menu/user/UserMenu.java
@@ -31,14 +31,18 @@ import cz.nic.tablexia.sync.work.DownloadUser;
 import cz.nic.tablexia.sync.work.PushDataToServer;
 import cz.nic.tablexia.sync.work.SyncWork;
 import cz.nic.tablexia.util.ui.ClickListenerWithSound;
+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.BackButtonHideComponent;
 import cz.nic.tablexia.util.ui.dialog.components.CenterPositionDialogComponent;
+import cz.nic.tablexia.util.ui.dialog.components.CloseButtonContentDialogComponent;
 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.PositiveNegativeButtonContentDialogComponent;
 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.TextFiledDialogComponent;
 import cz.nic.tablexia.util.ui.dialog.components.ViewportMaximumSizeComponent;
@@ -61,6 +65,10 @@ public class UserMenu extends AbstractMenu {
     private static final int    SYNC_REQUEST_DIALOG_MAXIMUM_LENGTH   = 36;
     private static final float  SYNC_DIALOG_TITLE_PAD                = 0.15f;
     private static final float  SYNC_DIALOG_TEXT_FIELD_PAD           = 0.05f;
+
+    private static final int    SYNC_FAILED_DIALOG_WIDTH             = 280;
+    private static final int    SYNC_FAILED_DIALOG_HEIGHT            = 220;
+    private static final float  SYNC_FAILED_DIALOG_BOTTOM_PADDING    = 1/40f;
     
     public static final String  CODE_REGEX                           = "(([0-9]|[a-f]){8})-(([0-9]|[a-f]){4})-(([0-9]|[a-f]){4})-(([0-9]|[a-f]){4})-(([0-9]|[a-f]){12})$";
 
@@ -167,17 +175,34 @@ public class UserMenu extends AbstractMenu {
                         new PositiveNegativeButtonContentDialogComponent(new ClickListener() {
                                 @Override
                                 public void clicked(InputEvent event, float x, float y) {
-                                    super.clicked(event, x, y);
-                                    if (textFiledDialogComponent.getText().length()>0 && textFiledDialogComponent.getText().toLowerCase().matches(CODE_REGEX)) {
+                                    if (textFiledDialogComponent.getText().length() > 0 && textFiledDialogComponent.getText().toLowerCase().matches(CODE_REGEX)) {
                                         DownloadUser syncWork = new DownloadUser(textFiledDialogComponent.getText());
                                         syncWork.registerListener(new SyncWork.RestSyncListener() {
                                             @Override
                                             public void onSuccess(User user) {
                                                 ApplicationBus.getInstance().post(new RefreshUserMenu()).asynchronously();
                                             }
+
+                                            @Override
+                                            public void onFailure(Throwable t) {
+                                                showSyncFailedDialog(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_ERROR));
+                                            }
+
+                                            @Override
+                                            protected void onWrongResponseCode(int responseCode) {
+                                                if(responseCode == DownloadUser.USER_NOT_FOUND_STATUS_CODE)
+                                                    showSyncFailedDialog(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_WRONG_ID));
+                                                else {
+                                                    showSyncFailedDialog(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_ERROR));
+                                                }
+                                            }
                                         });
                                         RestSynchronizationService.doSyncWork(syncWork);
                                     }
+                                    else { //ID was blank or didn't match the ID regex
+                                        showSyncFailedDialog(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYNC_REQUEST_WRONG_ID));
+                                    }
+
                                     //Prevents keyboard getting stuck
                                     Gdx.input.setOnscreenKeyboardVisible(false);
                                 }
@@ -185,7 +210,6 @@ public class UserMenu extends AbstractMenu {
                             new ClickListener() {
                                 @Override
                                 public void clicked(InputEvent event, float x, float y) {
-                                    super.clicked(event, x, y);
                                     //Prevents keyboard getting stuck
                                     Gdx.input.setOnscreenKeyboardVisible(false);
                                 }
@@ -215,6 +239,38 @@ public class UserMenu extends AbstractMenu {
         layoutContainer.setActor(container);
     }
 
+    private void showSyncFailedDialog(final String reason) {
+        Gdx.app.postRunnable(new Runnable() {
+            @Override
+            public void run() {
+                TablexiaComponentDialog dialog;
+
+                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 CloseButtonContentDialogComponent(ApplicationTextManager.getInstance().getText(ApplicationTextManager.ApplicationTextsAssets.SYSTEM_UNDERSTAND)));
+                        add(new FixedSpaceContentDialogComponent(SYNC_FAILED_DIALOG_BOTTOM_PADDING));
+
+                        add(new AlertOnShowDialogComponent());
+                        add(new AdaptiveSizeDialogComponent());
+                        add(new CenterPositionDialogComponent());
+                    }
+                };
+
+                dialog = TablexiaComponentDialogFactory.getInstance().createDialog(components.toArray(new TablexiaDialogComponentAdapter[]{}));
+                dialog.show(SYNC_FAILED_DIALOG_WIDTH, SYNC_FAILED_DIALOG_HEIGHT, false);
+            }
+        });
+    }
+
     @Override
     public void dispose() {
         ApplicationBus.getInstance().unsubscribe(this);
diff --git a/core/src/cz/nic/tablexia/sync/work/ConfirmDataReceived.java b/core/src/cz/nic/tablexia/sync/work/ConfirmDataReceived.java
index 74443b908..034160357 100644
--- a/core/src/cz/nic/tablexia/sync/work/ConfirmDataReceived.java
+++ b/core/src/cz/nic/tablexia/sync/work/ConfirmDataReceived.java
@@ -31,7 +31,7 @@ public class ConfirmDataReceived extends SyncWork {
     }
 
     @Override
-    protected void onResponse(Net.HttpResponse httpResponse) {
+    protected void onSuccessfulResponse(Net.HttpResponse httpResponse) {
         GameDAO.markGamesAsSync(getUser().getGames());
         ScreenDAO.markScreensAsSync(getUser().getScreens());
         Log.debug(this.getClass(), String.format("Marking %d of games as synchronized", getUser().getGames() != null ? getUser().getGames().size() : 0));
diff --git a/core/src/cz/nic/tablexia/sync/work/DownloadUser.java b/core/src/cz/nic/tablexia/sync/work/DownloadUser.java
index 4b2155cea..9d6a29890 100644
--- a/core/src/cz/nic/tablexia/sync/work/DownloadUser.java
+++ b/core/src/cz/nic/tablexia/sync/work/DownloadUser.java
@@ -16,6 +16,7 @@ import cz.nic.tablexia.util.Log;
  */
 public class DownloadUser extends SyncWork {
 
+    public static final int USER_NOT_FOUND_STATUS_CODE = 404;
     private final String uuid;
 
     public DownloadUser(String uuid) {
@@ -34,7 +35,7 @@ public class DownloadUser extends SyncWork {
     }
 
     @Override
-    protected void onResponse(Net.HttpResponse httpResponse) {
+    protected void onSuccessfulResponse(Net.HttpResponse httpResponse) {
         String userJson = httpResponse.getResultAsString();
         Json json = new Json();
         json.setIgnoreUnknownFields(true);
diff --git a/core/src/cz/nic/tablexia/sync/work/PushDataToServer.java b/core/src/cz/nic/tablexia/sync/work/PushDataToServer.java
index 77eb89efe..3dba26421 100644
--- a/core/src/cz/nic/tablexia/sync/work/PushDataToServer.java
+++ b/core/src/cz/nic/tablexia/sync/work/PushDataToServer.java
@@ -61,7 +61,7 @@ public class PushDataToServer extends SyncWork {
     }
 
     @Override
-    protected void onResponse(Net.HttpResponse httpResponse) {
+    protected void onSuccessfulResponse(Net.HttpResponse httpResponse) {
 
         Log.debug(this.getClass(), "received uuid: " + httpResponse.getHeader("uuid"));
 
diff --git a/core/src/cz/nic/tablexia/sync/work/SyncWork.java b/core/src/cz/nic/tablexia/sync/work/SyncWork.java
index 88f6b42bd..3a22b2bc3 100644
--- a/core/src/cz/nic/tablexia/sync/work/SyncWork.java
+++ b/core/src/cz/nic/tablexia/sync/work/SyncWork.java
@@ -63,20 +63,19 @@ public abstract class SyncWork implements Net.HttpResponseListener {
         }
 
         if (httpResponse.getStatus().getStatusCode() != getExpectedSuccessResponseCode()) {
-            failed(new Exception(String.format("expected response code was %d but received %d", getExpectedSuccessResponseCode(), httpResponse.getStatus().getStatusCode())));
+            Log.debug(getClass(), "Wrong Response Code received! " + httpResponse.getStatus().getStatusCode() + " (expected: " + getExpectedSuccessResponseCode() + ")");
+            if(listener != null) listener.onWrongResponseCode(httpResponse.getStatus().getStatusCode());
             return;
         }
 
-        onResponse(httpResponse);
+        onSuccessfulResponse(httpResponse);
         if (listener != null) {
             listener.onSuccess(getUser());
         }
     }
 
     // override this method to handle success response from server
-    protected void onResponse(Net.HttpResponse httpResponse) {
-
-    }
+    protected void onSuccessfulResponse(Net.HttpResponse httpResponse) {}
 
     @Override
     public void failed(Throwable t) {
@@ -190,10 +189,12 @@ public abstract class SyncWork implements Net.HttpResponseListener {
 
         }
 
-        public void onCancelled() {
-
+        // on wrong response from the server
+        protected void onWrongResponseCode(int responseCode) {
         }
 
+        public void onCancelled() {}
+
         public abstract void onSuccess(User user);
     }
 }
-- 
GitLab