-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CMM-837 oddie bot support UI #22284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
CMM-837 oddie bot support UI #22284
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
ee9bde8
Creating library
adalpari df7aa44
Creating list screen
adalpari 488c373
Adding conversation screen
adalpari 1fee31d
Adding the + and the welcome items
adalpari 036fec7
Navigating to the new ai support
adalpari 817feb0
Renaming
adalpari b74f1ab
Some refactor
adalpari 77423c4
Extracting common methods
adalpari 5ac7784
Accessing the API
adalpari 6324b04
Modifying fields
adalpari d677ce0
Fixing message visibuil
adalpari e14fe06
Some styling
adalpari d804184
Extracting strings
adalpari 82f1da0
Improving previews in code
adalpari 54e8621
Previewing dark mode
adalpari 2b6c494
Removing unused func
adalpari 959072d
Some styling
adalpari 0195829
Extracting model classes
adalpari 368ea9a
Some refactoring
adalpari 53463e8
detekt
adalpari f17a90f
Username
adalpari e80af2d
manifest fix
adalpari 8cb5d72
Merge branch 'trunk' into feature/CMM-837-Oddie-bot-support-UI
adalpari 617429d
Moving module inside wordpress
adalpari a29d75e
Fixing theme
adalpari d1e25ee
Message fix
adalpari fe88e81
Min fixes
adalpari cea5b38
Remove empty lines
adalpari df2a5a4
Merge remote-tracking branch 'origin/trunk' into feature/CMM-837-Oddi…
adalpari 167992c
Removing debug code
adalpari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
WordPress/src/main/java/org/wordpress/android/support/aibot/model/BotConversation.kt
This file contains hidden or 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,11 @@ | ||
| package org.wordpress.android.support.aibot.model | ||
|
|
||
| import java.util.Date | ||
|
|
||
| data class BotConversation( | ||
| val id: Long, | ||
| val createdAt: Date, | ||
| val mostRecentMessageDate: Date, | ||
| val lastMessage: String, | ||
| val messages: List<BotMessage> | ||
| ) |
11 changes: 11 additions & 0 deletions
11
WordPress/src/main/java/org/wordpress/android/support/aibot/model/BotMessage.kt
This file contains hidden or 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,11 @@ | ||
| package org.wordpress.android.support.aibot.model | ||
|
|
||
| import java.util.Date | ||
|
|
||
| data class BotMessage( | ||
| val id: Long, | ||
| val text: String, | ||
| val date: Date, | ||
| val userWantsToTalkToHuman: Boolean, | ||
| val isWrittenByUser: Boolean | ||
| ) |
109 changes: 109 additions & 0 deletions
109
WordPress/src/main/java/org/wordpress/android/support/aibot/ui/AIBotSupportActivity.kt
This file contains hidden or 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,109 @@ | ||
| package org.wordpress.android.support.aibot.ui | ||
|
|
||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.ui.platform.ComposeView | ||
| import androidx.compose.ui.platform.ViewCompositionStrategy | ||
| import androidx.navigation.NavHostController | ||
| import androidx.navigation.compose.NavHost | ||
| import androidx.navigation.compose.composable | ||
| import androidx.navigation.compose.rememberNavController | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import org.wordpress.android.ui.compose.theme.AppThemeM3 | ||
|
|
||
| @AndroidEntryPoint | ||
| class AIBotSupportActivity : AppCompatActivity() { | ||
| private val viewModel by viewModels<AIBotSupportViewModel>() | ||
|
|
||
| private lateinit var composeView: ComposeView | ||
| private lateinit var navController: NavHostController | ||
|
|
||
| private lateinit var userName: String | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| userName = intent.getStringExtra(USERNAME).orEmpty() | ||
| composeView = ComposeView(this) | ||
| setContentView( | ||
| composeView.apply { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
| this.isForceDarkAllowed = false | ||
| } | ||
| setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
| setContent { | ||
| NavigableContent() | ||
| } | ||
| } | ||
| ) | ||
| viewModel.init(intent.getStringExtra(ACCESS_TOKEN_ID)!!) | ||
| } | ||
|
|
||
| private enum class ConversationScreen { | ||
| List, | ||
| Detail | ||
| } | ||
|
|
||
| @Composable | ||
| private fun NavigableContent() { | ||
| navController = rememberNavController() | ||
|
|
||
| AppThemeM3 { | ||
| NavHost( | ||
| navController = navController, | ||
| startDestination = ConversationScreen.List.name | ||
| ) { | ||
| composable(route = ConversationScreen.List.name) { | ||
| ConversationsListScreen( | ||
| conversations = viewModel.conversations, | ||
| onConversationClick = { conversation -> | ||
| viewModel.selectConversation(conversation) | ||
| navController.navigate(ConversationScreen.Detail.name) | ||
| }, | ||
| onBackClick = { finish() }, | ||
| onCreateNewConversationClick = { | ||
| viewModel.createNewConversation() | ||
| viewModel.selectedConversation.value?.let { newConversation -> | ||
| navController.navigate(ConversationScreen.Detail.name) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| composable(route = ConversationScreen.Detail.name) { | ||
| val selectedConversation by viewModel.selectedConversation.collectAsState() | ||
| selectedConversation?.let { conversation -> | ||
| ConversationDetailScreen( | ||
| userName = userName, | ||
| conversation = conversation, | ||
| onBackClick = { navController.navigateUp() }, | ||
| onSendMessage = { text -> | ||
| viewModel.sendMessage(text) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val ACCESS_TOKEN_ID = "arg_access_token_id" | ||
| private const val USERNAME = "arg_username" | ||
| @JvmStatic | ||
| fun createIntent( | ||
| context: Context, | ||
| accessToken: String, | ||
| userName: String, | ||
| ): Intent = Intent(context, AIBotSupportActivity::class.java).apply { | ||
| putExtra(ACCESS_TOKEN_ID, accessToken) | ||
| putExtra(USERNAME, userName) | ||
| } | ||
| } | ||
| } |
156 changes: 156 additions & 0 deletions
156
WordPress/src/main/java/org/wordpress/android/support/aibot/ui/AIBotSupportViewModel.kt
This file contains hidden or 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,156 @@ | ||
| package org.wordpress.android.support.aibot.ui | ||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import org.wordpress.android.support.aibot.util.generateSampleBotConversations | ||
| import org.wordpress.android.support.aibot.model.BotConversation | ||
| import org.wordpress.android.support.aibot.model.BotMessage | ||
| import rs.wordpress.api.kotlin.WpComApiClient | ||
| import rs.wordpress.api.kotlin.WpRequestResult | ||
| import uniffi.wp_api.BotConversationSummary | ||
| import uniffi.wp_api.WpAuthentication | ||
| import uniffi.wp_api.WpAuthenticationProvider | ||
| import java.util.Date | ||
| import javax.inject.Inject | ||
|
|
||
| private const val BOT_ID = "jetpack-chat-mobile" | ||
|
|
||
| @HiltViewModel | ||
| class AIBotSupportViewModel @Inject constructor() : ViewModel() { | ||
| private val _conversations = MutableStateFlow<List<BotConversation>>(emptyList()) | ||
| val conversations: StateFlow<List<BotConversation>> = _conversations.asStateFlow() | ||
|
|
||
| private val _selectedConversation = MutableStateFlow<BotConversation?>(null) | ||
| val selectedConversation: StateFlow<BotConversation?> = _selectedConversation.asStateFlow() | ||
|
|
||
| private lateinit var accessToken: String | ||
|
|
||
| private val wpComApiClient: WpComApiClient by lazy { | ||
| WpComApiClient( | ||
| WpAuthenticationProvider.staticWithAuth(WpAuthentication.Bearer(token = accessToken) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| fun init(accessToken: String) { | ||
| loadDummyData() | ||
|
|
||
| this.accessToken = accessToken | ||
| // loadConversations() | ||
| } | ||
|
|
||
| fun loadConversations() { | ||
| viewModelScope.launch { | ||
| val response = wpComApiClient.request { requestBuilder -> | ||
| requestBuilder.supportBots().getBotConverationList(BOT_ID) | ||
| } | ||
| when (response) { | ||
| is WpRequestResult.Success -> { | ||
| val conversations = response.response.data | ||
| _conversations.value = conversations.toBotConversations() | ||
| } | ||
|
|
||
| else -> { | ||
| // stub for now | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun selectConversation(conversation: BotConversation) { | ||
| _selectedConversation.value = conversation | ||
| } | ||
|
|
||
| fun createNewConversation() { | ||
| val now = Date() | ||
|
|
||
| // Create initial bot greeting message | ||
| val greetingMessage = BotMessage( | ||
| id = 0, | ||
| text = "Hi! I'm here to help you with any questions about WordPress. How can I assist you today?", | ||
| date = now, | ||
| userWantsToTalkToHuman = false, | ||
| isWrittenByUser = false | ||
| ) | ||
|
|
||
| val newConversation = BotConversation( | ||
| id = 0, | ||
| mostRecentMessageDate = now, | ||
| messages = listOf(greetingMessage), | ||
| createdAt = now, | ||
| lastMessage = greetingMessage.text | ||
| ) | ||
|
|
||
| // Add to the top of the conversations list | ||
| _conversations.value = listOf(newConversation) + _conversations.value | ||
|
|
||
| // Select the new conversation | ||
| _selectedConversation.value = newConversation | ||
| } | ||
|
|
||
| fun sendMessage(text: String) { | ||
| val currentConversation = _selectedConversation.value ?: return | ||
| val now = Date() | ||
| val userMessageId = System.currentTimeMillis() | ||
|
|
||
| // Create new user message | ||
| val userMessage = BotMessage( | ||
| id = userMessageId, | ||
| text = text, | ||
| date = now, | ||
| userWantsToTalkToHuman = false, | ||
| isWrittenByUser = true | ||
| ) | ||
|
|
||
| // Create bot response (dummy response for now) | ||
| val botMessage = BotMessage( | ||
| id = userMessageId + 1, // Ensure unique ID by incrementing | ||
| text = "Thanks for your message! This is a dummy response. In a real implementation, " + | ||
| "this would connect to the support bot API.", | ||
| date = Date(now.time + 1), // Slightly later timestamp | ||
| userWantsToTalkToHuman = false, | ||
| isWrittenByUser = false | ||
| ) | ||
|
|
||
| // Update conversation with new messages | ||
| val updatedMessages = currentConversation.messages + listOf(userMessage, botMessage) | ||
| val updatedConversation = currentConversation.copy( | ||
| messages = updatedMessages, | ||
| mostRecentMessageDate = botMessage.date | ||
adalpari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| // Update the conversation in the list | ||
| _conversations.value = _conversations.value.map { conversation -> | ||
| if (conversation.id == updatedConversation.id) { | ||
| updatedConversation | ||
| } else { | ||
| conversation | ||
| } | ||
| } | ||
|
|
||
| // Update selected conversation | ||
| _selectedConversation.value = updatedConversation | ||
| } | ||
|
|
||
| private fun loadDummyData() { | ||
| _conversations.value = generateSampleBotConversations() | ||
| } | ||
|
|
||
| private fun List<BotConversationSummary>.toBotConversations(): List<BotConversation> = | ||
| map { it.toBotConversation() } | ||
|
|
||
|
|
||
| private fun BotConversationSummary.toBotConversation(): BotConversation = | ||
| BotConversation ( | ||
| id = chatId.toLong(), | ||
| createdAt = createdAt, | ||
| mostRecentMessageDate = lastMessage.createdAt, | ||
| lastMessage = lastMessage.content, | ||
| messages = listOf() | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.