From df0d48f291c398facd22ffbe5033af6f4b7f877d Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 11 Apr 2019 18:00:44 +0400 Subject: [PATCH] Fixed crash when node is offline --- .../chronosx88/influence/helpers/actions/UIActions.java | 1 + .../io/github/chronosx88/influence/logic/ChatLogic.java | 8 +++++++- .../io/github/chronosx88/influence/logic/MainLogic.java | 3 +-- .../github/chronosx88/influence/logic/StartChatLogic.java | 5 +++++ .../chronosx88/influence/presenters/ChatPresenter.java | 8 ++++++++ .../influence/presenters/StartChatPresenter.java | 6 ++++++ 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/actions/UIActions.java b/app/src/main/java/io/github/chronosx88/influence/helpers/actions/UIActions.java index b791d6f..208f7c7 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/actions/UIActions.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/actions/UIActions.java @@ -11,4 +11,5 @@ public class UIActions { public static final int PEER_NOT_EXIST = 0x7; public static final int SUCCESSFUL_CREATE_CHAT = 0x8; public static final int MESSAGE_RECEIVED = 0x9; + public static final int NODE_IS_OFFLINE = 0x10; } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java index 42a607e..adc2303 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java @@ -46,12 +46,18 @@ public class ChatLogic implements IChatLogicContract { } }; this.timer = new Timer(); - timer.schedule(timerTask, 1, 1000); + if(AppHelper.getPeerDHT() != null) { + timer.schedule(timerTask, 1, 1000); + } this.keyPairManager = new KeyPairManager(); } @Override public void sendMessage(MessageEntity message) { + if(AppHelper.getPeerDHT() == null) { + ObservableUtils.notifyUI(UIActions.NODE_IS_OFFLINE); + return; + } new Thread(() -> { Data data = null; try { diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java index 3d3624a..eea2263 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java @@ -249,8 +249,7 @@ public class MainLogic implements IMainLogicContract { Data serializedUserProfile = null; try { serializedUserProfile = new Data(gson.toJson(userProfile)) - .protectEntry(mainKeyPair.getPrivate()) - .sign(keyPairManager.getKeyPair("mainSigningKeyPair")); + .protectEntry(mainKeyPair.getPrivate()); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java index 58a89b7..0474355 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java @@ -38,6 +38,11 @@ public class StartChatLogic implements IStartChatLogicContract { @Override public void sendStartChatMessage(String peerID) { + if(peerDHT == null) { + ObservableUtils.notifyUI(UIActions.NODE_IS_OFFLINE); + return; + } + new Thread(() -> { PublicUserProfile recipientPublicProfile = getPublicProfile(peerID); if(recipientPublicProfile == null) { diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.java index 9cd991c..4c049f8 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.java +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.java @@ -1,5 +1,7 @@ package io.github.chronosx88.influence.presenters; +import android.widget.Toast; + import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -54,6 +56,12 @@ public class ChatPresenter implements IChatPresenterContract, IObserver { } MessageEntity messageEntity = LocalDBWrapper.getMessageByID(jsonArray.get(1).getAsString()); view.updateMessageList(messageEntity); + break; + } + + case UIActions.NODE_IS_OFFLINE: { + Toast.makeText(AppHelper.getContext(), "Нода не запущена!", Toast.LENGTH_SHORT).show(); + break; } } } diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/StartChatPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/StartChatPresenter.java index 26f8c82..78c1b78 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/StartChatPresenter.java +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/StartChatPresenter.java @@ -40,6 +40,12 @@ public class StartChatPresenter implements IStartChatPresenterContract, IObserve view.showMessage("Чат успешно создан!"); break; } + + case UIActions.NODE_IS_OFFLINE: { + view.showProgressDialog(false); + view.showMessage("Нода не запущена!"); + break; + } } } }