mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-21 23:02:18 +00:00
[WIP] Made a wrapper for the local database.
This commit is contained in:
parent
33e3aadcc4
commit
c96231e284
@ -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<ChatEntity> 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<ChatEntity> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -42,7 +42,7 @@ public class NetworkHandler implements NetworkObserver {
|
|||||||
switch (getMessageAction((String) object)) {
|
switch (getMessageAction((String) object)) {
|
||||||
case NetworkActions.CREATE_CHAT: {
|
case NetworkActions.CREATE_CHAT: {
|
||||||
NewChatRequestMessage newChatRequestMessage = gson.fromJson((String) object, NewChatRequestMessage.class);
|
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());
|
handleIncomingChatRequest(newChatRequestMessage.getChatID(), newChatRequestMessage.getSenderPeerAddress());
|
||||||
ObservableUtils.notifyUI(UIActions.NEW_CHAT);
|
ObservableUtils.notifyUI(UIActions.NEW_CHAT);
|
||||||
break;
|
break;
|
||||||
@ -50,7 +50,7 @@ public class NetworkHandler implements NetworkObserver {
|
|||||||
|
|
||||||
case NetworkActions.SUCCESSFULL_CREATE_CHAT: {
|
case NetworkActions.SUCCESSFULL_CREATE_CHAT: {
|
||||||
NewChatRequestMessage newChatRequestMessage = gson.fromJson((String) object, NewChatRequestMessage.class);
|
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);
|
ObservableUtils.notifyUI(UIActions.SUCCESSFUL_CREATE_CHAT);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -63,14 +63,7 @@ public class NetworkHandler implements NetworkObserver {
|
|||||||
return jsonObject.get("action").getAsInt();
|
return jsonObject.get("action").getAsInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createChatEntry(String chatID, String name, PeerAddress peerAddress) {
|
|
||||||
List<ChatEntity> 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) {
|
private void handleIncomingChatRequest(String chatID, PeerAddress chatStarterAddress) {
|
||||||
NewChatRequestMessage newChatRequestMessage = new NewChatRequestMessage(chatID, AppHelper.getPeerID(), peerDHT.peerAddress());
|
NewChatRequestMessage newChatRequestMessage = new NewChatRequestMessage(chatID, AppHelper.getPeerID(), peerDHT.peerAddress());
|
||||||
@ -95,7 +88,7 @@ public class NetworkHandler implements NetworkObserver {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
createChatEntry(
|
LocalDBWrapper.createChatEntry(
|
||||||
newChatRequestMessage.getChatID(),
|
newChatRequestMessage.getChatID(),
|
||||||
newChatRequestMessage.getSenderID(),
|
newChatRequestMessage.getSenderID(),
|
||||||
newChatRequestMessage.getSenderPeerAddress()
|
newChatRequestMessage.getSenderPeerAddress()
|
||||||
@ -153,13 +146,13 @@ public class NetworkHandler implements NetworkObserver {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
createChatEntry(
|
LocalDBWrapper.createChatEntry(
|
||||||
newChatRequestMessage.getChatID(),
|
newChatRequestMessage.getChatID(),
|
||||||
newChatRequestMessage.getSenderID(),
|
newChatRequestMessage.getSenderID(),
|
||||||
newChatRequestMessage.getSenderPeerAddress()
|
newChatRequestMessage.getSenderPeerAddress()
|
||||||
);
|
);
|
||||||
|
|
||||||
FutureRemove remove = peerDHT.remove(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats"))
|
peerDHT.remove(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats"))
|
||||||
.contentKey(Number160.createHash(newChatRequestMessage.getChatID()))
|
.contentKey(Number160.createHash(newChatRequestMessage.getChatID()))
|
||||||
.start()
|
.start()
|
||||||
.awaitUninterruptibly();
|
.awaitUninterruptibly();
|
||||||
|
@ -19,6 +19,7 @@ import java.util.UUID;
|
|||||||
import io.github.chronosx88.influence.contracts.startchat.StartChatLogicContract;
|
import io.github.chronosx88.influence.contracts.startchat.StartChatLogicContract;
|
||||||
import io.github.chronosx88.influence.helpers.AppHelper;
|
import io.github.chronosx88.influence.helpers.AppHelper;
|
||||||
import io.github.chronosx88.influence.helpers.KeyPairManager;
|
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.NetworkHandler;
|
||||||
import io.github.chronosx88.influence.helpers.ObservableUtils;
|
import io.github.chronosx88.influence.helpers.ObservableUtils;
|
||||||
import io.github.chronosx88.influence.helpers.P2PUtils;
|
import io.github.chronosx88.influence.helpers.P2PUtils;
|
||||||
@ -67,7 +68,7 @@ public class StartChatLogic implements StartChatLogicContract {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NetworkHandler.createChatEntry(newChatRequestMessage.getChatID(), peerID, recipientPeerAddress);
|
LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), peerID, recipientPeerAddress);
|
||||||
ObservableUtils.notifyUI(UIActions.NEW_CHAT);
|
ObservableUtils.notifyUI(UIActions.NEW_CHAT);
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
@ -7,17 +7,18 @@ import androidx.room.PrimaryKey;
|
|||||||
|
|
||||||
@Entity(tableName = "messages")
|
@Entity(tableName = "messages")
|
||||||
public class MessageEntity {
|
public class MessageEntity {
|
||||||
@NonNull @PrimaryKey String id;
|
@PrimaryKey(autoGenerate = true) String id;
|
||||||
|
@ColumnInfo public int type;
|
||||||
@ColumnInfo public String chatID;
|
@ColumnInfo public String chatID;
|
||||||
@ColumnInfo public String sender;
|
@ColumnInfo public String sender;
|
||||||
@ColumnInfo public String date;
|
@ColumnInfo public String timestamp;
|
||||||
@ColumnInfo public String text;
|
@ColumnInfo public String text;
|
||||||
|
|
||||||
public MessageEntity(String id, String chatID, String sender, String date, String text) {
|
public MessageEntity(int type, String chatID, String sender, String timestamp, String text) {
|
||||||
this.id = id;
|
this.type = type;
|
||||||
this.chatID = chatID;
|
this.chatID = chatID;
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.date = date;
|
this.timestamp = timestamp;
|
||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,10 +26,16 @@ public class MessageEntity {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getType() { return type; }
|
||||||
|
|
||||||
public String getChatID() {
|
public String getChatID() {
|
||||||
return chatID;
|
return chatID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSender() {
|
public String getSender() {
|
||||||
return sender;
|
return sender;
|
||||||
}
|
}
|
||||||
@ -40,6 +47,6 @@ public class MessageEntity {
|
|||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return id + "/" + chatID + "/" + sender + "/" + text;
|
return id + "/" + chatID + "/" + type + "/" + sender + "/" + text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
0
gradlew.bat
vendored
Normal file → Executable file
0
gradlew.bat
vendored
Normal file → Executable file
Loading…
Reference in New Issue
Block a user