refactor(net): 优化API请求错误处理和基础组件功能
- 添加JsonSyntaxException异常处理,提供更友好的错误提示 - 修复API响应判断逻辑,移除不必要的空值检查 - 优化HttpException转换为ApiException时的数据解析 - 移除BaseActivity中废弃的launch方法和相关变量 - 重构BaseActivity中的权限请求和页面跳转回调机制 - 更新Loading对话框的显示方法,支持自定义消息和内容更新 - 修复CollectFragment中的语法错误和日志记录功能 - 优化DishSamplingActivity中的页面跳转实现方式 - 完善SettingActivity中的相机权限检查逻辑
This commit is contained in:
@@ -35,18 +35,8 @@ import kotlin.getValue
|
||||
open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityBaseBinding
|
||||
private var launcher: ActivityResultLauncher<Intent>? = null
|
||||
private var launchCallback: ((Intent?) -> Unit)? = null
|
||||
|
||||
public fun launch(cls: Class<*>, launchCallback: (Intent?) -> Unit) {
|
||||
this.launchCallback = launchCallback
|
||||
launcher?.launch(Intent(this, cls))
|
||||
}
|
||||
|
||||
public fun launch(intent: Intent, launchCallback: (Intent?) -> Unit) {
|
||||
this.launchCallback = launchCallback
|
||||
launcher?.launch(intent)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -57,12 +47,6 @@ open class BaseActivity : AppCompatActivity() {
|
||||
}
|
||||
setContentView(binding.root)
|
||||
statusBarDarkFont(enable = false)
|
||||
launcher =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
launchCallback?.invoke(result.data)
|
||||
}
|
||||
}
|
||||
|
||||
// window.setDecorFitsSystemWindows(false) // 启用 Edge-to-Edge
|
||||
// window.insetsController?.apply {
|
||||
@@ -95,8 +79,7 @@ open class BaseActivity : AppCompatActivity() {
|
||||
fun statusBarDarkFont(enable: Boolean) {
|
||||
//window.decorView.systemUiVisibility = if (enable) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
|
||||
val color = ContextCompat.getColor(
|
||||
this,
|
||||
if (enable) R.color.black else R.color.white
|
||||
this, if (enable) R.color.black else R.color.white
|
||||
)
|
||||
binding.tvLeftTime.setTextColor(color)
|
||||
binding.tvRightTime.setTextColor(color)
|
||||
@@ -161,8 +144,11 @@ open class BaseActivity : AppCompatActivity() {
|
||||
handler.removeCallbacks(updateTask)
|
||||
}
|
||||
|
||||
fun showLoading() {
|
||||
Loading.show(this)
|
||||
fun showLoading(msg: String = "加载中……") {
|
||||
Loading.show(
|
||||
context = this,
|
||||
message = msg
|
||||
)
|
||||
}
|
||||
|
||||
fun dismissLoading() {
|
||||
@@ -193,4 +179,52 @@ open class BaseActivity : AppCompatActivity() {
|
||||
netViewModel.querySeasoningList(param, onSuccess, onFailure)
|
||||
}
|
||||
|
||||
|
||||
private var permissionCallback: ((isGranted: Boolean) -> Unit)? = null
|
||||
private var activityCallback: ((intent: Intent?) -> Unit)? = null
|
||||
|
||||
fun requestMultiplePermissions(
|
||||
permissions: Array<String>,
|
||||
callback: (isGranted: Boolean) -> Unit
|
||||
) {
|
||||
this.permissionCallback = callback
|
||||
requestMultiplePermissionsLauncher.launch(permissions)
|
||||
}
|
||||
|
||||
fun requestPermission(permission: String, callback: (isGranted: Boolean) -> Unit) {
|
||||
this.permissionCallback = callback
|
||||
requestPermissionLauncher.launch(permission)
|
||||
}
|
||||
|
||||
val requestMultiplePermissionsLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.RequestMultiplePermissions()
|
||||
) { permissions ->
|
||||
var isGranted = true
|
||||
permissions.entries.forEach {
|
||||
if (!it.value) {
|
||||
isGranted = false
|
||||
}
|
||||
}
|
||||
permissionCallback?.invoke(isGranted)
|
||||
}
|
||||
|
||||
// 权限请求回调
|
||||
val requestPermissionLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.RequestPermission()
|
||||
) { isGranted ->
|
||||
permissionCallback?.invoke(isGranted)
|
||||
}
|
||||
|
||||
fun startActivity(intent: Intent, callback: (Intent?) -> Unit) {
|
||||
this.activityCallback = callback
|
||||
startActivityLauncher.launch(intent)
|
||||
}
|
||||
|
||||
// activity页面返回的回调
|
||||
private val startActivityLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) {
|
||||
activityCallback?.invoke(it.data)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user