-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6652630
commit 11ab818
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
android/app/src/main/java/com/pras/slugcourses/ChatScreen.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 |
---|---|---|
@@ -1,9 +1,51 @@ | ||
package com.pras.slugcourses | ||
|
||
import android.util.Log | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavController | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.request.prepareGet | ||
import io.ktor.utils.io.ByteReadChannel | ||
import io.ktor.utils.io.core.isEmpty | ||
import io.ktor.utils.io.core.readBytes | ||
|
||
//todo pls fix | ||
private const val CHAT_URL = "http://localhost:8000/?userinput=" | ||
private const val TAG = "ChatScreen" | ||
@Composable | ||
fun ChatScreen(navController: NavController) { | ||
Log.d(TAG, "ChatScreen") | ||
//todo pls fix | ||
//queryChat("hello") | ||
Row(Modifier.padding(16.dp), verticalAlignment = Alignment.Bottom, horizontalArrangement = Arrangement.Center) { | ||
//todo pls fix | ||
|
||
} | ||
} | ||
|
||
suspend fun queryChat(query: String) { | ||
//todo pls fix | ||
val client = HttpClient(CIO) | ||
|
||
client.prepareGet(CHAT_URL + query) .execute { httpResponse -> | ||
val channel: ByteReadChannel = httpResponse.body() | ||
var response: String = "" | ||
while (!channel.isClosedForRead) { | ||
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong()) | ||
while (!packet.isEmpty) { | ||
val bytes = packet.readBytes() | ||
response+=String(bytes) | ||
Log.d(TAG, packet.readBytes().toString()) | ||
} | ||
} | ||
Log.d(TAG, response) | ||
} | ||
} |