优化
This commit is contained in:
@@ -7,6 +7,7 @@ import com.sw.dualscreen.model.response.ApiResponse
|
||||
import com.sw.dualscreen.model.response.EquipmentInfo
|
||||
import com.sw.dualscreen.network.ApiClient
|
||||
import com.sw.dualscreen.repository.RemoteRepository
|
||||
import com.sw.dualscreen.utils.Debouncer
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -24,15 +25,17 @@ abstract class BaseViewModel() : ViewModel() {
|
||||
* 带进度条的请求
|
||||
*/
|
||||
protected fun launchWithLoading(block: suspend () -> Unit) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_showLoading.value = true
|
||||
block()
|
||||
} catch (e: Exception) {
|
||||
// 错误处理可被子类重写
|
||||
handleError(e)
|
||||
} finally {
|
||||
_showLoading.value = false
|
||||
Debouncer(1000).debounce {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_showLoading.value = true
|
||||
block()
|
||||
} catch (e: Exception) {
|
||||
// 错误处理可被子类重写
|
||||
handleError(e)
|
||||
} finally {
|
||||
_showLoading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,19 +44,23 @@ abstract class BaseViewModel() : ViewModel() {
|
||||
* 不带进度条的请求
|
||||
*/
|
||||
protected fun launch(block: suspend () -> Unit) {
|
||||
viewModelScope.launch() {
|
||||
try {
|
||||
block()
|
||||
} catch (e: Exception) {
|
||||
// 错误处理可被子类重写
|
||||
handleError(e)
|
||||
Debouncer(1000).debounce {
|
||||
viewModelScope.launch() {
|
||||
try {
|
||||
block()
|
||||
} catch (e: Exception) {
|
||||
// 错误处理可被子类重写
|
||||
handleError(e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun parseResponse(response: ApiResponse<*>): Boolean {
|
||||
val code = response.code
|
||||
if (code == "00000" || code == "200" || code == "0") {
|
||||
//|| code == "200" || code == "0"
|
||||
if (code == "00000" ) {
|
||||
return true
|
||||
}
|
||||
val message = response.msg
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.sw.dualscreen.model.response.UserFaceModel
|
||||
import com.sw.dualscreen.model.response.UserFaceModel2
|
||||
import com.sw.dualscreen.model.response.UserNutrition
|
||||
import com.sw.dualscreen.objbox.CollectedFoodInfo
|
||||
import com.sw.dualscreen.utils.Debouncer
|
||||
import com.sw.dualscreen.utils.SPUtil
|
||||
import com.sw.dualscreen.utils.SpTool
|
||||
import com.sw.plate.App
|
||||
|
||||
Reference in New Issue
Block a user