Skip to content

Commit 48429b1

Browse files
committed
Add stop generating button
1 parent dce1a27 commit 48429b1

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

app/src/main/java/com/chatgptlite/wanted/constants/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ const val urlToGithub = "https://github.com/lambiengcode"
99

1010
const val matchResultString = "\"text\":"
1111
const val matchResultTurboString = "\"content\":"
12+
const val ConversationTestTag = "ConversationTestTag"

app/src/main/java/com/chatgptlite/wanted/ui/conversations/Conversation.kt

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package com.chatgptlite.wanted.ui.conversations
22

3+
import androidx.compose.animation.animateContentSize
34
import androidx.compose.foundation.layout.*
45
import androidx.compose.foundation.lazy.LazyColumn
56
import androidx.compose.foundation.lazy.rememberLazyListState
7+
import androidx.compose.foundation.shape.RoundedCornerShape
8+
import androidx.compose.material.icons.Icons
9+
import androidx.compose.material.icons.filled.Stop
10+
import androidx.compose.material3.ExtendedFloatingActionButton
11+
import androidx.compose.material3.Icon
612
import androidx.compose.material3.Surface
13+
import androidx.compose.material3.Text
714
import androidx.compose.runtime.Composable
815
import androidx.compose.runtime.collectAsState
916
import androidx.compose.runtime.getValue
17+
import androidx.compose.ui.Alignment
1018
import androidx.compose.ui.Modifier
19+
import androidx.compose.ui.graphics.Color
1120
import androidx.compose.ui.platform.testTag
1221
import androidx.compose.ui.tooling.preview.Preview
1322
import androidx.compose.ui.unit.dp
1423
import androidx.hilt.navigation.compose.hiltViewModel
24+
import com.chatgptlite.wanted.constants.ConversationTestTag
1525
import com.chatgptlite.wanted.models.MessageModel
1626
import com.chatgptlite.wanted.ui.conversations.components.MessageCard
1727
import com.chatgptlite.wanted.ui.conversations.components.TextInput
@@ -27,19 +37,18 @@ fun Conversation() {
2737
) {
2838
Box(Modifier.fillMaxSize()) {
2939
Column(Modifier.fillMaxSize()) {
30-
MessageList(modifier = Modifier
31-
.weight(1f)
32-
.padding(horizontal = 16.dp))
40+
MessageList(
41+
modifier = Modifier
42+
.weight(1f)
43+
.padding(horizontal = 16.dp)
44+
)
3345
TextInput()
3446
}
3547
}
3648
}
3749
}
3850
}
3951

40-
41-
const val ConversationTestTag = "ConversationTestTag"
42-
4352
@Composable
4453
fun MessageList(
4554
modifier: Modifier = Modifier,
@@ -49,6 +58,7 @@ fun MessageList(
4958

5059
val conversationId by conversationViewModel.currentConversationState.collectAsState()
5160
val messagesMap by conversationViewModel.messagesState.collectAsState()
61+
val isFabExpanded by conversationViewModel.isFabExpanded.collectAsState()
5262

5363
val messages: List<MessageModel> =
5464
if (messagesMap[conversationId] == null) listOf() else messagesMap[conversationId]!!
@@ -76,11 +86,33 @@ fun MessageList(
7686
}
7787
}
7888
}
89+
ExtendedFloatingActionButton(
90+
text = {
91+
Text(text = "Stop Generating", color = Color.White)
92+
},
93+
icon = {
94+
Icon(
95+
imageVector = Icons.Default.Stop,
96+
contentDescription = "Stop Generating",
97+
tint = Color.White,
98+
modifier = Modifier
99+
.size(35.dp)
100+
)
101+
},
102+
onClick = {
103+
conversationViewModel.stopReceivingResults()
104+
},
105+
shape = RoundedCornerShape(16.dp),
106+
modifier = Modifier
107+
.align(Alignment.BottomEnd)
108+
.padding(bottom = 16.dp)
109+
.animateContentSize(),
110+
expanded = isFabExpanded,
111+
)
79112
}
80113
}
81114

82115

83-
84116
@Preview(showBackground = true)
85117
@Composable
86118
fun DefaultPreview2() {

app/src/main/java/com/chatgptlite/wanted/ui/conversations/ConversationViewModel.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ class ConversationViewModel @Inject constructor(
3030
private val _messages: MutableStateFlow<HashMap<String, MutableList<MessageModel>>> =
3131
MutableStateFlow(HashMap())
3232
private val _isFetching: MutableStateFlow<Boolean> = MutableStateFlow(false)
33+
private val _isFabExpanded = MutableStateFlow(false)
3334

3435
val currentConversationState: StateFlow<String> = _currentConversation.asStateFlow()
3536
val conversationsState: StateFlow<MutableList<ConversationModel>> = _conversations.asStateFlow()
3637
val messagesState: StateFlow<HashMap<String, MutableList<MessageModel>>> =
3738
_messages.asStateFlow()
3839
val isFetching: StateFlow<Boolean> = _isFetching.asStateFlow()
40+
val isFabExpanded: StateFlow<Boolean> get() = _isFabExpanded
41+
42+
private var stopReceivingResults = false
43+
44+
3945

4046
suspend fun initialize() {
4147
_isFetching.value = true
@@ -59,6 +65,7 @@ class ConversationViewModel @Inject constructor(
5965
}
6066

6167
suspend fun sendMessage(message: String) {
68+
stopReceivingResults = false
6269
if (getMessagesByConversation(_currentConversation.value).isEmpty()) {
6370
createConversationRemote(message)
6471
}
@@ -83,12 +90,19 @@ class ConversationViewModel @Inject constructor(
8390
messagesTurbo = getMessagesParamsTurbo(_currentConversation.value)
8491
)
8592
)
86-
8793
var answerFromGPT: String = ""
88-
89-
flow.collect {
90-
answerFromGPT += it
94+
// When flow collecting updateLocalAnswer including FAB behavior expanded.
95+
// On completion FAB == false
96+
flow.onCompletion {
97+
setFabExpanded(false)
98+
}.collect { value ->
99+
if (stopReceivingResults) {
100+
setFabExpanded(false)
101+
return@collect
102+
}
103+
answerFromGPT += value
91104
updateLocalAnswer(answerFromGPT.trim())
105+
setFabExpanded(true)
92106
}
93107

94108
// Save to Firestore
@@ -210,4 +224,10 @@ class ConversationViewModel @Inject constructor(
210224

211225
_messages.value = messagesMap
212226
}
227+
fun stopReceivingResults() {
228+
stopReceivingResults = true
229+
}
230+
private fun setFabExpanded(expanded: Boolean) {
231+
_isFabExpanded.value = expanded
232+
}
213233
}

0 commit comments

Comments
 (0)