Added stop for tracking new messages when chat activity closed

This commit is contained in:
ChronosX88 2019-04-10 19:18:53 +04:00
parent 6670715418
commit a0d50a3d1c
5 changed files with 21 additions and 1 deletions

View File

@ -4,4 +4,5 @@ import io.github.chronosx88.influence.models.roomEntities.MessageEntity;
public interface IChatLogicContract { public interface IChatLogicContract {
void sendMessage(MessageEntity message); void sendMessage(MessageEntity message);
void stopTrackingForNewMsgs();
} }

View File

@ -3,4 +3,5 @@ package io.github.chronosx88.influence.contracts.chatactivity;
public interface IChatPresenterContract { public interface IChatPresenterContract {
void sendMessage(String text); void sendMessage(String text);
void updateAdapter(); void updateAdapter();
void onDestroy();
} }

View File

@ -34,6 +34,7 @@ public class ChatLogic implements IChatLogicContract {
private ChatEntity chatEntity; private ChatEntity chatEntity;
private Thread checkNewMessagesThread = null; private Thread checkNewMessagesThread = null;
private KeyPairManager keyPairManager; private KeyPairManager keyPairManager;
private Timer timer;
public ChatLogic(ChatEntity chatEntity) { public ChatLogic(ChatEntity chatEntity) {
this.chatEntity = chatEntity; this.chatEntity = chatEntity;
@ -44,7 +45,7 @@ public class ChatLogic implements IChatLogicContract {
checkForNewMessages(); checkForNewMessages();
} }
}; };
Timer timer = new Timer(); this.timer = new Timer();
timer.schedule(timerTask, 1, 1000); timer.schedule(timerTask, 1, 1000);
this.keyPairManager = new KeyPairManager(); this.keyPairManager = new KeyPairManager();
} }
@ -167,4 +168,10 @@ public class ChatLogic implements IChatLogicContract {
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
return jsonObject.get("action").getAsInt(); return jsonObject.get("action").getAsInt();
} }
@Override
public void stopTrackingForNewMsgs() {
timer.cancel();
timer.purge();
}
} }

View File

@ -58,4 +58,9 @@ public class ChatPresenter implements IChatPresenterContract, IObserver {
List<MessageEntity> entities = LocalDBWrapper.getMessagesByChatID(chatID); List<MessageEntity> entities = LocalDBWrapper.getMessagesByChatID(chatID);
view.updateMessageList(entities == null ? new ArrayList<>() : entities); view.updateMessageList(entities == null ? new ArrayList<>() : entities);
} }
@Override
public void onDestroy() {
logic.stopTrackingForNewMsgs();
}
} }

View File

@ -86,4 +86,10 @@ public class ChatActivity extends AppCompatActivity implements IChatViewContract
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
}
} }