Skip to content

Commit

Permalink
Add platform buttons to select content of assistant bubbles
Browse files Browse the repository at this point in the history
  • Loading branch information
Taewan-P committed Mar 4, 2025
1 parent b549921 commit e8e54e8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.icons.rounded.Refresh
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardColors
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand All @@ -29,6 +34,7 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.halilibo.richtext.commonmark.CommonmarkAstNodeParser
Expand Down Expand Up @@ -148,6 +154,39 @@ fun GPTMobileIcon(loading: Boolean) {
}
}

@Composable
fun PlatformButton(
isLoading: Boolean,
name: String,
selected: Boolean,
onPlatformClick: () -> Unit
) {
val buttonContent: @Composable RowScope.() -> Unit = {
Spacer(modifier = Modifier.width(12.dp))

if (isLoading) {
CircularProgressIndicator(modifier = Modifier.size(16.dp))
Spacer(modifier = Modifier.width(8.dp))
}

Text(
text = name,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = if (selected) MaterialTheme.colorScheme.onSecondaryContainer else MaterialTheme.colorScheme.primary
)
Spacer(modifier = Modifier.width(12.dp))
if (isLoading) Spacer(modifier = Modifier.width(4.dp))
}

TextButton(
modifier = Modifier.widthIn(max = 160.dp),
onClick = onPlatformClick,
colors = if (selected) ButtonDefaults.filledTonalButtonColors() else ButtonDefaults.textButtonColors(),
content = buttonContent
)
}

@Composable
private fun EditIcon(onEditClick: () -> Unit) {
IconButton(onClick = onEditClick) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -18,11 +19,13 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -187,15 +190,35 @@ fun ChatScreen(
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp)
) {
Row(modifier = Modifier.fillMaxWidth()) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
GPTMobileIcon(if (i == groupedMessages.assistantMessages.size - 1) !isIdle else false)
Row(
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.horizontalScroll(rememberScrollState())
) {
chatViewModel.enabledPlatformsInChat.forEachIndexed { j, uid ->
val platform = appEnabledPlatforms.find { it.uid == uid }
PlatformButton(
isLoading = if (i == groupedMessages.assistantMessages.size - 1) isLoading else false,
name = platform?.name ?: stringResource(R.string.unknown),
selected = platformIndexState == j,
onPlatformClick = { chatViewModel.updateChatPlatformIndex(i, j) }
)
Spacer(modifier = Modifier.width(8.dp))
}
}
}
OpponentChatBubble(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp, vertical = 12.dp)
.widthIn(max = maximumOpponentChatBubbleWidth),
canRetry = canUseChat && isIdle,
canRetry = canUseChat && isIdle && i == groupedMessages.assistantMessages.size - 1,
isLoading = if (i == groupedMessages.assistantMessages.size - 1) isLoading else false,
text = assistantContent,
onCopyClick = { clipboardManager.setText(AnnotatedString(assistantContent)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ class ChatViewModel @Inject constructor(
}
}

fun updateChatPlatformIndex(assistantIndex: Int, platformIndex: Int) {
if (assistantIndex >= _chatStates.value.indexStates.size || assistantIndex < 0) return
if (platformIndex >= enabledPlatformsInChat.size || platformIndex < 0) return

_chatStates.update {
val updatedIndex = it.indexStates.toMutableList()
updatedIndex[assistantIndex] = platformIndex
it.copy(indexStates = updatedIndex)
}
}

fun updateQuestion(q: String) = _question.update { q }

fun exportChat(): Pair<String, String> {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,5 @@
<string name="migrated">Migrated</string>
<string name="error">Error</string>
<string name="select_text">Select Text</string>
<string name="unknown">Unknown</string>
</resources>

0 comments on commit e8e54e8

Please sign in to comment.