优化
This commit is contained in:
@@ -3,6 +3,7 @@ package com.sw.inbound.viewmodel
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.sw.inbound.GlobalData
|
||||
import com.sw.inbound.model.response.ApiResponse
|
||||
import com.sw.inbound.model.response.DictType
|
||||
@@ -12,7 +13,9 @@ import com.sw.inbound.model.response.SearchGoodsInfo
|
||||
import com.sw.inbound.network.LoadingState
|
||||
import com.sw.inbound.objbox.FoodModule
|
||||
import com.sw.inbound.repository.RemoteRepository
|
||||
import com.sw.inbound.utils.SPUtil
|
||||
import com.sw.inbound.utils.ToastUtils
|
||||
import com.sw.inbound.utils.ext.toType
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -201,19 +204,19 @@ abstract class BaseViewModel(
|
||||
}
|
||||
|
||||
fun recognizeGoodsList(
|
||||
nameList: List<String>?=null,
|
||||
scoreList: List<FoodModule.IdNameScore>?=null,
|
||||
nameList: List<String>? = null,
|
||||
scoreList: List<FoodModule.IdNameScore>? = null,
|
||||
callback: (List<SearchGoodsInfo.Record>) -> Unit
|
||||
) {
|
||||
launch {
|
||||
if (nameList.isNullOrEmpty() && scoreList.isNullOrEmpty()) return@launch
|
||||
val paramList = if (nameList.isNullOrEmpty()) scoreList!!.map { it.name } else nameList
|
||||
val response = repository.recognizeGoodsList(paramList)
|
||||
val list = if (parseResponse(response)) response.data?:listOf() else listOf()
|
||||
val list = if (parseResponse(response)) response.data ?: listOf() else listOf()
|
||||
if (scoreList.isNullOrEmpty().not()) {
|
||||
list.forEach { record ->
|
||||
val item = scoreList.firstOrNull { it.name == record.goodsName }
|
||||
record.score = ((1 - (item?.score?:0.0)) * 10000).roundToInt()
|
||||
record.score = ((1 - (item?.score ?: 0.0)) * 10000).roundToInt()
|
||||
}
|
||||
}
|
||||
callback(list)
|
||||
@@ -333,4 +336,42 @@ abstract class BaseViewModel(
|
||||
// GlobalData.activeKey = equipmentInfo.arcsoftActiveKey!!
|
||||
GlobalData.restId = equipmentInfo.canteenId!!
|
||||
}
|
||||
|
||||
val goodsKey = "goods"
|
||||
|
||||
fun putGoodsData(key: String, jsonData: String) {
|
||||
try {
|
||||
SPUtil.getInstance(spName = goodsKey).put(key, jsonData)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun getGoodsData(key: String):String? {
|
||||
try {
|
||||
return SPUtil.getInstance(spName = goodsKey).get<String>(key)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fun getGoodsList(key:String):List<GoodsInfo>? {
|
||||
try {
|
||||
val jsonData = getGoodsData(key)
|
||||
return jsonData?.toType(typeToken=object:TypeToken<List<GoodsInfo>?>(){})
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
fun deleteKey(key:String) {
|
||||
try {
|
||||
SPUtil.getInstance(spName = goodsKey).remove(key)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user