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

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 package com.navinfo.omqs.http.taskdownload
import android.content.Context import android.content.Context
import android.util.Log
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import com.navinfo.collect.library.map.NIMapController import com.navinfo.collect.library.map.NIMapController
@ -41,6 +42,7 @@ class TaskDownloadManager constructor(
ConcurrentHashMap<Int, TaskDownloadScope>() ConcurrentHashMap<Int, TaskDownloadScope>()
} }
fun init(context: Context) { fun init(context: Context) {
this.context = context this.context = context
} }
@ -102,6 +104,7 @@ class TaskDownloadManager constructor(
fun addTask(taskBean: TaskBean) { fun addTask(taskBean: TaskBean) {
Log.e("jingo", "下载线程 ${taskBean.id}")
if (!scopeMap.containsKey(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( fun observer(
id: Int, lifecycleOwner: LifecycleOwner, observer: Observer<TaskBean> id: Int, lifecycleOwner: LifecycleOwner, observer: Observer<TaskBean>
) { ) {
Log.e("jingo", "监听线程 ${id}")
if (scopeMap.containsKey(id)) { if (scopeMap.containsKey(id)) {
scopeMap[id]!!.observer(lifecycleOwner, observer) scopeMap[id]!!.observer(lifecycleOwner, observer)
} }

View File

@ -1,14 +1,16 @@
package com.navinfo.omqs.http.taskdownload package com.navinfo.omqs.http.taskdownload
import android.util.Log import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import com.navinfo.omqs.Constant
import com.navinfo.collect.library.data.entity.TaskBean import com.navinfo.collect.library.data.entity.TaskBean
import com.navinfo.omqs.Constant
import com.navinfo.omqs.db.ImportOMDBHelper import com.navinfo.omqs.db.ImportOMDBHelper
import com.navinfo.omqs.tools.FileManager import com.navinfo.omqs.tools.FileManager
import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus
import com.navinfo.omqs.ui.other.BaseViewHolder
import com.navinfo.omqs.util.DateTimeUtil import com.navinfo.omqs.util.DateTimeUtil
import io.realm.Realm import io.realm.Realm
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -21,7 +23,7 @@ class TaskDownloadScope(
private val downloadManager: TaskDownloadManager, private val downloadManager: TaskDownloadManager,
val taskBean: TaskBean, 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 val observer = Observer<Any> {}
// private var lifecycleOwner: LifecycleOwner? = null private var lifecycleOwner: LifecycleOwner? = null
/** /**
*通知UI更新 *通知UI更新
@ -92,6 +94,7 @@ class TaskDownloadScope(
* @param status [OfflineMapCityBean.Status] * @param status [OfflineMapCityBean.Status]
*/ */
private suspend fun change(status: Int, message: String = "") { private suspend fun change(status: Int, message: String = "") {
if (taskBean.status != status || status == FileDownloadStatus.LOADING || status == FileDownloadStatus.IMPORTING) { if (taskBean.status != status || status == FileDownloadStatus.LOADING || status == FileDownloadStatus.IMPORTING) {
taskBean.status = status taskBean.status = status
taskBean.message = message taskBean.message = message
@ -111,8 +114,9 @@ class TaskDownloadScope(
* 添加下载任务观察者 * 添加下载任务观察者
*/ */
fun observer(owner: LifecycleOwner, ob: Observer<TaskBean>) { fun observer(owner: LifecycleOwner, ob: Observer<TaskBean>) {
removeObserver() removeObserver()
// this.lifecycleOwner = owner this.lifecycleOwner = owner
downloadData.observe(owner, ob) downloadData.observe(owner, ob)
} }
@ -134,9 +138,7 @@ class TaskDownloadScope(
Log.e("jingo", "数据安装 $it") Log.e("jingo", "数据安装 $it")
if (it == "finish") { if (it == "finish") {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
downloadManager.mapController.layerManagerHandler.omdbLayersUpdate()
downloadManager.mapController.mMapView.updateMap(true) downloadManager.mapController.mMapView.updateMap(true)
Log.e("jingo", "数据安装结束,刷新地图")
} }
change(FileDownloadStatus.DONE) change(FileDownloadStatus.DONE)
@ -235,10 +237,17 @@ class TaskDownloadScope(
} }
fun removeObserver() { fun removeObserver() {
downloadData.observeForever(observer) // downloadData.observeForever(observer)
// lifecycleOwner?.let { //// lifecycleOwner?.let {
downloadData.removeObserver(observer) // downloadData.removeObserver(observer)
// null //// 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 { interface TaskAdapterCallback {
fun itemOnClick(bean: HadLinkDvoBean) 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.isSelected = bean.reason != ""
binding.taskEdit.setOnClickListener { 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) showLinkEditDialog(position, bean)
} }
}) })

View File

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

View File

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

View File

@ -16,6 +16,8 @@ open class BaseViewHolder(val viewBinding: ViewBinding) :
private val lifecycleRegistry = LifecycleRegistry(this) private val lifecycleRegistry = LifecycleRegistry(this)
var tag = "" var tag = ""
private var listener: OnLifecycleStateListener? = null
init { init {
// dataBinding.lifecycleOwner = this // dataBinding.lifecycleOwner = this
lifecycleRegistry.currentState = Lifecycle.State.INITIALIZED lifecycleRegistry.currentState = Lifecycle.State.INITIALIZED
@ -37,19 +39,33 @@ open class BaseViewHolder(val viewBinding: ViewBinding) :
fun onStart() { fun onStart() {
lifecycleRegistry.currentState = Lifecycle.State.STARTED // lifecycleRegistry.currentState = Lifecycle.State.STARTED //
lifecycleRegistry.currentState = Lifecycle.State.RESUMED // ON_RESUME EVENT lifecycleRegistry.currentState = Lifecycle.State.RESUMED // ON_RESUME EVENT
listener?.onState(tag,Lifecycle.State.STARTED)
} }
fun onStop() { fun onStop() {
lifecycleRegistry.currentState = Lifecycle.State.STARTED // 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() { fun onDestroy() {
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED /// ON_DESTROY EVENT lifecycleRegistry.currentState = Lifecycle.State.DESTROYED /// ON_DESTROY EVENT
listener?.onState(tag,Lifecycle.State.DESTROYED)
} }
override fun getLifecycle(): Lifecycle { override fun getLifecycle(): Lifecycle {
return lifecycleRegistry 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_constraintRight_toRightOf="@id/task_list_search"
app:layout_constraintTop_toTopOf="@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:id="@+id/task_list_recyclerview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"