Added basic implementation of ChatDB (Room-based)

This commit is contained in:
ChronosX88 2019-03-18 17:17:09 +04:00
parent a8b8a3f902
commit 72812f4e03
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
6 changed files with 95 additions and 1 deletions

View File

@ -5,10 +5,11 @@ import android.content.Context;
import net.tomp2p.dht.PeerDHT; import net.tomp2p.dht.PeerDHT;
import androidx.room.Room;
import io.github.chronosx88.influence.observable.MainObservable; 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 { public class AppHelper extends Application {
@ -16,12 +17,17 @@ public class AppHelper extends Application {
private static MainObservable observable; private static MainObservable observable;
private static String peerID; private static String peerID;
private static PeerDHT peerDHT; private static PeerDHT peerDHT;
private static RoomHelper chatDB;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
instance = this; instance = this;
observable = new MainObservable(); observable = new MainObservable();
chatDB = Room.databaseBuilder(getApplicationContext(), RoomHelper.class, "chatDB")
.allowMainThreadQueries()
.build();
} }
public static void storePeerID(String peerID1) { peerID = peerID1; } 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 String getPeerID() { return peerID; }
public static PeerDHT getPeerDHT() { return peerDHT; } public static PeerDHT getPeerDHT() { return peerDHT; }
public static RoomHelper getChatDB() { return chatDB; }
} }

View File

@ -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();
}

View File

@ -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<ChatModel> getAllChats();
@Query("SELECT * FROM chats WHERE id = :chatID")
List<ChatModel> getChatByID(String chatID);
}

View File

@ -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<MessageModel> getMessagesByChatID(String chatID);
@Query("SELECT * FROM messages WHERE id = :id")
List<MessageModel> getMessageByID(String id);
}

View File

@ -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;
}

View File

@ -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;
}