打开增加检查网络情况
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
package com.sw.dualscreen.activity
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.CountDownTimer
|
||||
import androidx.activity.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.sw.dualscreen.GlobalData
|
||||
import com.sw.dualscreen.databinding.ActivityInitBinding
|
||||
import com.sw.dualscreen.dialog.RemindDialog
|
||||
import com.sw.dualscreen.ext.clickWithDebounce
|
||||
import com.sw.dualscreen.ext.visible
|
||||
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.L
|
||||
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||
import com.sw.plate.utils.AppUtil
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
@@ -37,7 +43,11 @@ class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
"version" to GlobalData.foodModelVersion
|
||||
)
|
||||
) { items ->
|
||||
val list = items ?: emptyList()
|
||||
if (items == null) {
|
||||
getVectorErrorDialog()
|
||||
return@getCollectedFoodVector
|
||||
}
|
||||
val list = items
|
||||
if (pageNum == 1 && list.isEmpty()) {
|
||||
block()
|
||||
return@getCollectedFoodVector
|
||||
@@ -75,20 +85,13 @@ class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
|
||||
override fun initialize() {
|
||||
super.initialize()
|
||||
showWaitingDialog("加载中……")
|
||||
lifecycleScope.launch {
|
||||
FoodModule.init(this@InitActivity) {
|
||||
getCollectedFoodVector {
|
||||
runOnUiThread {
|
||||
hideWaitingDialog()
|
||||
binding.root.postDelayed({
|
||||
goMainActivity()
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
initNetworkTimer()
|
||||
binding.okButton.clickWithDebounce(500) {
|
||||
checkNetworkTimer?.cancel()
|
||||
checkNetworkTimer?.start()
|
||||
}
|
||||
|
||||
showWaitingDialog("网络连接中...")
|
||||
checkNetworkTimer?.start()
|
||||
|
||||
// val isSuccess = viewModel.checkEquipmentInfo()
|
||||
// if (isSuccess) {
|
||||
@@ -116,9 +119,69 @@ class InitActivity : BaseActivity<ActivityInitBinding>() {
|
||||
// }
|
||||
}
|
||||
|
||||
private fun getCollectedFoodVector() {
|
||||
showWaitingDialog2("加载中……")
|
||||
lifecycleScope.launch {
|
||||
FoodModule.init(this@InitActivity) {
|
||||
getCollectedFoodVector {
|
||||
runOnUiThread {
|
||||
hideWaitingDialog()
|
||||
binding.root.postDelayed({
|
||||
goMainActivity()
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun goMainActivity() {
|
||||
val intent = Intent(this, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
private var checkNetworkTimer: CountDownTimer? = null
|
||||
|
||||
private fun initNetworkTimer() {
|
||||
checkNetworkTimer = object : CountDownTimer(5 * 300, 300) {
|
||||
override fun onTick(millisUntilFinished: Long) {
|
||||
|
||||
}
|
||||
|
||||
override fun onFinish() {
|
||||
L.e("检测网络连接状态" + AppUtil.isNetworkConnected(this@InitActivity))
|
||||
if (!AppUtil.isNetworkConnected(this@InitActivity)) {
|
||||
hideWaitingDialog()
|
||||
binding.okButton.visible()
|
||||
loadNetworkErrorDialog()
|
||||
return
|
||||
}
|
||||
getCollectedFoodVector()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadNetworkErrorDialog() {
|
||||
RemindDialog(
|
||||
context = context,
|
||||
content = "网络连接异常,请检查WiFi连接状态后重试",
|
||||
confirmBlock = {
|
||||
startActivity(Intent("android.settings.WIFI_SETTINGS"))
|
||||
}).show()
|
||||
}
|
||||
|
||||
private fun getVectorErrorDialog() {
|
||||
RemindDialog(
|
||||
context = context,
|
||||
content = "未查询到向量数据,请稍后重试?",
|
||||
confirmBlock = {
|
||||
getCollectedFoodVector()
|
||||
}).show()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
checkNetworkTimer?.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@@ -916,8 +916,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
// private val initialDelay = 5 * 60 * 1000L
|
||||
// private val dealyMillis = 10 * 60 * 1000L
|
||||
private val initialDelay = 30 * 1000L
|
||||
private val dealyMillis = 60 * 1000L
|
||||
private var taskPageNo = 1
|
||||
private val dealyMillis = 3 * 60 * 1000L
|
||||
// private var taskPageNo = 1
|
||||
fun startFaceTask() {
|
||||
faceTaskJob =
|
||||
intervalExecutor.startIntervalTaskWithInitialDelay(initialDelay, dealyMillis) {
|
||||
@@ -926,10 +926,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
return@startIntervalTaskWithInitialDelay
|
||||
}
|
||||
viewModel.getFaceIncrementList(
|
||||
pageNo = taskPageNo,
|
||||
// pageNo = taskPageNo,
|
||||
timestamp = timestamp,
|
||||
onAllQueryFinished = {
|
||||
taskPageNo = 1
|
||||
// taskPageNo = 1
|
||||
},
|
||||
onPageQueryFinished = { list ->
|
||||
runOnUiThread {
|
||||
|
||||
@@ -116,13 +116,34 @@ fun EditText.addOnActionSearchListener(searchCallback: () -> Unit) {
|
||||
return@setOnEditorActionListener false
|
||||
}
|
||||
}
|
||||
fun View.clickWithDebounce(delay: Long = 500, action: () -> Unit) {
|
||||
var job: Job? = null
|
||||
//fun View.clickWithDebounce(delay: Long = 500, action: () -> Unit) {
|
||||
// var job: Job? = null
|
||||
// setOnClickListener {
|
||||
// job?.cancel()
|
||||
// job = CoroutineScope(Dispatchers.Main).launch {
|
||||
// delay(delay)
|
||||
// action()
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
/**
|
||||
* 极简版防重复点击扩展函数
|
||||
* @param delay 防抖时间(默认300ms)
|
||||
* @param action 点击执行逻辑
|
||||
*/
|
||||
fun View.clickWithDebounce(delay: Long = 300, action: () -> Unit) {
|
||||
setOnClickListener {
|
||||
job?.cancel()
|
||||
job = CoroutineScope(Dispatchers.Main).launch {
|
||||
delay(delay)
|
||||
action()
|
||||
// 用View的tag存储是否可点击的状态(默认可点击)
|
||||
if (tag as? Boolean ?: true) {
|
||||
tag = false // 标记为不可点击
|
||||
action() // 立即执行点击逻辑
|
||||
|
||||
// 启动协程,延迟后恢复可点击状态
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
delay(delay)
|
||||
tag = true // 恢复可点击
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,17 +185,17 @@ class UserViewModel : BaseViewModel() {
|
||||
return@withContext
|
||||
}
|
||||
onPageQueryFinished(list)
|
||||
if (list.size >= pageSize) {
|
||||
lastFaceTimestamp = list.last().faceUpdateTimestamp ?: 0L
|
||||
getFaceIncrementList(
|
||||
pageNo = pageNo + 1,
|
||||
pageSize = pageSize,
|
||||
timestamp = timestamp,
|
||||
onAllQueryFinished = onAllQueryFinished,
|
||||
onPageQueryFinished = onPageQueryFinished
|
||||
)
|
||||
return@withContext
|
||||
}
|
||||
// if (list.size >= pageSize) {
|
||||
// lastFaceTimestamp = list.last().faceUpdateTimestamp ?: 0L
|
||||
// getFaceIncrementList(
|
||||
// pageNo = pageNo + 1,
|
||||
// pageSize = pageSize,
|
||||
// timestamp = timestamp,
|
||||
// onAllQueryFinished = onAllQueryFinished,
|
||||
// onPageQueryFinished = onPageQueryFinished
|
||||
// )
|
||||
// return@withContext
|
||||
// }
|
||||
//查询完成所有数据,保存时间戳
|
||||
if (list.isNotEmpty()) {
|
||||
//非空实现最后一条数据的是时间戳,空使用上次list最后一条时间戳
|
||||
@@ -576,7 +576,7 @@ class UserViewModel : BaseViewModel() {
|
||||
launchWithLoading {
|
||||
val response = repository.getCollectedFoodVector(param)
|
||||
if (parseResponse(response)) {
|
||||
block(response.data)
|
||||
block(response.data?:emptyList())
|
||||
} else {
|
||||
block(null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user