From 72812f4e03e7c6cc2345454ad98d1e2572deed90 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 18 Mar 2019 17:17:09 +0400 Subject: [PATCH] Added basic implementation of ChatDB (Room-based) --- .../influence/helpers/AppHelper.java | 10 ++++++- .../influence/helpers/RoomHelper.java | 14 ++++++++++ .../influence/models/daos/ChatDao.java | 23 ++++++++++++++++ .../influence/models/daos/MessageDao.java | 26 +++++++++++++++++++ .../models/roomEntities/ChatModel.java | 11 ++++++++ .../models/roomEntities/MessageModel.java | 12 +++++++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/github/chronosx88/influence/helpers/RoomHelper.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/models/roomEntities/ChatModel.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageModel.java diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java index f198d0d..c85f033 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java @@ -5,10 +5,11 @@ import android.content.Context; import net.tomp2p.dht.PeerDHT; +import androidx.room.Room; import io.github.chronosx88.influence.observable.MainObservable; /** - * Extended Application class which designed for getting Context from anywhere in the application. + * Extended Application class which designed for getting various objects from anywhere in the application. */ public class AppHelper extends Application { @@ -16,12 +17,17 @@ public class AppHelper extends Application { private static MainObservable observable; private static String peerID; private static PeerDHT peerDHT; + private static RoomHelper chatDB; @Override public void onCreate() { super.onCreate(); instance = this; observable = new MainObservable(); + chatDB = Room.databaseBuilder(getApplicationContext(), RoomHelper.class, "chatDB") + .allowMainThreadQueries() + .build(); + } public static void storePeerID(String peerID1) { peerID = peerID1; } @@ -37,4 +43,6 @@ public class AppHelper extends Application { public static String getPeerID() { return peerID; } public static PeerDHT getPeerDHT() { return peerDHT; } + + public static RoomHelper getChatDB() { return chatDB; } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/RoomHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/RoomHelper.java new file mode 100644 index 0000000..161457c --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/RoomHelper.java @@ -0,0 +1,14 @@ +package io.github.chronosx88.influence.helpers; + +import androidx.room.Database; +import androidx.room.RoomDatabase; +import io.github.chronosx88.influence.models.roomEntities.ChatModel; +import io.github.chronosx88.influence.models.roomEntities.MessageModel; +import io.github.chronosx88.influence.models.daos.ChatDao; +import io.github.chronosx88.influence.models.daos.MessageDao; + +@Database(entities = { MessageModel.class, ChatModel.class }, version = 1) +public abstract class RoomHelper extends RoomDatabase { + public abstract ChatDao chatDao(); + public abstract MessageDao messageDao(); +} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java new file mode 100644 index 0000000..dcd92a2 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java @@ -0,0 +1,23 @@ +package io.github.chronosx88.influence.models.daos; + +import java.util.List; + +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.Query; +import io.github.chronosx88.influence.models.roomEntities.ChatModel; + +@Dao +public interface ChatDao { + @Insert + void addChat(ChatModel chatModel); + + @Query("DELETE FROM chats WHERE id = :chatID") + void deleteChat(String chatID); + + @Query("SELECT * FROM chats") + List getAllChats(); + + @Query("SELECT * FROM chats WHERE id = :chatID") + List getChatByID(String chatID); +} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java b/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java new file mode 100644 index 0000000..4cabd67 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/daos/MessageDao.java @@ -0,0 +1,26 @@ +package io.github.chronosx88.influence.models.daos; + +import java.util.List; + +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.Query; +import io.github.chronosx88.influence.models.roomEntities.MessageModel; + +@Dao +public interface MessageDao { + @Insert + void insertMessage(MessageModel chatModel); + + @Query("DELETE FROM messages WHERE id = :msgID") + void deleteMessage(String msgID); + + @Query("DELETE FROM messages WHERE chatID = :chatID") + void deleteMessagesByChatID(String chatID); + + @Query("SELECT * FROM messages WHERE chatID = :chatID") + List getMessagesByChatID(String chatID); + + @Query("SELECT * FROM messages WHERE id = :id") + List getMessageByID(String id); +} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/ChatModel.java b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/ChatModel.java new file mode 100644 index 0000000..955fa9e --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/ChatModel.java @@ -0,0 +1,11 @@ +package io.github.chronosx88.influence.models.roomEntities; + +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "chats") +public class ChatModel { + @PrimaryKey String id; + String name; + String keyPairID; +} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageModel.java b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageModel.java new file mode 100644 index 0000000..96d65b9 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/roomEntities/MessageModel.java @@ -0,0 +1,12 @@ +package io.github.chronosx88.influence.models.roomEntities; + +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "messages") +public class MessageModel { + @PrimaryKey String id; + String chatID; + String sender; + String text; +}