refactor(net): 优化API请求错误处理和基础组件功能
- 添加JsonSyntaxException异常处理,提供更友好的错误提示 - 修复API响应判断逻辑,移除不必要的空值检查 - 优化HttpException转换为ApiException时的数据解析 - 移除BaseActivity中废弃的launch方法和相关变量 - 重构BaseActivity中的权限请求和页面跳转回调机制 - 更新Loading对话框的显示方法,支持自定义消息和内容更新 - 修复CollectFragment中的语法错误和日志记录功能 - 优化DishSamplingActivity中的页面跳转实现方式 - 完善SettingActivity中的相机权限检查逻辑
This commit is contained in:
@@ -238,8 +238,8 @@ class DishSamplingActivity : BaseActivity() {
|
||||
}
|
||||
val food = FoodRecord()
|
||||
binding.etInputDish.let {
|
||||
if (it.tag!=null && it.tag.toString() == it.text.toString()) {
|
||||
food.foodId = foodId
|
||||
if (it.tag != null && it.tag.toString() == it.text.toString()) {
|
||||
food.foodId = foodId
|
||||
}
|
||||
}
|
||||
food.foodName = showFoodName
|
||||
@@ -259,17 +259,16 @@ class DishSamplingActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun jumpSearch(v: View) {
|
||||
launch(Intent(this, FoodSearchActivity::class.java).apply {
|
||||
val intent = Intent(this, FoodSearchActivity::class.java).apply {
|
||||
putExtra(FoodSearchActivity.FOOD_NAME, binding.etInputDish.text.toString().trim())
|
||||
}) { intent ->
|
||||
intent?.let {
|
||||
foodId = it.getStringExtra(FoodSearchActivity.FOOD_ID)
|
||||
foodName = it.getStringExtra(FoodSearchActivity.FOOD_NAME)
|
||||
binding.etInputDish.run {
|
||||
setText(foodName)
|
||||
tag = foodName
|
||||
setSelection(text.length)
|
||||
}
|
||||
}
|
||||
startActivity(intent) {
|
||||
foodId = it?.getStringExtra(FoodSearchActivity.FOOD_ID)
|
||||
foodName = it?.getStringExtra(FoodSearchActivity.FOOD_NAME)
|
||||
binding.etInputDish.run {
|
||||
setText(foodName)
|
||||
tag = foodName
|
||||
setSelection(text.length)
|
||||
}
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.shuwei.dish.match.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.fragment.app.Fragment
|
||||
@@ -23,7 +23,6 @@ import com.shuwei.dish.match.utils.ext.dp
|
||||
import com.shuwei.dish.match.utils.ext.gone
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import com.shuwei.dish.match.utils.ext.visible
|
||||
import kotlin.text.replace
|
||||
|
||||
/**
|
||||
* 设置Activity
|
||||
@@ -41,18 +40,6 @@ class SettingActivity : BaseActivity() {
|
||||
}
|
||||
private lateinit var previewView: PreviewView
|
||||
|
||||
// 权限申请
|
||||
private val requestPermissionLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
||||
if (isGranted) {
|
||||
// 权限已授予,显示 CollectFragment
|
||||
showFragment(fragmentList[1])
|
||||
} else {
|
||||
// 权限被拒绝
|
||||
toast("暂无相机权限,无法使用菜品采集功能")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivitySettingBinding.inflate(layoutInflater)
|
||||
@@ -138,22 +125,31 @@ class SettingActivity : BaseActivity() {
|
||||
/**
|
||||
* 检查相机权限并显示 CollectFragment
|
||||
*/
|
||||
@SuppressLint("ObsoleteSdkInt")
|
||||
private fun checkCameraPermissionAndShowFragment() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
Manifest.permission.CAMERA
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
// 权限已授予,直接显示 Fragment
|
||||
showFragment(fragmentList[1])
|
||||
} else {
|
||||
// 权限未授予,申请权限
|
||||
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
// Android 6.0 以下,直接显示 Fragment
|
||||
showFragment(fragmentList[1])
|
||||
return
|
||||
}
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
Manifest.permission.CAMERA
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
// 权限已授予,直接显示 Fragment
|
||||
showFragment(fragmentList[1])
|
||||
} else {
|
||||
// 权限未授予,申请权限
|
||||
requestPermission(Manifest.permission.CAMERA) { isGranted ->
|
||||
if (isGranted) {
|
||||
// 权限已授予,显示 CollectFragment
|
||||
showFragment(fragmentList[1])
|
||||
} else {
|
||||
// 权限被拒绝
|
||||
toast("暂无相机权限,无法使用菜品采集功能")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ package com.shuwei.dish.match.ui.fragment
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@@ -15,19 +15,16 @@ import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.adapter.FoodCollectionAdapter
|
||||
import com.shuwei.dish.match.base.BaseFragment
|
||||
import com.shuwei.dish.match.databinding.FragmentCollectBinding
|
||||
import com.shuwei.dish.match.dialog.Loading
|
||||
import com.shuwei.dish.match.objbox.FoodCollectionBean
|
||||
import com.shuwei.dish.match.objbox.FoodModule
|
||||
import com.shuwei.dish.match.objbox.ObjectBox
|
||||
import com.shuwei.dish.match.ui.SettingActivity
|
||||
import com.shuwei.dish.match.utils.BitmapSaver
|
||||
import com.shuwei.dish.match.utils.Debouncer
|
||||
import com.shuwei.dish.match.utils.ImageUtil
|
||||
import com.shuwei.dish.match.utils.LogSaveUtil
|
||||
import com.shuwei.dish.match.utils.ext.clickWithDebounce
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
class CollectFragment : BaseFragment<FragmentCollectBinding>() {
|
||||
@@ -46,7 +43,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
// private val searchFoodList = mutableListOf<FoodInfo>()
|
||||
// private val searchFoodList = mutableListOf<FoodInfo>()
|
||||
// private var settingActivity: SettingActivity? = null
|
||||
// private var checkedItem: FoodInfo? = null
|
||||
// private val searchFoodAdapter by lazy {
|
||||
@@ -96,7 +93,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>() {
|
||||
if (index == -1) {
|
||||
toast("每次只允许保存${MAX_COUNT}条数据")
|
||||
hideWaitingDialog()
|
||||
rerurn@cameraCallback
|
||||
rerurn@ cameraCallback
|
||||
}
|
||||
activity?.runOnUiThread {
|
||||
foodCollectionList[index].let {
|
||||
@@ -268,7 +265,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>() {
|
||||
// //}
|
||||
// runBlocking {
|
||||
// activity?.runOnUiThread {
|
||||
// showWaitingDialog2("图片上传中$count/$totalFileCount")
|
||||
// showWaitingDialog("图片上传中$count/$totalFileCount")
|
||||
// }
|
||||
// val foodList = batch.mapIndexed { index, it ->
|
||||
// Food(
|
||||
@@ -351,9 +348,21 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>() {
|
||||
binding.llCameraFlag.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
private fun log(msg: String) {}
|
||||
private fun searchByFoodName(name: String, block:(String)-> Unit) {}
|
||||
private fun hideWaitingDialog() {}
|
||||
private fun showWaitingDialog(msg: String) {}
|
||||
private fun showWaitingDialog2(msg: String) {}
|
||||
private fun log(msg: String) {
|
||||
Log.d(TAG, "log: $msg")
|
||||
LogSaveUtil.saveLogFile(msg)
|
||||
}
|
||||
|
||||
private fun searchByFoodName(name: String, block: (String) -> Unit) {}
|
||||
private fun hideWaitingDialog() {
|
||||
settingActivity.dismissLoading()
|
||||
}
|
||||
|
||||
private fun showWaitingDialog(msg: String) {
|
||||
if (Loading.isShowing()) {
|
||||
Loading.updateContent(msg)
|
||||
return
|
||||
}
|
||||
settingActivity.showLoading(msg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user