修改任务下载按钮刷新不及时问题

This commit is contained in:
squallzhjch 2023-07-26 10:49:23 +08:00
parent 7a268f43e0
commit e69b6fca03
8 changed files with 75 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package com.navinfo.omqs.http.taskdownload
import android.content.Context
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.navinfo.collect.library.map.NIMapController
@ -17,7 +18,7 @@ import java.util.concurrent.ConcurrentHashMap
class TaskDownloadManager constructor(
val importFactory: ImportOMDBHiltFactory,
val netApi: RetrofitNetworkServiceAPI,
val mapController:NIMapController
val mapController: NIMapController
) {
lateinit var context: Context
@ -41,6 +42,7 @@ class TaskDownloadManager constructor(
ConcurrentHashMap<Int, TaskDownloadScope>()
}
fun init(context: Context) {
this.context = context
}
@ -102,8 +104,9 @@ class TaskDownloadManager constructor(
fun addTask(taskBean: TaskBean) {
Log.e("jingo", "下载线程 ${taskBean.id}")
if (!scopeMap.containsKey(taskBean.id)) {
scopeMap[taskBean.id] = TaskDownloadScope( this, taskBean)
scopeMap[taskBean.id] = TaskDownloadScope(this, taskBean)
}
}
@ -111,6 +114,7 @@ class TaskDownloadManager constructor(
fun observer(
id: Int, lifecycleOwner: LifecycleOwner, observer: Observer<TaskBean>
) {
Log.e("jingo", "监听线程 ${id}")
if (scopeMap.containsKey(id)) {
scopeMap[id]!!.observer(lifecycleOwner, observer)
}

View File

@ -1,14 +1,16 @@
package com.navinfo.omqs.http.taskdownload
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.navinfo.omqs.Constant
import com.navinfo.collect.library.data.entity.TaskBean
import com.navinfo.omqs.Constant
import com.navinfo.omqs.db.ImportOMDBHelper
import com.navinfo.omqs.tools.FileManager
import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus
import com.navinfo.omqs.ui.other.BaseViewHolder
import com.navinfo.omqs.util.DateTimeUtil
import io.realm.Realm
import kotlinx.coroutines.*
@ -21,7 +23,7 @@ class TaskDownloadScope(
private val downloadManager: TaskDownloadManager,
val taskBean: TaskBean,
) :
CoroutineScope by CoroutineScope(Dispatchers.IO + CoroutineName("OfflineMapDownLoad")) {
CoroutineScope by CoroutineScope(Dispatchers.IO + CoroutineName("TaskMapDownLoad")) {
/**
*下载任务用来取消的
@ -31,8 +33,8 @@ class TaskDownloadScope(
/**
* 管理观察者同时只有一个就行了
*/
private val observer = Observer<Any> {}
// private var lifecycleOwner: LifecycleOwner? = null
// private val observer = Observer<Any> {}
private var lifecycleOwner: LifecycleOwner? = null
/**
*通知UI更新
@ -92,6 +94,7 @@ class TaskDownloadScope(
* @param status [OfflineMapCityBean.Status]
*/
private suspend fun change(status: Int, message: String = "") {
if (taskBean.status != status || status == FileDownloadStatus.LOADING || status == FileDownloadStatus.IMPORTING) {
taskBean.status = status
taskBean.message = message
@ -111,8 +114,9 @@ class TaskDownloadScope(
* 添加下载任务观察者
*/
fun observer(owner: LifecycleOwner, ob: Observer<TaskBean>) {
removeObserver()
// this.lifecycleOwner = owner
this.lifecycleOwner = owner
downloadData.observe(owner, ob)
}
@ -134,9 +138,7 @@ class TaskDownloadScope(
Log.e("jingo", "数据安装 $it")
if (it == "finish") {
withContext(Dispatchers.Main) {
downloadManager.mapController.layerManagerHandler.omdbLayersUpdate()
downloadManager.mapController.mMapView.updateMap(true)
Log.e("jingo", "数据安装结束,刷新地图")
}
change(FileDownloadStatus.DONE)
@ -235,10 +237,17 @@ class TaskDownloadScope(
}
fun removeObserver() {
downloadData.observeForever(observer)
// lifecycleOwner?.let {
downloadData.removeObserver(observer)
// null
// }
// downloadData.observeForever(observer)
//// lifecycleOwner?.let {
// downloadData.removeObserver(observer)
//// null
//// }
if (lifecycleOwner != null) {
Log.e(
"jingo",
"移除的上一个监听者 ${lifecycleOwner.hashCode()} ${(lifecycleOwner as BaseViewHolder).tag}"
)
downloadData.removeObservers(lifecycleOwner!!)
}
}
}

View File

@ -11,7 +11,7 @@ import com.navinfo.omqs.ui.other.BaseViewHolder
interface TaskAdapterCallback {
fun itemOnClick(bean: HadLinkDvoBean)
fun editOnclick(position: Int, bean: HadLinkDvoBean)
fun editOnClick(position: Int, bean: HadLinkDvoBean)
}
/**
@ -54,7 +54,7 @@ class TaskAdapter(
}
binding.taskEdit.isSelected = bean.reason != ""
binding.taskEdit.setOnClickListener {
callback.editOnclick(position, bean)
callback.editOnClick(position, bean)
}
}

View File

@ -45,7 +45,7 @@ class TaskFragment : BaseFragment() {
}
}
override fun editOnclick(position: Int, bean: HadLinkDvoBean) {
override fun editOnClick(position: Int, bean: HadLinkDvoBean) {
showLinkEditDialog(position, bean)
}
})

View File

@ -7,6 +7,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.navinfo.collect.library.data.entity.TaskBean
@ -19,6 +23,7 @@ import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus
import com.navinfo.omqs.tools.FileManager.Companion.FileUploadStatus
import com.navinfo.omqs.ui.other.BaseRecyclerViewAdapter
import com.navinfo.omqs.ui.other.BaseViewHolder
import com.navinfo.omqs.ui.other.OnLifecycleStateListener
import com.navinfo.omqs.ui.widget.LeftDeleteView
/**
@ -115,6 +120,7 @@ class TaskListAdapter(
override fun onViewRecycled(holder: BaseViewHolder) {
super.onViewRecycled(holder)
//页面滑动时会用holder重构页面但是对进度条的监听回调会一直返回扰乱UI所以当当前holder去重构的时候移除监听
//这里 BaseViewHolder 的LifecycleOwner 状态很早就DESTROYED 了,这个回调比较晚,起到的作用很小
downloadManager.removeObserver(holder.tag.toInt())
}
@ -122,7 +128,7 @@ class TaskListAdapter(
holder: BaseViewHolder,
@SuppressLint("RecyclerView") position: Int
) {
Log.e("jingo", "TaskListAdapter onBindViewHolder $position ")
val binding: AdapterTaskListBinding =
holder.viewBinding as AdapterTaskListBinding
val taskBean = data[position]
@ -141,8 +147,22 @@ class TaskListAdapter(
//tag 方便onclick里拿到数据
holder.tag = taskBean.id.toString()
changeViews(binding, taskBean)
holder.addObserver(object : OnLifecycleStateListener {
override fun onState(tag: String, state: Lifecycle.State) {
when (state) {
Lifecycle.State.STARTED ->
downloadManager.observer(
taskBean.id,
holder,
DownloadObserver(taskBean.id, holder)
)
Lifecycle.State.DESTROYED ->
downloadManager.removeObserver(tag.toInt())
else -> {}
}
}
})
downloadManager.addTask(taskBean)
downloadManager.observer(taskBean.id, holder, DownloadObserver(taskBean.id, holder))
uploadManager.addTask(taskBean)
uploadManager.observer(taskBean.id, holder, UploadObserver(taskBean.id, binding))
if (taskBean.status == FileDownloadStatus.NONE) {
@ -195,9 +215,10 @@ class TaskListAdapter(
binding.taskDeleteLayout.setOnClickListener {
//重置状态
leftDeleteView?.resetDeleteStatus()
if(taskBean.syncStatus != FileUploadStatus.DONE){
Toast.makeText(binding.taskUploadBtn.context, "数据未上传,不允许关闭!", Toast.LENGTH_SHORT).show()
}else{
if (taskBean.syncStatus != FileUploadStatus.DONE) {
Toast.makeText(binding.taskUploadBtn.context, "数据未上传,不允许关闭!", Toast.LENGTH_SHORT)
.show()
} else {
itemListener?.invoke(position, ItemClickStatus.DELETE_LAYOUT_CLICK, taskBean)
}
}

View File

@ -160,7 +160,7 @@ class TaskViewModel @Inject constructor(
val objects =
realm.where(TaskBean::class.java).notEqualTo("syncStatus", syncUpload).or()
.between("operationTime", beginNowTime, nowTime)
.equalTo("syncStatus", syncUpload).findAll()
.equalTo("syncStatus", syncUpload).findAll().sort("id")
val taskList = realm.copyFromRealm(objects)
for (item in taskList) {
FileManager.checkOMDBFileInfo(item)

View File

@ -16,6 +16,8 @@ open class BaseViewHolder(val viewBinding: ViewBinding) :
private val lifecycleRegistry = LifecycleRegistry(this)
var tag = ""
private var listener: OnLifecycleStateListener? = null
init {
// dataBinding.lifecycleOwner = this
lifecycleRegistry.currentState = Lifecycle.State.INITIALIZED
@ -37,19 +39,33 @@ open class BaseViewHolder(val viewBinding: ViewBinding) :
fun onStart() {
lifecycleRegistry.currentState = Lifecycle.State.STARTED //
lifecycleRegistry.currentState = Lifecycle.State.RESUMED // ON_RESUME EVENT
listener?.onState(tag,Lifecycle.State.STARTED)
}
fun onStop() {
lifecycleRegistry.currentState = Lifecycle.State.STARTED //
lifecycleRegistry.currentState = Lifecycle.State.CREATED // ON_STOP EVENT
lifecycleRegistry.currentState = Lifecycle.State.CREATED //
// listener?.onState(tag,Lifecycle.State.STARTED)// ON_STOP EVENT
}
fun onDestroy() {
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED /// ON_DESTROY EVENT
listener?.onState(tag,Lifecycle.State.DESTROYED)
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
fun addObserver(listener: OnLifecycleStateListener) {
this.listener = listener
}
}
/**
* 生命周期变化
*/
interface OnLifecycleStateListener {
fun onState(tag:String,state: Lifecycle.State)
}

View File

@ -41,7 +41,7 @@
app:layout_constraintRight_toRightOf="@id/task_list_search"
app:layout_constraintTop_toTopOf="@id/task_list_search" />
<androidx.recyclerview.widget.RecyclerView
<com.yanzhenjie.recyclerview.SwipeRecyclerView
android:id="@+id/task_list_recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"