From a0d50a3d1c4b73ab82bc804f8d9711b5e7250349 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 10 Apr 2019 19:18:53 +0400 Subject: [PATCH] Added stop for tracking new messages when chat activity closed --- .../contracts/chatactivity/IChatLogicContract.java | 1 + .../contracts/chatactivity/IChatPresenterContract.java | 1 + .../io/github/chronosx88/influence/logic/ChatLogic.java | 9 ++++++++- .../chronosx88/influence/presenters/ChatPresenter.java | 5 +++++ .../github/chronosx88/influence/views/ChatActivity.java | 6 ++++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatLogicContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatLogicContract.java index 5048b96..e5878fb 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatLogicContract.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatLogicContract.java @@ -4,4 +4,5 @@ import io.github.chronosx88.influence.models.roomEntities.MessageEntity; public interface IChatLogicContract { void sendMessage(MessageEntity message); + void stopTrackingForNewMsgs(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatPresenterContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatPresenterContract.java index a4487d6..3e77721 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatPresenterContract.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/chatactivity/IChatPresenterContract.java @@ -3,4 +3,5 @@ package io.github.chronosx88.influence.contracts.chatactivity; public interface IChatPresenterContract { void sendMessage(String text); void updateAdapter(); + void onDestroy(); } 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 6304ca0..4ccb7d3 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 @@ -34,6 +34,7 @@ public class ChatLogic implements IChatLogicContract { private ChatEntity chatEntity; private Thread checkNewMessagesThread = null; private KeyPairManager keyPairManager; + private Timer timer; public ChatLogic(ChatEntity chatEntity) { this.chatEntity = chatEntity; @@ -44,7 +45,7 @@ public class ChatLogic implements IChatLogicContract { checkForNewMessages(); } }; - Timer timer = new Timer(); + this.timer = new Timer(); timer.schedule(timerTask, 1, 1000); this.keyPairManager = new KeyPairManager(); } @@ -167,4 +168,10 @@ public class ChatLogic implements IChatLogicContract { JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); return jsonObject.get("action").getAsInt(); } + + @Override + public void stopTrackingForNewMsgs() { + timer.cancel(); + timer.purge(); + } } 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 1728ea4..3d00d6a 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 @@ -58,4 +58,9 @@ public class ChatPresenter implements IChatPresenterContract, IObserver { List entities = LocalDBWrapper.getMessagesByChatID(chatID); view.updateMessageList(entities == null ? new ArrayList<>() : entities); } + + @Override + public void onDestroy() { + logic.stopTrackingForNewMsgs(); + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.java index 68e579f..cb0cb7b 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/ChatActivity.java @@ -86,4 +86,10 @@ public class ChatActivity extends AppCompatActivity implements IChatViewContract } return super.onOptionsItemSelected(item); } + + @Override + protected void onDestroy() { + super.onDestroy(); + presenter.onDestroy(); + } }