-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial concepts for Chat Screens / Kotlin 2.1 / Updated other versions
- Loading branch information
Showing
35 changed files
with
1,428 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/kotlin/org/jdc/template/model/db/main/chatmessage/ChatMessageDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.jdc.template.model.db.main.chatmessage | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import org.jdc.template.model.domain.inline.ChatMessageId | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
|
||
@Dao | ||
interface ChatMessageDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(entity: ChatMessageEntity): Long | ||
|
||
@Query("DELETE FROM ChatMessage WHERE id = :id") | ||
suspend fun deleteById(id: ChatMessageId) | ||
|
||
@Query("SELECT * FROM ChatMessage WHERE id = :id") | ||
suspend fun findById(id: ChatMessageId): ChatMessageEntity? | ||
|
||
@Query("SELECT * FROM ChatMessage WHERE chatThreadId = :chatThreadId ORDER BY createdDate DESC") | ||
fun findAllPaging(chatThreadId: ChatThreadId): PagingSource<Int, ChatMessageEntity> | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/kotlin/org/jdc/template/model/db/main/chatmessage/ChatMessageEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.jdc.template.model.db.main.chatmessage | ||
|
||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
import androidx.room.PrimaryKey | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import org.jdc.template.model.db.main.chatthread.ChatThreadEntity | ||
import org.jdc.template.model.domain.inline.ChatMessageId | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
import org.jdc.template.model.domain.inline.IndividualId | ||
import java.util.UUID | ||
|
||
@Entity( | ||
tableName = "ChatMessage", | ||
foreignKeys = [ | ||
ForeignKey( | ||
entity = ChatThreadEntity::class, | ||
parentColumns = arrayOf("id"), | ||
childColumns = arrayOf("chatThreadId"), | ||
deferred = true, | ||
onDelete = ForeignKey.CASCADE | ||
) | ||
] | ||
) | ||
data class ChatMessageEntity( | ||
@PrimaryKey | ||
val id: ChatMessageId = ChatMessageId(UUID.randomUUID().toString()), | ||
val chatThreadId: ChatThreadId, | ||
val individualId: IndividualId, | ||
val message: String, | ||
|
||
val createdDate: Instant = Clock.System.now(), | ||
val lastModified: Instant = Clock.System.now(), | ||
) |
27 changes: 27 additions & 0 deletions
27
app/src/main/kotlin/org/jdc/template/model/db/main/chatthread/ChatThreadDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jdc.template.model.db.main.chatthread | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import kotlinx.coroutines.flow.Flow | ||
import org.jdc.template.model.domain.ChatThreadListItem | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
|
||
@Dao | ||
interface ChatThreadDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(entity: ChatThreadEntity): Long | ||
|
||
@Query("SELECT * FROM ChatThread WHERE id = :id") | ||
suspend fun findById(id: ChatThreadId): ChatThreadEntity? | ||
|
||
@Query("DELETE FROM ChatThread WHERE id = :id") | ||
suspend fun deleteById(id: ChatThreadId) | ||
|
||
@Query("SELECT * FROM ChatThread ORDER BY lastModified DESC") | ||
fun findAllFlow(): Flow<List<ChatThreadEntity>> | ||
|
||
@Query("SELECT id, name FROM ChatThread ORDER BY lastModified DESC") | ||
fun findAllChatThreadListItemFlow(): Flow<List<ChatThreadListItem>> | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/kotlin/org/jdc/template/model/db/main/chatthread/ChatThreadEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jdc.template.model.db.main.chatthread | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
import org.jdc.template.model.domain.inline.IndividualId | ||
import java.util.UUID | ||
|
||
@Entity("ChatThread") | ||
data class ChatThreadEntity( | ||
@PrimaryKey | ||
val id: ChatThreadId = ChatThreadId(UUID.randomUUID().toString()), | ||
val name: String, | ||
val ownerIndividualId: IndividualId, | ||
|
||
val createdDate: Instant = Clock.System.now(), | ||
val lastModified: Instant = Clock.System.now(), | ||
) |
18 changes: 18 additions & 0 deletions
18
app/src/main/kotlin/org/jdc/template/model/domain/ChatMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.jdc.template.model.domain | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import org.jdc.template.model.domain.inline.ChatMessageId | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
import org.jdc.template.model.domain.inline.IndividualId | ||
import java.util.UUID | ||
|
||
data class ChatMessage( | ||
val id: ChatMessageId = ChatMessageId(UUID.randomUUID().toString()), | ||
val chatThreadId: ChatThreadId, | ||
val individualId: IndividualId, | ||
val message: String, | ||
|
||
val createdDate: Instant = Clock.System.now(), | ||
val lastModified: Instant = Clock.System.now(), | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/org/jdc/template/model/domain/ChatThread.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.jdc.template.model.domain | ||
|
||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
import org.jdc.template.model.domain.inline.IndividualId | ||
|
||
data class ChatThread( | ||
val id: ChatThreadId, | ||
val name: String, | ||
val ownerIndividualId: IndividualId, | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/kotlin/org/jdc/template/model/domain/ChatThreadListItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.jdc.template.model.domain | ||
|
||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
|
||
data class ChatThreadListItem( | ||
val id: ChatThreadId, | ||
val name: String, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/kotlin/org/jdc/template/model/repository/ChatRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.jdc.template.model.repository | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.mapLatest | ||
import kotlinx.coroutines.launch | ||
import org.jdc.template.inject.ApplicationScope | ||
import org.jdc.template.inject.IoDispatcher | ||
import org.jdc.template.model.db.main.MainDatabaseWrapper | ||
import org.jdc.template.model.db.main.chatmessage.ChatMessageEntity | ||
import org.jdc.template.model.db.main.chatthread.ChatThreadEntity | ||
import org.jdc.template.model.domain.ChatMessage | ||
import org.jdc.template.model.domain.ChatThread | ||
import org.jdc.template.model.domain.ChatThreadListItem | ||
import org.jdc.template.model.domain.inline.ChatMessageId | ||
import org.jdc.template.model.domain.inline.ChatThreadId | ||
import org.jdc.template.model.domain.inline.IndividualId | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ChatRepository | ||
@Inject constructor( | ||
private val mainDatabaseWrapper: MainDatabaseWrapper, | ||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
@ApplicationScope private val appScope: CoroutineScope, | ||
) { | ||
private fun mainDatabase() = mainDatabaseWrapper.getDatabase() | ||
private fun chatThreadDao() = mainDatabase().chatThreadDao() | ||
private fun chatMessageDao() = mainDatabase().chatMessageDao() | ||
|
||
fun getChatThreadListFlow(): Flow<List<ChatThreadListItem>> = chatThreadDao().findAllChatThreadListItemFlow() | ||
|
||
fun getPagingAllMessagesFlow(chatThreadId: ChatThreadId): Flow<PagingData<ChatMessage>> { | ||
val config = PagingConfig(pageSize = 20, initialLoadSize = 20, enablePlaceholders = false) | ||
|
||
return Pager(config) { | ||
chatMessageDao().findAllPaging(chatThreadId) | ||
}.flow.mapLatest { page: PagingData<ChatMessageEntity> -> | ||
page.map { entity -> | ||
entity.toChatMessage() | ||
} | ||
} | ||
} | ||
|
||
fun sendMessageAsync(chatThreadId: ChatThreadId, individualId: IndividualId, message: String) = appScope.launch(ioDispatcher) { | ||
val newMessage = ChatMessageEntity( | ||
chatThreadId = chatThreadId, | ||
individualId = individualId, | ||
message = message | ||
) | ||
|
||
chatMessageDao().insert(newMessage) | ||
} | ||
|
||
suspend fun deleteMessage(chatMessageId: ChatMessageId) { | ||
chatMessageDao().deleteById(chatMessageId) | ||
} | ||
|
||
suspend fun getChatThreadById(chatThreadId: ChatThreadId): ChatThread? = chatThreadDao().findById(chatThreadId)?.toChatThread() | ||
suspend fun saveNewChatThread(chatThread: ChatThread) { | ||
chatThreadDao().insert(chatThread.toEntity()) | ||
} | ||
} | ||
|
||
private fun ChatMessageEntity.toChatMessage() = ChatMessage( | ||
id = id, | ||
chatThreadId = chatThreadId, | ||
individualId = individualId, | ||
message = message, | ||
createdDate = createdDate, | ||
lastModified = lastModified | ||
) | ||
|
||
private fun ChatThreadEntity.toChatThread() = ChatThread( | ||
id = id, | ||
name = name, | ||
ownerIndividualId = ownerIndividualId | ||
) | ||
|
||
private fun ChatThread.toEntity() = ChatThreadEntity( | ||
id = id, | ||
name = name, | ||
ownerIndividualId = ownerIndividualId | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.