Skip to content

Commit

Permalink
Using suspending functions when accessing the room database
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxcucus committed Sep 23, 2024
1 parent 17cae19 commit 3cb8bd8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions kotlin/PlanesAndroid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ dependencies {

implementation 'androidx.room:room-runtime:2.6.1'
kapt 'androidx.room:room-compiler:2.6.1'
implementation 'androidx.room:room-ktx:2.6.1'

implementation 'junit:junit:4.13.2'
implementation 'com.google.truth:truth:1.4.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import java.util.Date
interface ChatDao {

@Query("INSERT into ChatMessages (sender_id, sender_name, message, created_at, receiver_id, receiver_name, recorder_id, recorder_name) VALUES (:senderId, :senderName, :m, :createdAt, :receiverId, :receiverName, :recorderId, :recorderName)")
fun addChatMessage(senderId: Long, senderName: String, m : String, createdAt : Date, receiverId : Long, receiverName : String, recorderId: Long, recorderName: String)
suspend fun addChatMessage(senderId: Long, senderName: String, m : String, createdAt : Date, receiverId : Long, receiverName : String, recorderId: Long, recorderName: String)

@Query("DELETE from ChatMessages WHERE current_timestamp - created_at <= :daysBefore * 24 * 60 * 60 * 1000 ")
fun deleteOldMessages(daysBefore: Int)
suspend fun deleteOldMessages(daysBefore: Int)

@Query("SELECT id, sender_id, sender_name, message, created_at, receiver_id, receiver_name, recorder_id, recorder_name FROM ChatMessages WHERE ((receiver_id = :userid and receiver_name = :username) or (sender_id = :userid and sender_name = :username)) and recorder_id = :userid and recorder_name = :username ORDER BY created_at ASC")
fun getMessages(username: String, userid: Long) : List<ChatMessage>
suspend fun getMessages(username: String, userid: Long) : List<ChatMessage>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DatabaseService internal constructor(private val m_Context: Context) : IDa
).build()
}

override fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String) {
override suspend fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String) {
var senderId = message.m_SenderId.toLong()
var senderName = message.m_SenderName
var receiverId = message.m_ReceiverId.toLong()
Expand All @@ -27,11 +27,13 @@ class DatabaseService internal constructor(private val m_Context: Context) : IDa
val formatter = SimpleDateFormat("dd MM yyyy HH:mm:ss")
formatter.timeZone = TimeZone.getTimeZone("GMT");
var formattedDate = message.m_CreatedAt
val date: Date = formatter.parse(formattedDate)
val date: Date? = formatter.parse(formattedDate)

if (date == null)
return
var dao = db.chatDao()

dao.addChatMessage(senderId, senderName, m, date, receiverId, receiverName, recorderId, recorderName)
dao.addChatMessage(senderId, senderName, m, date!!, receiverId, receiverName, recorderId, recorderName)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DatabaseServiceGlobal : IDatabaseService {
return
global_Service = DatabaseService(context)
}
override fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String) {
override suspend fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String) {
global_Service!!.addChatMessage(message, recorderId, recorderName)
}
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.planes.android.chat
import com.planes.multiplayer_engine.responses.ChatMessageResponse

interface IDatabaseService {
fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String)
suspend fun addChatMessage(message: ChatMessageResponse, recorderId: Long, recorderName: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.TimeUnit

class ReceiveChatMessagesService(databaseService: IDatabaseService) : IReceiveChatMessagesService {
Expand Down Expand Up @@ -56,8 +58,13 @@ class ReceiveChatMessagesService(databaseService: IDatabaseService) : IReceiveCh
return;
var chatMessages = body.m_Messages

for (message in chatMessages) {
m_DatabaseService.addChatMessage(message, m_PlaneRound.getUserId(), m_PlaneRound.getUsername())

runBlocking { // this: CoroutineScope
launch {
for (message in chatMessages) {
m_DatabaseService.addChatMessage(message, m_PlaneRound.getUserId(), m_PlaneRound.getUsername())
}
}
}

if (m_UpdateChat)
Expand Down

0 comments on commit 3cb8bd8

Please sign in to comment.