Private
Public Access
1
0

Implements /message fetching

This commit is contained in:
2023-08-07 21:30:46 -07:00
parent 203e1c5ed9
commit 7a5e1fd338
8 changed files with 139 additions and 27 deletions

View File

@@ -5,6 +5,10 @@
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />

View File

@@ -1,19 +0,0 @@
package net.buzzert.kordophone.backend
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import net.buzzert.kordophone.backend.model.Conversation
import java.net.URL
class ChatRepository(baseURL: URL) {
private val client: APIClient = APIClient(baseURL)
private val apiInterface: APIInterface = client.getClient().create(APIInterface::class.java)
suspend fun getVersion(): String {
return apiInterface.getVersion().string()
}
suspend fun fetchConversations(): List<Conversation> {
return apiInterface.getConversations().body()!!
}
}

View File

@@ -0,0 +1,18 @@
package net.buzzert.kordophone.backend.model
import com.google.gson.annotations.SerializedName
import java.util.Date
data class Message(
@SerializedName("text")
val text: String,
@SerializedName("guid")
val guid: String,
@SerializedName("sender")
val sender: String?, // optional: nil means "from me"
@SerializedName("date")
val date: Date,
) {}

View File

@@ -1,4 +1,4 @@
package net.buzzert.kordophone.backend
package net.buzzert.kordophone.backend.server
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

View File

@@ -1,10 +1,12 @@
package net.buzzert.kordophone.backend
package net.buzzert.kordophone.backend.server
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
interface APIInterface {
@GET("/version")
@@ -12,4 +14,7 @@ interface APIInterface {
@GET("/conversations")
suspend fun getConversations(): Response<List<Conversation>>
@GET("/messages")
suspend fun getMessages(@Query("guid") conversationGUID: String): Response<List<Message>>
}

View File

@@ -0,0 +1,24 @@
package net.buzzert.kordophone.backend.server
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import java.net.URL
class ChatRepository(apiInterface: APIInterface) {
// private val client: APIClient = APIClient(baseURL)
// private val apiInterface: APIInterface = client.getClient().create(APIInterface::class.java)
private val apiInterface: APIInterface = apiInterface
suspend fun getVersion(): String {
return apiInterface.getVersion().string()
}
suspend fun fetchConversations(): List<Conversation> {
return apiInterface.getConversations().body()!!
}
suspend fun fetchMessages(conversation: Conversation): List<Message> {
return apiInterface.getMessages(conversation.guid).body()!!
}
}

View File

@@ -0,0 +1,36 @@
package net.buzzert.kordophone.backend.server
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import okhttp3.MediaType
import okhttp3.ResponseBody
import retrofit2.Response
class MockServer: APIInterface {
val version = "Kordophone-2.0"
val conversations: MutableList<Conversation> = mutableListOf()
val messages: MutableMap<String, MutableList<Message>> = mutableMapOf()
override suspend fun getVersion(): ResponseBody {
return ResponseBody.create(MediaType.get("text/plain"), version)
}
override suspend fun getConversations(): Response<List<Conversation>> {
return Response.success(conversations)
}
override suspend fun getMessages(conversationGUID: String): Response<List<Message>> {
return Response.success(messages[conversationGUID])
}
fun addConversation(conversation: Conversation) {
conversations.add(conversation)
}
fun addMessagesToConversation(conversation: Conversation, messages: List<Message>) {
val guid = conversation.guid
this.messages[guid]?.addAll(messages) ?: run {
this.messages[guid] = messages.toMutableList()
}
}
}

View File

@@ -1,10 +1,18 @@
package net.buzzert.kordophone.backend
import kotlinx.coroutines.runBlocking
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.server.APIClient
import net.buzzert.kordophone.backend.server.APIInterface
import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophone.backend.server.MockServer
import org.junit.Test
import org.junit.Assert.*
import java.net.URL
import java.util.Date
import java.util.UUID
/**
* Example local unit test, which will execute on the development machine (host).
@@ -12,22 +20,58 @@ import java.net.URL
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
val repository = ChatRepository(URL("http://localhost:5738"))
private val mockServer = MockServer().also {
val conversation = Conversation(
date = Date(),
participants = listOf("james@magahern.com"),
displayName = null,
unreadCount = 0,
lastMessagePreview = "Hello",
guid = UUID.randomUUID().toString()
)
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
it.addConversation(conversation)
val message = Message(
date = Date(),
text = "Hey",
guid = UUID.randomUUID().toString(),
sender = null,
)
it.addMessagesToConversation(conversation, listOf(message))
}
private val repository = ChatRepository(mockServer)
private fun getLiveRepo(host: String): ChatRepository {
val client = APIClient(URL(host))
return ChatRepository(client.getClient().create(APIInterface::class.java))
}
@Test
fun testGetVersion() = runBlocking {
val version = repository.getVersion()
println("Version: $version")
assertEquals(version, mockServer.version)
}
@Test
fun testFetchConversations() = runBlocking {
val conversations = repository.fetchConversations()
println("Conversations: $conversations")
assertEquals(conversations.count(), 1)
val conversation = conversations.first()
assertEquals(conversation.participants, listOf("james@magahern.com"))
}
@Test
fun testFetchMessages() = runBlocking {
val conversations = repository.fetchConversations()
val messages = repository.fetchMessages(conversations.first())
assertEquals(messages.count(), 1)
val message = messages.first()
assertEquals(message.text, "Hey")
}
}