From 27939c261f467045cba7e1e26f22a137abd8f640 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 18 May 2019 17:06:35 +0400 Subject: [PATCH] Added new message notification when somebody send message --- app/build.gradle | 1 - .../chronosx88/influence/logic/ChatLogic.java | 37 ++++++++++--------- .../notifications/NewMessageNotification.java | 23 ++++++++++++ 3 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/io/github/chronosx88/influence/models/notifications/NewMessageNotification.java diff --git a/app/build.gradle b/app/build.gradle index 68be1b8..37dbbb9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -62,7 +62,6 @@ dependencies { implementation 'jdom:jdom:1.1' implementation 'commons-beanutils:commons-beanutils:1.8.3' implementation 'org.apache.tomcat:servlet-api:6.0.35' - //implementation group: 'org.eclipse.jetty.orbit', name: 'javax.servlet.jsp', version: '2.2.0.v201112011158' } repositories { mavenCentral() 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 57e8d0f..d07c417 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 @@ -9,7 +9,6 @@ import net.tomp2p.storage.Data; import java.io.IOException; import java.util.Map; -import java.util.Timer; import java.util.TimerTask; import java.util.UUID; @@ -24,30 +23,33 @@ import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.models.JoinChatMessage; import io.github.chronosx88.influence.models.NextChunkReference; import io.github.chronosx88.influence.models.TextMessage; +import io.github.chronosx88.influence.models.notifications.NewMessageNotification; import io.github.chronosx88.influence.models.roomEntities.ChatEntity; import io.github.chronosx88.influence.models.roomEntities.MessageEntity; public class ChatLogic implements CoreContracts.IChatLogicContract { private static Gson gson = new Gson(); private String chatID; - private volatile String newMessage = ""; + //private volatile String newMessage = ""; private ChatEntity chatEntity; - private Thread checkNewMessagesThread = null; + //private Thread checkNewMessagesThread = null; private KeyPairManager keyPairManager; - private Timer timer; public ChatLogic(ChatEntity chatEntity) { this.chatEntity = chatEntity; this.chatID = chatEntity.chatID; - TimerTask timerTask = new TimerTask() { + /*TimerTask timerTask = new TimerTask() { @Override public void run() { checkForNewMessages(); } - }; - this.timer = new Timer(); - if(AppHelper.getPeerDHT() != null) { - timer.schedule(timerTask, 1, 1000); + };*/ + if(AppHelper.getPeerDHT() != null && AppHelper.getNotificationSystem() != null) { + AppHelper.getNotificationSystem().subscribe(chatID, notification -> { + if(notification instanceof NewMessageNotification) { + handleNewMessages(chatEntity.chunkCursor); + } + }); } this.keyPairManager = new KeyPairManager(); } @@ -67,15 +69,16 @@ public class ChatLogic implements CoreContracts.IChatLogicContract { } data.protectEntry(keyPairManager.getKeyPair("mainKeyPair")); P2PUtils.put(chatID + "_messages" + chatEntity.chunkCursor, message.messageID, data); - try { + /*try { P2PUtils.put(chatID + "_newMessage", null, new Data(message.messageID)); } catch (IOException e) { e.printStackTrace(); - } + }*/ + AppHelper.getNotificationSystem().publish(chatID, new NewMessageNotification()); }).start(); } - private void checkForNewMessages() { + /*private void checkForNewMessages() { if(checkNewMessagesThread == null) { checkNewMessagesThread = new Thread(() -> { Map data = P2PUtils.get(chatID + "_newMessage"); @@ -120,7 +123,7 @@ public class ChatLogic implements CoreContracts.IChatLogicContract { }); checkNewMessagesThread.start(); } - } + }*/ private void handleNewMessages(int chunkID) { new Thread(() -> { @@ -160,8 +163,9 @@ public class ChatLogic implements CoreContracts.IChatLogicContract { String messageID = UUID.randomUUID().toString(); try { P2PUtils.put(chatEntity.chatID + "_messages" + chunkID, messageID, new Data(gson.toJson(new NextChunkReference(messageID, AppHelper.getPeerID(), AppHelper.getPeerID(), System.currentTimeMillis(), chatEntity.chunkCursor+1)))); - P2PUtils.put(chatEntity.chatID + "_newMessage", null, new Data(messageID)); - LocalDBWrapper.updateChatEntity(chatEntity); + //P2PUtils.put(chatEntity.chatID + "_newMessage", null, new Data(messageID)); + //LocalDBWrapper.updateChatEntity(chatEntity); + AppHelper.getNotificationSystem().publish(chatID, new NewMessageNotification()); } catch (IOException e) { e.printStackTrace(); } @@ -177,7 +181,6 @@ public class ChatLogic implements CoreContracts.IChatLogicContract { @Override public void stopTrackingForNewMsgs() { - timer.cancel(); - timer.purge(); + AppHelper.getNotificationSystem().unsubscribe(chatID); } } diff --git a/app/src/main/java/io/github/chronosx88/influence/models/notifications/NewMessageNotification.java b/app/src/main/java/io/github/chronosx88/influence/models/notifications/NewMessageNotification.java new file mode 100644 index 0000000..5d2887e --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/notifications/NewMessageNotification.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2019 ChronosX88 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package io.github.chronosx88.influence.models.notifications; + +import rice.p2p.scribe.ScribeContent; + +public class NewMessageNotification implements ScribeContent { +}