Skip to content

Commit

Permalink
fix: make sure when model or type is changed for gpt action, it will …
Browse files Browse the repository at this point in the history
…be applied immediately
  • Loading branch information
plateaukao committed Aug 20, 2024
1 parent 2bc1d68 commit 39834fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,14 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
}

is Gpt -> {
val gptAction = config.gptActionList[state.gptActionIndex]
translationViewModel.viewModelScope.launch {
translationViewModel.updateInputMessage(actionModeMenuViewModel.selectedText.value)
val selectedTextWithContext = ninjaWebView.getSelectedTextWithContext()
translationViewModel.updateMessageWithContext(selectedTextWithContext)
if (translationViewModel.hasOpenAiApiKey()) {
translationViewModel.updateTranslateMethod(TRANSLATE_API.GPT)
translationViewModel.gptActionInfo = state.gptAction
translationViewModel.gptActionInfo = gptAction
translationViewModel.url = ninjaWebView.url.orEmpty()
TranslateDialogFragment(
translationViewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ private fun GptRow(
ActionMenuItem(
gptActionInfo.name,
null,
onClicked = {
translationViewModel.gptActionInfo = gptActionInfo
onClicked = { // need to pass in index so that the action can be updated
translationViewModel.gptActionInfo =
translationViewModel.getGptActionList()[index]
translationViewModel.translate(TRANSLATE_API.GPT)
},
onLongClicked = { translationViewModel.showEditGptActionDialog(index) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.preference.ChatGPTActionInfo
import info.plateaukao.einkbro.preference.ConfigManager
import info.plateaukao.einkbro.preference.HighlightStyle
import info.plateaukao.einkbro.unit.ShareUtil
Expand Down Expand Up @@ -190,16 +189,18 @@ class ActionModeMenuViewModel : ViewModel(), KoinComponent {
action = { _actionModeMenuState.value = ActionModeMenuState.DeeplTranslate }
)
)
if (configManager.gptApiKey.isNotEmpty() && configManager.gptActionList.isNotEmpty()) {
for (actionInfo in configManager.gptActionList.reversed()) {
if (configManager.gptActionList.isNotEmpty()) {
configManager.gptActionList.reversed().map { actionInfo ->
menuInfos.add(
0,
MenuInfo(
actionInfo.name,
icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_gpt),
action = {
_actionModeMenuState.value =
ActionModeMenuState.Gpt(actionInfo)
ActionModeMenuState.Gpt(
configManager.gptActionList.indexOf(actionInfo)
)
}
)
)
Expand Down Expand Up @@ -276,7 +277,7 @@ class ActionModeMenuViewModel : ViewModel(), KoinComponent {

sealed class ActionModeMenuState {
data object Idle : ActionModeMenuState()
class Gpt(val gptAction: ChatGPTActionInfo) : ActionModeMenuState()
class Gpt(val gptActionIndex: Int) : ActionModeMenuState()
data object GoogleTranslate : ActionModeMenuState()
data object DeeplTranslate : ActionModeMenuState()
data object Papago : ActionModeMenuState()
Expand Down

0 comments on commit 39834fb

Please sign in to comment.