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

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!!)
}
}
}