I started using Room database and went through several docs to create room entities. These are my relations. A Chat Channel can have Many Conversations. So this goes as one-to-many relationship. Hence i created entities as below.
Channel Entity
@Entity(primaryKeys = ["channelId"])
@TypeConverters(TypeConverters::class)
data class Channel(
@field:SerializedName("channelId")
val channelId: String,
@field:SerializedName("channelName")
val channelName: String,
@field:SerializedName("createdBy")
val creationTs: String,
@field:SerializedName("creationTs")
val createdBy: String,
@field:SerializedName("members")
val members: List<String>,
@field:SerializedName("favMembers")
val favMembers: List<String>
) {
// Does not show up in the response but set in post processing.
var isOneToOneChat: Boolean = false
var isChatBot: Boolean = false
}
Conversation Entity
@Entity(primaryKeys = ["msgId"],
foreignKeys = [
ForeignKey(entity = Channel::class,
parentColumns = arrayOf("channelId"),
childColumns = arrayOf("msgId"),
onUpdate = CASCADE,
onDelete = CASCADE
)
])
@TypeConverters(TypeConverters::class)
data class Conversation(
@field:SerializedName("msgId")
val msgId: String,
@field:SerializedName("employeeID")
val employeeID: String,
@field:SerializedName("channelId")
val channelId: String,
@field:SerializedName("channelName")
val channelName: String,
@field:SerializedName("sender")
val sender: String,
@field:SerializedName("sentAt")
val sentAt: String,
@field:SerializedName("senderName")
val senderName: String,
@field:SerializedName("status")
val status: String,
@field:SerializedName("msgType")
val msgType: String,
@field:SerializedName("type")
val panicType: String?,
@field:SerializedName("message")
val message: List<Message>,
@field:SerializedName("deliveredTo")
val delivered: List<Delivered>?,
@field:SerializedName("readBy")
val read: List<Read>?
) {
data class Message(
@field:SerializedName("txt")
val txt: String,
@field:SerializedName("lang")
val lang: String,
@field:SerializedName("trans")
val trans: String
)
data class Delivered(
@field:SerializedName("employeeID")
val employeeID: String,
@field:SerializedName("date")
val date: String
)
data class Read(
@field:SerializedName("employeeID")
val employeeID: String,
@field:SerializedName("date")
val date: String
)
// Does not show up in the response but set in post processing.
var isHeaderView: Boolean = false
}
Now as you can see Conversation belongs to a Channel. When user sees a list of channels, i need to display several attributes of last Conversation in the list item. My question is, is it enough if i just declare relation like above or should i contain Converstion object in Channel class? What are the other ways in which i can handle it? Because UI needs to get most recent conversation that happened along with time, status etc. in each item of the channel list when user scrolls. So there should not be any lag in UI because of this when i query.
And how can i have recent Converstaion object in Channel object?