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(); + } }