From c96231e2845e70735d39b10f9e3bd2a5c4ccae9b Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 26 Mar 2019 15:06:36 +0400 Subject: [PATCH] [WIP] Made a wrapper for the local database. --- .../influence/helpers/LocalDBWrapper.java | 43 +++++++++++++++++++ .../influence/helpers/NetworkHandler.java | 19 +++----- .../influence/logic/StartChatLogic.java | 3 +- .../models/roomEntities/MessageEntity.java | 19 +++++--- gradlew | 0 gradlew.bat | 0 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java mode change 100755 => 100644 gradlew mode change 100644 => 100755 gradlew.bat diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java new file mode 100644 index 0000000..e04b0a0 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/LocalDBWrapper.java @@ -0,0 +1,43 @@ +package io.github.chronosx88.influence.helpers; + +import android.util.Log; + +import net.tomp2p.peers.PeerAddress; + +import java.util.List; + +import io.github.chronosx88.influence.models.roomEntities.ChatEntity; +import io.github.chronosx88.influence.models.roomEntities.MessageEntity; + +public class LocalDBWrapper { + private static final String LOG_TAG = "LocalDBWrapper"; + private static RoomHelper dbInstance = AppHelper.getChatDB(); + + /** + * Create a chat entry in the local database. + * @param chatID Chat ID + * @param name Chat name + * @param peerAddress Companion's address + * @return Successful chat creation (true / false) + */ + public static boolean createChatEntry(String chatID, String name, PeerAddress peerAddress) { + List chatEntities = AppHelper.getChatDB().chatDao().getChatByChatID(chatID); + if (chatEntities.size() > 0) { + Log.e(LOG_TAG, "Failed to create chat " + chatID + " because chat exists!"); + return false; + } + dbInstance.chatDao().addChat(new ChatEntity(chatID, name, "", Serializer.serialize(peerAddress))); + return true; + } + + public static boolean createMessageEntry(int type, String chatID, String sender, String text) { + List chatEntities = AppHelper.getChatDB().chatDao().getChatByChatID(chatID); + if(chatEntities.size() < 1) { + Log.e(LOG_TAG, "Failed to create message entry because chat " + chatID + " doesn't exists!"); + return false; + } + // TODO: Make message timestamp + dbInstance.messageDao().insertMessage(new MessageEntity(type, chatID, sender, "", text)); + return true; + } +} diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java index ca6001b..9dc4c3b 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java @@ -42,7 +42,7 @@ public class NetworkHandler implements NetworkObserver { switch (getMessageAction((String) object)) { case NetworkActions.CREATE_CHAT: { NewChatRequestMessage newChatRequestMessage = gson.fromJson((String) object, NewChatRequestMessage.class); - createChatEntry(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress()); + LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress()); handleIncomingChatRequest(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderPeerAddress()); ObservableUtils.notifyUI(UIActions.NEW_CHAT); break; @@ -50,7 +50,7 @@ public class NetworkHandler implements NetworkObserver { case NetworkActions.SUCCESSFULL_CREATE_CHAT: { NewChatRequestMessage newChatRequestMessage = gson.fromJson((String) object, NewChatRequestMessage.class); - createChatEntry(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress()); + LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress()); ObservableUtils.notifyUI(UIActions.SUCCESSFUL_CREATE_CHAT); break; } @@ -63,14 +63,7 @@ public class NetworkHandler implements NetworkObserver { return jsonObject.get("action").getAsInt(); } - public static void createChatEntry(String chatID, String name, PeerAddress peerAddress) { - List chatEntities = AppHelper.getChatDB().chatDao().getChatByChatID(chatID); - if (chatEntities.size() > 0) { - Log.e(LOG_TAG, "Failed to create chat " + chatID + " because chat exists!"); - return; - } - AppHelper.getChatDB().chatDao().addChat(new ChatEntity(chatID, name, "", Serializer.serialize(peerAddress))); - } + private void handleIncomingChatRequest(String chatID, PeerAddress chatStarterAddress) { NewChatRequestMessage newChatRequestMessage = new NewChatRequestMessage(chatID, AppHelper.getPeerID(), peerDHT.peerAddress()); @@ -95,7 +88,7 @@ public class NetworkHandler implements NetworkObserver { e.printStackTrace(); } - createChatEntry( + LocalDBWrapper.createChatEntry( newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress() @@ -153,13 +146,13 @@ public class NetworkHandler implements NetworkObserver { e.printStackTrace(); } - createChatEntry( + LocalDBWrapper.createChatEntry( newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderID(), newChatRequestMessage.getSenderPeerAddress() ); - FutureRemove remove = peerDHT.remove(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats")) + peerDHT.remove(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats")) .contentKey(Number160.createHash(newChatRequestMessage.getChatID())) .start() .awaitUninterruptibly(); 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 5ed80f3..1c8b5db 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 @@ -19,6 +19,7 @@ import java.util.UUID; import io.github.chronosx88.influence.contracts.startchat.StartChatLogicContract; import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.KeyPairManager; +import io.github.chronosx88.influence.helpers.LocalDBWrapper; import io.github.chronosx88.influence.helpers.NetworkHandler; import io.github.chronosx88.influence.helpers.ObservableUtils; import io.github.chronosx88.influence.helpers.P2PUtils; @@ -67,7 +68,7 @@ public class StartChatLogic implements StartChatLogicContract { e.printStackTrace(); } } - NetworkHandler.createChatEntry(newChatRequestMessage.getChatID(), peerID, recipientPeerAddress); + LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), peerID, recipientPeerAddress); ObservableUtils.notifyUI(UIActions.NEW_CHAT); }).start(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageEntity.java b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageEntity.java index 29c5cdd..6874255 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageEntity.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageEntity.java @@ -7,17 +7,18 @@ import androidx.room.PrimaryKey; @Entity(tableName = "messages") public class MessageEntity { - @NonNull @PrimaryKey String id; + @PrimaryKey(autoGenerate = true) String id; + @ColumnInfo public int type; @ColumnInfo public String chatID; @ColumnInfo public String sender; - @ColumnInfo public String date; + @ColumnInfo public String timestamp; @ColumnInfo public String text; - public MessageEntity(String id, String chatID, String sender, String date, String text) { - this.id = id; + public MessageEntity(int type, String chatID, String sender, String timestamp, String text) { + this.type = type; this.chatID = chatID; this.sender = sender; - this.date = date; + this.timestamp = timestamp; this.text = text; } @@ -25,10 +26,16 @@ public class MessageEntity { return id; } + public int getType() { return type; } + public String getChatID() { return chatID; } + public String getTimestamp() { + return timestamp; + } + public String getSender() { return sender; } @@ -40,6 +47,6 @@ public class MessageEntity { @NonNull @Override public String toString() { - return id + "/" + chatID + "/" + sender + "/" + text; + return id + "/" + chatID + "/" + type + "/" + sender + "/" + text; } } diff --git a/gradlew b/gradlew old mode 100755 new mode 100644 diff --git a/gradlew.bat b/gradlew.bat old mode 100644 new mode 100755