feat(data): 更新食物营养数据模型并优化向量数据处理
- 在DataBean中将肉蛋相关字段重命名为肉蛋豆,并添加总重量字段 - 新增FoodVectorTool工具类用于处理食物向量数据的分页获取和存储 - 重构InitActivity中的食物向量数据获取逻辑,使用新的工具类替代原有实现 - 在MainActivity中集成食物向量数据同步功能,在清除本地数据后重新获取 - 更新MainScreenPresentation中营养信息显示逻辑,适配新的数据模型结构 - 修改ApiClient中的日志标记逻辑,为不同接口添加特定的标签 - 调整食物订单列表的数据更新方式,优化适配器通知策略
This commit is contained in:
@@ -17,6 +17,7 @@ import com.sw.dualscreen.model.response.FoodVector
|
||||
import com.sw.dualscreen.objbox.Food
|
||||
import com.sw.dualscreen.objbox.FoodModule
|
||||
import com.sw.dualscreen.objbox.ObjectBox
|
||||
import com.sw.dualscreen.utils.FoodVectorTool
|
||||
import com.sw.dualscreen.utils.L
|
||||
import com.sw.dualscreen.utils.NetworkUtils
|
||||
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||
@@ -26,7 +27,7 @@ import kotlinx.coroutines.launch
|
||||
|
||||
class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
companion object {
|
||||
private const val PAGE_SIZE = 100
|
||||
const val PAGE_SIZE = 100
|
||||
}
|
||||
|
||||
private val userViewModel by viewModels<UserViewModel>()
|
||||
@@ -41,52 +42,47 @@ class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
}
|
||||
|
||||
private fun getCollectedFoodVector(block: () -> Unit) {
|
||||
userViewModel.getCollectedFoodVector(
|
||||
param = mutableMapOf(
|
||||
"pageNum" to "$pageNum",
|
||||
"pageSize" to "$PAGE_SIZE",
|
||||
"version" to GlobalData.foodModelVersion
|
||||
)
|
||||
) { items ->
|
||||
if (items == null) {
|
||||
// userViewModel.getCollectedFoodVector(
|
||||
// param = mutableMapOf(
|
||||
// "pageNum" to "$pageNum",
|
||||
// "pageSize" to "$PAGE_SIZE",
|
||||
// "version" to GlobalData.foodModelVersion
|
||||
// )
|
||||
// ) { items ->
|
||||
// if (items == null) {
|
||||
// getVectorErrorDialog()
|
||||
// return@getCollectedFoodVector
|
||||
// }
|
||||
// val list = items
|
||||
// if (pageNum == 1 && list.isEmpty()) {
|
||||
// block()
|
||||
// return@getCollectedFoodVector
|
||||
// }
|
||||
// if (list.size >= PAGE_SIZE) {
|
||||
// saveFoodVector(list)
|
||||
// pageNum++
|
||||
// getCollectedFoodVector(block)
|
||||
// return@getCollectedFoodVector
|
||||
// }
|
||||
// if (list.isNotEmpty()) {
|
||||
// saveFoodVector(list)
|
||||
// block()
|
||||
// }
|
||||
// }
|
||||
|
||||
FoodVectorTool.getFoodVector(
|
||||
userViewModel,
|
||||
onPageFinish = { list ->
|
||||
list?.let { FoodVectorTool.saveFoodVector(lifecycleScope,list) }
|
||||
}, successBlock = {
|
||||
block()
|
||||
}, failureBlock = {
|
||||
getVectorErrorDialog()
|
||||
return@getCollectedFoodVector
|
||||
}
|
||||
val list = items
|
||||
if (pageNum == 1 && list.isEmpty()) {
|
||||
block()
|
||||
return@getCollectedFoodVector
|
||||
}
|
||||
if (list.size >= PAGE_SIZE) {
|
||||
saveFoodVector(list)
|
||||
pageNum++
|
||||
getCollectedFoodVector(block)
|
||||
return@getCollectedFoodVector
|
||||
}
|
||||
if (list.isNotEmpty()) {
|
||||
saveFoodVector(list)
|
||||
block()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// private val box by lazy { ObjectBox.boxStore.boxFor(Food::class) }
|
||||
private fun saveFoodVector(list: List<FoodVector>) {
|
||||
lifecycleScope.launch {
|
||||
val vectorList = list.map { vt ->
|
||||
val foodVector = vt.foodVector?.removeSurrounding("[", "]")?.split(",")
|
||||
?.map { it.toFloatOrNull() ?: 0.0f }?.toFloatArray()
|
||||
Food(
|
||||
collectId = vt.id,
|
||||
foodId = vt.foodId,
|
||||
foodName = vt.foodName,
|
||||
version = vt.version,
|
||||
foodVector = foodVector
|
||||
)
|
||||
}
|
||||
ObjectBox.putAll(vectorList)
|
||||
}
|
||||
}
|
||||
|
||||
override fun initialize() {
|
||||
super.initialize()
|
||||
|
||||
@@ -55,6 +55,7 @@ import com.sw.dualscreen.socket.TcpClient
|
||||
import com.sw.dualscreen.utils.ActivityManager
|
||||
import com.sw.dualscreen.utils.BitmapSaver
|
||||
import com.sw.dualscreen.utils.Debouncer
|
||||
import com.sw.dualscreen.utils.FoodVectorTool
|
||||
import com.sw.dualscreen.utils.GsonUtils
|
||||
import com.sw.dualscreen.utils.ImageUtil
|
||||
import com.sw.dualscreen.utils.IntervalExecutor
|
||||
@@ -215,6 +216,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
withContext(Dispatchers.IO) {
|
||||
FaceServer.getInstance().clearAllFaces()
|
||||
}
|
||||
withContext(Dispatchers.IO) {
|
||||
ObjectBox.removeAll()
|
||||
}
|
||||
FoodVectorTool.getFoodVector(
|
||||
userViewModel = viewModel,
|
||||
onPageFinish = { list ->
|
||||
list?.let { FoodVectorTool.saveFoodVector(lifecycleScope,list) }
|
||||
},
|
||||
successBlock = {
|
||||
viewModel.getUserFaceCache(pageNo = 1)
|
||||
}, failureBlock = {
|
||||
hideWaitingDialog() // 显示提示信息
|
||||
ToastUtils.showToast(it)
|
||||
}
|
||||
)
|
||||
}
|
||||
viewModel.getUserFaceCache(pageNo = 1, onSuccess = {
|
||||
recognizeViewModel.refreshFaceList()
|
||||
|
||||
Reference in New Issue
Block a user