代码提交

This commit is contained in:
2025-08-11 18:32:04 +08:00
parent 317ac05278
commit 3b4ba599bf
188 changed files with 9770 additions and 34 deletions
@@ -0,0 +1,183 @@
package com.shuwei.dish.match.viewmodel
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.shuwei.dish.match.base.BaseApp
import com.shuwei.dish.match.db.AppRepository
import com.shuwei.dish.match.entity.CookFoodEntity
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
import com.shuwei.dish.match.entity.SeasoningEntity
import com.shuwei.dish.match.utils.ext.toast
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.collections.forEach
class AppViewModel(private val rep: AppRepository) : ViewModel() {
fun updateCookFood(entity: CookFoodEntity) {
viewModelScope.launch {
rep.updateCookFood(entity)
}
}
fun getCookFoodById(foodId: String, cookMode: Int, action: (CookFoodEntity?) -> Unit) {
viewModelScope.launch {
val data = rep.getCookFoodById(foodId, cookMode)
action(data)
}
}
fun getCookFoodList(cookMode: Int, action: (MutableList<CookFoodEntity>?) -> Unit) {
viewModelScope.launch {
val data = rep.getCookFoodList(cookMode)
action(data)
}
}
fun countCookFood(cookMode: Int, action: (Int) -> Unit) {
viewModelScope.launch {
val count = rep.countCookFood(cookMode)
action(count)
}
}
fun saveCookFood(cookMode: Int, entity: CookFoodEntity) {
viewModelScope.launch {
val data = rep.getCookFoodById(entity.foodId, cookMode)
if (data == null) {
rep.insertCookFood(entity)
return@launch
}
rep.updateCookFood(entity)
}
}
fun saveCookFoodAndGoods(
cookMode: Int,
entity: CookFoodEntity,
list: MutableList<CookFoodGoodsEntity>?,
onFinish: () -> Unit
) {
viewModelScope.launch {
val data = rep.getCookFoodById(entity.foodId, cookMode)
if (data != null) {
//存在数据删除
rep.deleteCookFood(entity.foodId, cookMode)
rep.deleteCookFoodGoodsList(entity.foodId)
}
//重新保存数据
rep.insertCookFood(entity)
list?.let { rep.insertGoodsList(it) }
onFinish()
}
}
fun deleteCookFoodAndGoods(
cookMode: Int,
foodId: String,
onFinish: () -> Unit
) {
viewModelScope.launch {
rep.deleteCookFood(foodId = foodId, cookMode = cookMode)
rep.deleteCookFoodGoodsList(foodId)
onFinish()
}
}
//--------------------------------------------------------------------------------------------------------
fun getCookFoodGoodsList(
foodId: String,
action: (list: MutableList<CookFoodGoodsEntity>) -> Unit
) {
viewModelScope.launch {
val list = rep.getCookFoodGoodsList(foodId)
action(list)
}
}
fun updateGoods(entity: CookFoodGoodsEntity) {
viewModelScope.launch {
rep.updateGoods(entity)
}
}
fun saveCookFoodGoods(list: MutableList<CookFoodGoodsEntity>) {
viewModelScope.launch {
val foodId = list[0].foodId
val data: MutableList<CookFoodGoodsEntity>? =
rep.getCookFoodGoodsList(foodId!!)
if (data.isNullOrEmpty()) {
rep.insertGoodsList(list)
return@launch
}
val localDbSeasoningList = rep.getCookFoodGoodsListByType(foodId, 3)
localDbSeasoningList.forEach {
it.isDel = 1
//更新本地调料数据为已删除的状态
rep.updateGoods(it)
}
//筛选出调料数据保存,主辅材不会变更不需要再次处理
val saveSeasoningList = list.filter { it.materialType == 3 }
rep.insertGoodsList(saveSeasoningList.toMutableList())
}
}
//--------------------------------------------------------------------------------------------------------
fun addSeasoning(list: MutableList<SeasoningEntity>, callback: () -> Unit) {
viewModelScope.launch {
rep.deleteBatch()
rep.insertSeasoningList(items = list)
callback()
}
}
private var isProcessing = false
fun loadSeasoning(action: (list: MutableList<SeasoningEntity>) -> Unit) {
viewModelScope.launch {
if (isProcessing) return@launch
isProcessing = true
rep.getAllStream().collect { list ->
action(list)
isProcessing = false
}
}
}
fun deleteEmptySeasoning() {
viewModelScope.launch {
rep.deleteEmptySeasoning()
}
}
fun deleteAll() {
viewModelScope.launch {
rep.deleteBatch()
}
}
fun updateAll(list: MutableList<SeasoningEntity>) {
viewModelScope.launch {
list.forEach {
rep.updateSeasoning(it)
}
}
}
fun saveSeasoning(entity: SeasoningEntity, block:()-> Unit) {
viewModelScope.launch {
val seasoning = rep.getSeasoningBySort(entity.sort)
if (seasoning == null) {
rep.insertSeasoning(entity)
block()
return@launch
}
rep.updateSeasoning(entity.also { it.id = seasoning.id })
block()
}
}
//--------------------------------------------------------------------------------------------------------
}
@@ -0,0 +1,42 @@
//package com.shuwei.dish.match.viewmodel
//
//import androidx.lifecycle.ViewModel
//import androidx.lifecycle.viewModelScope
//import com.shuwei.dish.match.db.CookFoodGoodsRepository
//import com.shuwei.dish.match.entity.CookFoodGoodsEntity
//import kotlinx.coroutines.launch
//
//class CookFoodGoodsViewModel(private val rep: CookFoodGoodsRepository) : ViewModel() {
//
// fun getCookFoodGoodsList(
// foodId: String,
// action: (list: MutableList<CookFoodGoodsEntity>) -> Unit
// ) {
// viewModelScope.launch {
// val list = rep.cookFoodGoodsDao.getCookFoodGoodsList(foodId)
// action(list)
// }
// }
//
// fun saveCookFoodGoods(list: MutableList<CookFoodGoodsEntity>) {
// viewModelScope.launch {
// val foodId = list[0].foodId
// val data: MutableList<CookFoodGoodsEntity>? =
// rep.cookFoodGoodsDao.getCookFoodGoodsList(foodId!!)
// if (data.isNullOrEmpty()) {
// rep.cookFoodGoodsDao.insertAll(list)
// return@launch
// }
// val localDbSeasoningList = rep.cookFoodGoodsDao.getCookFoodGoodsListByType(foodId, 3)
// localDbSeasoningList.forEach {
// it.isDel = 1
// //更新本地调料数据为已删除的状态
// rep.cookFoodGoodsDao.update(it)
// }
// //筛选出调料数据保存,主辅材不会变更不需要再次处理
// val saveSeasoningList = list.filter { it.materialType == 3 }
// rep.cookFoodGoodsDao.insertAll(saveSeasoningList.toMutableList())
// }
// }
//
//}
@@ -0,0 +1,44 @@
//package com.shuwei.dish.match.viewmodel
//
//import androidx.lifecycle.ViewModel
//import androidx.lifecycle.viewModelScope
//import com.shuwei.dish.match.db.CookFoodRepository
//import com.shuwei.dish.match.entity.CookFoodEntity
//import kotlinx.coroutines.launch
//
//class CookFoodViewModel(private val rep: CookFoodRepository) : ViewModel() {
//
// fun getCookFoodById(foodId: String, cookMode: Int, action: (CookFoodEntity?) -> Unit) {
// viewModelScope.launch {
// val data = rep.cookFoodDao.getCookFoodById(foodId, cookMode)
// action(data)
// }
// }
//
// fun getCookFoodList(cookMode: Int, action: (MutableList<CookFoodEntity>?) -> Unit) {
// viewModelScope.launch {
// val data = rep.cookFoodDao.getCookFoodList(cookMode)
// action(data)
// }
// }
//
// fun countCookFood(cookMode: Int, action: (Int) -> Unit) {
// viewModelScope.launch {
// val count = rep.cookFoodDao.countCookFood(cookMode)
// action(count)
// }
// }
//
// fun saveCookFood(cookMode: Int, entity: CookFoodEntity) {
// viewModelScope.launch {
// val data = rep.cookFoodDao.getCookFoodById(entity.foodId, cookMode)
// if (data == null) {
// rep.cookFoodDao.insert(entity)
// return@launch
// }
// rep.cookFoodDao.update(entity)
//
// }
// }
//
//}
@@ -0,0 +1,47 @@
//package com.shuwei.dish.match.viewmodel
//
//import androidx.lifecycle.ViewModel
//import androidx.lifecycle.viewModelScope
//import com.shuwei.dish.match.db.SeasoningRepository
//import com.shuwei.dish.match.entity.SeasoningEntity
//import kotlinx.coroutines.delay
//import kotlinx.coroutines.launch
//
//class DeviceSettingViewModel(private val rep: SeasoningRepository) : ViewModel() {
// fun addSeasoning(list: MutableList<SeasoningEntity>, callback:()-> Unit) {
// viewModelScope.launch {
// rep.seasoningDao.deleteBatch()
// delay(500)
// val rows: Array<Long>? = rep.seasoningDao.insertAll(items = list)
// if (rows.isNullOrEmpty().not()) {
// callback()
// }
// }
// }
//
// private var isProcessing = false
// fun loadSeasoning(action: (list: MutableList<SeasoningEntity>) -> Unit) {
// viewModelScope.launch {
// if (isProcessing) return@launch
// isProcessing = true
// rep.seasoningDao.getAllStream().collect { list ->
// action(list)
// isProcessing = false
// }
// }
// }
//
// fun deleteAll() {
// viewModelScope.launch {
// rep.seasoningDao.deleteBatch()
// }
// }
//
// fun updateAll(list: MutableList<SeasoningEntity>) {
// viewModelScope.launch {
// list.forEach {
// rep.seasoningDao.update(it)
// }
// }
// }
//}
@@ -0,0 +1,24 @@
//package com.shuwei.dish.match.viewmodel
//
//import androidx.lifecycle.ViewModel
//import androidx.lifecycle.viewModelScope
//import com.shuwei.dish.match.db.SeasoningRepository
//import com.shuwei.dish.match.entity.CookFoodEntity
//import com.shuwei.dish.match.entity.SeasoningEntity
//import kotlinx.coroutines.launch
//
//class SubmitDishViewModel(private val rep: SeasoningRepository) : ViewModel() {
//
// private var isProcessing = false
// fun loadSeasoning(action: (list: MutableList<SeasoningEntity>) -> Unit) {
// viewModelScope.launch {
// if (isProcessing) return@launch
// isProcessing = true
// rep.seasoningDao.getAllStream().collect { list ->
// action(list)
// isProcessing = false
// }
// }
// }
//
//}
@@ -0,0 +1,43 @@
package com.shuwei.dish.match.viewmodel.factory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.shuwei.dish.match.db.AppRepository
import com.shuwei.dish.match.viewmodel.AppViewModel
@Suppress("UNCHECKED_CAST")
class AppFactory(private val repo: AppRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return AppViewModel(repo) as T
}
}
//
//@Suppress("UNCHECKED_CAST")
//class DeviceSettingFactory(private val repo: SeasoningRepository) : ViewModelProvider.Factory {
// override fun <T : ViewModel> create(modelClass: Class<T>): T {
// return DeviceSettingViewModel(repo) as T
// }
//}
//
////@Suppress("UNCHECKED_CAST")
////class SubmitDishFactory(private val repo: SeasoningRepository) : ViewModelProvider.Factory {
//// override fun <T : ViewModel> create(modelClass: Class<T>): T {
//// return SubmitDishViewModel(repo) as T
//// }
////}
//
//@Suppress("UNCHECKED_CAST")
//class CookFoodFactory(private val repo: CookFoodRepository) : ViewModelProvider.Factory {
// override fun <T : ViewModel> create(modelClass: Class<T>): T {
// return CookFoodViewModel(repo) as T
// }
//}
//
//@Suppress("UNCHECKED_CAST")
//class CookFoodGoodsFactory(private val repo: CookFoodGoodsRepository) : ViewModelProvider.Factory {
// override fun <T : ViewModel> create(modelClass: Class<T>): T {
// return CookFoodGoodsViewModel(repo) as T
// }
//}