|
|
@@ -16,6 +16,7 @@ import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
|
|
|
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
|
|
|
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.Topic
|
|
|
import com.atmob.keyboard_android.util.error.ErrorHandler
|
|
|
+import kotlinx.coroutines.flow.single
|
|
|
|
|
|
/**
|
|
|
* 键盘ViewModel
|
|
|
@@ -242,9 +243,12 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
/**
|
|
|
* 获取键盘列表
|
|
|
*/
|
|
|
- fun getKeyboardList() {
|
|
|
- mKeyboardRepository.getKeyboardList(onSuccess = {
|
|
|
- val infos = it.keyboardInfos
|
|
|
+ fun getKeyboardList() = launch {
|
|
|
+ val resp = mKeyboardRepository.getKeyboardList().single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ } else {
|
|
|
+ val infos = resp.data?.keyboardInfos ?: mutableListOf<KeyboardInfo>()
|
|
|
val keyboardList = infos.map {
|
|
|
fixKeyboardSelectField(it)
|
|
|
}.toList()
|
|
|
@@ -263,9 +267,7 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
}
|
|
|
}
|
|
|
_keyboardList.value = infos
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -282,15 +284,20 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
*/
|
|
|
fun selectedKeyboard(
|
|
|
selectModel: KeyboardInfo,
|
|
|
- onSuccess: (resultObj: EmptyResp) -> Unit
|
|
|
+ onSuccess: (resultObj: EmptyResp) -> Unit,
|
|
|
+ onFail: () -> Unit,
|
|
|
) {
|
|
|
- mKeyboardRepository.selectedKeyboard(selectModel.id!!, onSuccess = {
|
|
|
- // 更新键盘信息
|
|
|
- _currentKeyboardInfo.value = selectModel
|
|
|
- onSuccess.invoke(it)
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- })
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.selectedKeyboard(selectModel.id!!).single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ onFail.invoke()
|
|
|
+ } else {
|
|
|
+ // 更新键盘信息
|
|
|
+ _currentKeyboardInfo.value = selectModel
|
|
|
+ onSuccess.invoke(resp.data!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -317,9 +324,15 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
val keyboardName = keyboardInfo.name
|
|
|
LogUtil.d("加载人设列表, id: ${keyboardId}, name: $keyboardName")
|
|
|
// 查询该键盘下的人设列表
|
|
|
- mKeyboardRepository.getCharacterList(keyboardId, onSuccess = {
|
|
|
- _characterList.value = it.characterInfos ?: listOf()
|
|
|
- }, errorCallback)
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.getCharacterList(keyboardId).single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ errorCallback.invoke(resp.code, resp.message)
|
|
|
+ } else {
|
|
|
+ val data = resp.data
|
|
|
+ _characterList.value = data?.characterInfos ?: listOf()
|
|
|
+ }
|
|
|
+ }
|
|
|
}, onFail = errorCallback)
|
|
|
}
|
|
|
|
|
|
@@ -327,11 +340,15 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
* 获取开场白列表
|
|
|
*/
|
|
|
fun getPrologueList() {
|
|
|
- mKeyboardRepository.getPrologueList(onSuccess = {
|
|
|
- _prologueList.value = it.prologues ?: listOf()
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- })
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.getPrologueList().single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ } else {
|
|
|
+ val data = resp.data
|
|
|
+ _prologueList.value = data?.prologues ?: listOf()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -368,14 +385,18 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
content = _userClipboardData.value ?: ""
|
|
|
)
|
|
|
_chatSuperReplyResult.value = ""
|
|
|
- mKeyboardRepository.chatSuperReply(req, onSuccess = {
|
|
|
- // 只有一条数据,直接添加到输入框
|
|
|
- _chatSuperReplyResult.value = it.content
|
|
|
- onSuccess.invoke()
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- onFail.invoke(errorMsg)
|
|
|
- })
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.chatSuperReply(req).single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ onFail.invoke(resp.message)
|
|
|
+ } else {
|
|
|
+ // 只有一条数据,直接添加到输入框
|
|
|
+ val data = resp.data
|
|
|
+ _chatSuperReplyResult.value = data?.content ?: ""
|
|
|
+ onSuccess.invoke()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
var keyboardId = _currentKeyboardInfo.value?.id ?: ""
|
|
|
@@ -415,14 +436,18 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
// 用户粘贴板中的内容
|
|
|
content = _userClipboardData.value ?: ""
|
|
|
)
|
|
|
- mKeyboardRepository.chatSuperSpeak(req, onSuccess = {
|
|
|
- // 有多条数据,需要跳转列表页面中显示
|
|
|
- _superSpeakChatListResult.value = it.list
|
|
|
- onSuccess.invoke()
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- onFail.invoke(errorMsg)
|
|
|
- })
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.chatSuperSpeak(req).single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ onFail.invoke(resp.message)
|
|
|
+ } else {
|
|
|
+ val data = resp.data
|
|
|
+ // 有多条数据,需要跳转列表页面中显示
|
|
|
+ _superSpeakChatListResult.value = data?.list ?: mutableListOf()
|
|
|
+ onSuccess.invoke()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
var keyboardId = _currentKeyboardInfo.value?.id ?: ""
|
|
|
@@ -451,14 +476,18 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
) {
|
|
|
onStart.invoke()
|
|
|
_prologueChatListResult.value = listOf<String>()
|
|
|
- mKeyboardRepository.chatPrologue(name, onSuccess = {
|
|
|
- // 有多条数据,需要跳转列表页面中显示
|
|
|
- _prologueChatListResult.value = it.list
|
|
|
- onSuccess.invoke()
|
|
|
- }, onFail = { errorCode, errorMsg ->
|
|
|
- ErrorHandler.handleError(errorCode, errorMsg)
|
|
|
- onFail.invoke(errorMsg)
|
|
|
- })
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.chatPrologue(name).single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ onFail.invoke(resp.message)
|
|
|
+ } else {
|
|
|
+ // 有多条数据,需要跳转列表页面中显示
|
|
|
+ val data = resp.data
|
|
|
+ _prologueChatListResult.value = data?.list ?: mutableListOf()
|
|
|
+ onSuccess.invoke()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -468,41 +497,58 @@ class KeyboardViewModel : BaseViewModel() {
|
|
|
onSuccess: (model: KeyboardInfo) -> Unit,
|
|
|
onFail: (errorCode: Int, errorMsg: String) -> Unit
|
|
|
) {
|
|
|
- mKeyboardRepository.getCurrentKeyboardInfo(onSuccess = {
|
|
|
- // 如果当前未在键盘中,手动选择过,就会为null
|
|
|
- if (it.id.isNullOrBlank()) {
|
|
|
- mKeyboardRepository.getKeyboardList(onSuccess = {
|
|
|
- val infos = it.keyboardInfos.map {
|
|
|
- fixKeyboardSelectField(it)
|
|
|
- }
|
|
|
- // 数据为空
|
|
|
- if (infos.isEmpty()) {
|
|
|
- onFail.invoke(-1, "")
|
|
|
- } else {
|
|
|
- // 先找已选中的键盘,否则找系统键盘
|
|
|
- val selectedKeyboard = infos.filter {
|
|
|
- it.isSelect == true
|
|
|
- }.toList().firstOrNull()
|
|
|
- if (selectedKeyboard != null) {
|
|
|
- onSuccess.invoke(selectedKeyboard)
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.getCurrentKeyboardInfo().single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ onFail.invoke(resp.code, resp.message)
|
|
|
+ } else {
|
|
|
+ // 如果当前未在键盘中,手动选择过,就会为null
|
|
|
+ val data = resp.data
|
|
|
+ if (data == null) {
|
|
|
+ onFail.invoke(-1, resp.message)
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
+ if (data.id.isNullOrBlank()) {
|
|
|
+ launch {
|
|
|
+ val resp = mKeyboardRepository.getKeyboardList().single()
|
|
|
+ if (ErrorHandler.isError(resp)) {
|
|
|
+ ErrorHandler.handleError(resp.code, resp.message)
|
|
|
+ onFail.invoke(resp.code, resp.message)
|
|
|
+ return@launch
|
|
|
} else {
|
|
|
- // 没有找到选中的键盘,则使用系统键盘
|
|
|
- val systemKeyboard = infos.filter {
|
|
|
- KeyboardType.isSystem(it.type ?: "")
|
|
|
- }.toList()
|
|
|
- if (systemKeyboard.isNotEmpty()) {
|
|
|
- onSuccess.invoke(systemKeyboard.first())
|
|
|
+ val infos = resp.data?.keyboardInfos?.map {
|
|
|
+ fixKeyboardSelectField(it)
|
|
|
+ } ?: mutableListOf()
|
|
|
+ // 数据为空
|
|
|
+ if (infos.isEmpty()) {
|
|
|
+ onFail.invoke(-1, "")
|
|
|
} else {
|
|
|
- // 连系统键盘都没有,则取第一个
|
|
|
- onSuccess.invoke(infos[0])
|
|
|
+ // 先找已选中的键盘,否则找系统键盘
|
|
|
+ val selectedKeyboard = infos.filter {
|
|
|
+ it.isSelect == true
|
|
|
+ }.toList().firstOrNull()
|
|
|
+ if (selectedKeyboard != null) {
|
|
|
+ onSuccess.invoke(selectedKeyboard)
|
|
|
+ } else {
|
|
|
+ // 没有找到选中的键盘,则使用系统键盘
|
|
|
+ val systemKeyboard = infos.filter {
|
|
|
+ KeyboardType.isSystem(it.type ?: "")
|
|
|
+ }.toList()
|
|
|
+ if (systemKeyboard.isNotEmpty()) {
|
|
|
+ onSuccess.invoke(systemKeyboard.first())
|
|
|
+ } else {
|
|
|
+ // 连系统键盘都没有,则取第一个
|
|
|
+ onSuccess.invoke(infos[0])
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }, onFail)
|
|
|
- } else {
|
|
|
- onSuccess.invoke(fixKeyboardSelectField(it))
|
|
|
+ } else {
|
|
|
+ onSuccess.invoke(fixKeyboardSelectField(data))
|
|
|
+ }
|
|
|
}
|
|
|
- }, onFail)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|