增加任务列表和下载功能
This commit is contained in:
@@ -61,10 +61,10 @@ class EvaluationResultFragment : BaseFragment(), View.OnClickListener {
|
||||
if (id != null) {
|
||||
viewModel.loadData(id)
|
||||
} else {
|
||||
viewModel.loadMetadata()
|
||||
viewModel.initNewData()
|
||||
}
|
||||
} else {
|
||||
viewModel.loadMetadata()
|
||||
viewModel.initNewData()
|
||||
}
|
||||
|
||||
// //监听大分类数据变化
|
||||
|
||||
@@ -4,9 +4,8 @@ import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.collect.library.map.GeoPoint
|
||||
import com.navinfo.collect.library.map.NIMapController
|
||||
import com.navinfo.collect.library.data.entity.QsRecordBean
|
||||
import com.navinfo.collect.library.map.NIMapController
|
||||
import com.navinfo.omqs.db.RoomAppDatabase
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import io.realm.Realm
|
||||
@@ -46,8 +45,10 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
|
||||
var liveDataQsRecordBean = MutableLiveData<QsRecordBean>()
|
||||
|
||||
var oldBean: QsRecordBean? = null
|
||||
|
||||
init {
|
||||
liveDataQsRecordBean.value = QsRecordBean(id = UUID.randomUUID().toString(), classType = "89193")
|
||||
liveDataQsRecordBean.value = QsRecordBean(id = UUID.randomUUID().toString())
|
||||
Log.e("jingo", "EvaluationResultViewModel 创建了 ${hashCode()}")
|
||||
mapController.markerHandle.apply {
|
||||
setOnMapClickListener {
|
||||
@@ -69,7 +70,7 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
/**
|
||||
* 查询数据库,获取问题分类
|
||||
*/
|
||||
fun loadMetadata() {
|
||||
fun initNewData() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
getClassTypeList()
|
||||
getProblemLinkList()
|
||||
@@ -88,7 +89,6 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
Log.e("jingo", "getClassTypeList S")
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val list = roomAppDatabase.getScProblemTypeDao().findClassTypeList()
|
||||
Log.e("jingo", "getClassTypeList ${list?.size}")
|
||||
list?.let {
|
||||
if (list.isNotEmpty()) {
|
||||
//通知页面更新
|
||||
@@ -110,6 +110,7 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
* 获取问题环节列表和初步问题
|
||||
*/
|
||||
fun getProblemLinkList() {
|
||||
Log.e("jingo", "getProblemLinkList S")
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val list = roomAppDatabase.getScRootCauseAnalysisDao().findAllData()
|
||||
list?.let { tl ->
|
||||
@@ -128,21 +129,26 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
}
|
||||
if (liveDataQsRecordBean.value!!.problemLink.isEmpty()) {
|
||||
liveDataQsRecordBean.value!!.problemLink = middleList[0]
|
||||
Log.e("jingo", "getProblemLinkList ${middleList[0]}")
|
||||
}
|
||||
if (liveDataQsRecordBean.value!!.cause.isEmpty()) {
|
||||
liveDataQsRecordBean.value!!.cause = rightList[0].text
|
||||
Log.e("jingo", "getProblemLinkList ${rightList[0].text}")
|
||||
}
|
||||
liveDataQsRecordBean.postValue(liveDataQsRecordBean.value)
|
||||
liveDataMiddleTypeList.postValue(middleList)
|
||||
liveDataRightTypeList.postValue(rightList)
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.e("jingo", "getProblemLinkList E")
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取问题类型列表和问题现象
|
||||
*/
|
||||
private suspend fun getProblemList(classType: String) {
|
||||
Log.e("jingo", "getProblemList S")
|
||||
val typeList = roomAppDatabase.getScProblemTypeDao().findProblemTypeList(classType)
|
||||
typeList?.let { tl ->
|
||||
if (tl.isNotEmpty()) {
|
||||
@@ -158,10 +164,20 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
)
|
||||
)
|
||||
}
|
||||
if (liveDataQsRecordBean.value!!.problemType.isEmpty()) {
|
||||
liveDataQsRecordBean.value!!.problemType = typeTitleList[0]
|
||||
Log.e("jingo", "getProblemList ${typeTitleList[0]}")
|
||||
}
|
||||
liveDataMiddleTypeList.postValue(typeTitleList)
|
||||
if (liveDataQsRecordBean.value!!.phenomenon.isEmpty()) {
|
||||
liveDataQsRecordBean.value!!.phenomenon = phenomenonRightList[0].text
|
||||
Log.e("jingo", "getProblemList ${phenomenonRightList[0].text}")
|
||||
}
|
||||
liveDataQsRecordBean.postValue(liveDataQsRecordBean.value)
|
||||
liveDataRightTypeList.postValue(phenomenonRightList)
|
||||
}
|
||||
}
|
||||
Log.e("jingo", "getProblemList E")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,45 +195,42 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
fun setPhenomenonMiddleBean(adapterBean: RightBean) {
|
||||
liveDataQsRecordBean.value!!.phenomenon = adapterBean.text
|
||||
liveDataQsRecordBean.value!!.problemType = adapterBean.title
|
||||
liveDataQsRecordBean.postValue(liveDataQsRecordBean.value)
|
||||
}
|
||||
|
||||
fun setProblemLinkMiddleBean(adapterBean: RightBean) {
|
||||
liveDataQsRecordBean.value!!.cause = adapterBean.text
|
||||
liveDataQsRecordBean.value!!.problemLink = adapterBean.title
|
||||
|
||||
liveDataQsRecordBean.postValue(liveDataQsRecordBean.value)
|
||||
}
|
||||
|
||||
fun saveData() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
Log.e("jingo","realm hashCOde ${realm.hashCode()}")
|
||||
realm.executeTransaction {
|
||||
it.copyToRealmOrUpdate(liveDataQsRecordBean.value)
|
||||
}
|
||||
realm.close()
|
||||
// realm.close()
|
||||
mapController.layerManagerHandler.addOrUpdateQsRecordMark(liveDataQsRecordBean.value!!)
|
||||
liveDataFinish.postValue(true)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteData() {
|
||||
// viewModelScope.launch(Dispatchers.IO) {
|
||||
// val qsRecord = QsRecordBean(
|
||||
// id = UUID.randomUUID().toString(),
|
||||
// classType = liveDataCurrentClassType.value.toString(),
|
||||
// type = liveDataCurrentProblemType.value.toString(),
|
||||
// phenomenon = liveDataCurrentPhenomenon.value.toString(),
|
||||
// problemLink = liveDataCurrentProblemLink.value.toString(),
|
||||
// cause = liveDataCurrentCause.value.toString(),
|
||||
// )
|
||||
// qsRecord.geometry = currentGeoPoint!!.toGeometry()
|
||||
// val realm = Realm.getDefaultInstance()
|
||||
// realm.executeTransaction {
|
||||
// it.copyToRealmOrUpdate(qsRecord)
|
||||
// }
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
Log.e("jingo","realm hashCOde ${realm.hashCode()}")
|
||||
realm.executeTransaction {
|
||||
val objects = it.where(QsRecordBean::class.java)
|
||||
.equalTo("id", liveDataQsRecordBean.value?.id).findFirst()
|
||||
objects?.deleteFromRealm()
|
||||
}
|
||||
// realm.close()
|
||||
// mapController.layerManagerHandler.addOrUpdateQsRecordMark(qsRecord)
|
||||
// liveDataFinish.postValue(true)
|
||||
// }
|
||||
mapController.layerManagerHandler.removeQsRecordMark(liveDataQsRecordBean.value!!)
|
||||
liveDataFinish.postValue(true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,7 +242,8 @@ class EvaluationResultViewModel @Inject constructor(
|
||||
val objects = realm.where<QsRecordBean>().equalTo("id", id).findFirst()
|
||||
|
||||
if (objects != null) {
|
||||
liveDataQsRecordBean.postValue(realm.copyFromRealm(objects))
|
||||
oldBean = realm.copyFromRealm(objects)
|
||||
liveDataQsRecordBean.postValue(oldBean!!.copy())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.databinding.AdapterOfflineMapCityBinding
|
||||
import com.navinfo.omqs.bean.OfflineMapCityBean
|
||||
import com.navinfo.omqs.http.offlinemapdownload.OfflineMapDownloadManager
|
||||
import com.navinfo.omqs.tools.FileManager
|
||||
import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus
|
||||
import com.navinfo.omqs.ui.other.BaseRecyclerViewAdapter
|
||||
import com.navinfo.omqs.ui.other.BaseViewHolder
|
||||
import javax.inject.Inject
|
||||
@@ -31,11 +33,11 @@ class OfflineMapCityListAdapter(
|
||||
if (it.tag != null) {
|
||||
val cityBean = data[it.tag as Int]
|
||||
when (cityBean.status) {
|
||||
OfflineMapCityBean.NONE, OfflineMapCityBean.UPDATE, OfflineMapCityBean.PAUSE, OfflineMapCityBean.ERROR -> {
|
||||
FileDownloadStatus.NONE, FileDownloadStatus.UPDATE, FileDownloadStatus.PAUSE, FileDownloadStatus.ERROR -> {
|
||||
Log.e("jingo", "开始下载 ${cityBean.status}")
|
||||
downloadManager.start(cityBean.id)
|
||||
}
|
||||
OfflineMapCityBean.LOADING, OfflineMapCityBean.WAITING -> {
|
||||
FileDownloadStatus.LOADING, FileDownloadStatus.WAITING -> {
|
||||
Log.e("jingo", "暂停 ${cityBean.status}")
|
||||
downloadManager.pause(cityBean.id)
|
||||
}
|
||||
@@ -88,37 +90,37 @@ class OfflineMapCityListAdapter(
|
||||
binding.offlineMapProgress.progress =
|
||||
(cityBean.currentSize * 100 / cityBean.fileSize).toInt()
|
||||
when (cityBean.status) {
|
||||
OfflineMapCityBean.NONE -> {
|
||||
FileDownloadStatus.NONE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "下载"
|
||||
}
|
||||
OfflineMapCityBean.WAITING -> {
|
||||
FileDownloadStatus.WAITING -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "等待中"
|
||||
}
|
||||
OfflineMapCityBean.LOADING -> {
|
||||
FileDownloadStatus.LOADING -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "暂停"
|
||||
}
|
||||
OfflineMapCityBean.PAUSE -> {
|
||||
FileDownloadStatus.PAUSE -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "继续"
|
||||
}
|
||||
OfflineMapCityBean.ERROR -> {
|
||||
FileDownloadStatus.ERROR -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "重试"
|
||||
}
|
||||
OfflineMapCityBean.DONE -> {
|
||||
FileDownloadStatus.DONE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "已完成"
|
||||
}
|
||||
OfflineMapCityBean.UPDATE -> {
|
||||
FileDownloadStatus.UPDATE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "更新"
|
||||
|
||||
@@ -8,11 +8,12 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
import com.navinfo.omqs.databinding.FragmentOfflineMapBinding
|
||||
import com.navinfo.omqs.ui.fragment.BaseFragment
|
||||
|
||||
/**
|
||||
* 离线地图总页面
|
||||
*/
|
||||
class OfflineMapFragment : Fragment() {
|
||||
class OfflineMapFragment : BaseFragment() {
|
||||
|
||||
private var _binding: FragmentOfflineMapBinding? = null
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.navinfo.omqs.db.ImportOMDBHelper
|
||||
import com.navinfo.omqs.hilt.ImportOMDBHiltFactory
|
||||
import com.navinfo.omqs.tools.CoroutineUtils
|
||||
import com.navinfo.omqs.ui.activity.BaseActivity
|
||||
import com.navinfo.omqs.ui.fragment.BaseFragment
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmDictionary
|
||||
@@ -38,12 +39,13 @@ import javax.inject.Inject
|
||||
* 个人中心
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class PersonalCenterFragment : Fragment(), FSAFActivityCallbacks {
|
||||
class PersonalCenterFragment : BaseFragment(), FSAFActivityCallbacks {
|
||||
|
||||
private var _binding: FragmentPersonalCenterBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
private val fileChooser by lazy { FileChooser(requireContext()) }
|
||||
private val viewModel by lazy { viewModels<PersonalCenterViewModel>().value }
|
||||
|
||||
@Inject
|
||||
lateinit var importOMDBHiltFactory: ImportOMDBHiltFactory
|
||||
|
||||
@@ -61,9 +63,9 @@ class PersonalCenterFragment : Fragment(), FSAFActivityCallbacks {
|
||||
binding.root.setNavigationItemSelectedListener {
|
||||
when (it.itemId) {
|
||||
R.id.personal_center_menu_offline_map ->
|
||||
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
|
||||
findNavController().navigate(R.id.OfflineMapFragment)
|
||||
R.id.personal_center_menu_obtain_data -> { // 生成数据,根据sqlite文件生成对应的zip文件
|
||||
fileChooser.openChooseFileDialog(object: FileChooserCallback() {
|
||||
fileChooser.openChooseFileDialog(object : FileChooserCallback() {
|
||||
override fun onCancel(reason: String) {
|
||||
}
|
||||
|
||||
@@ -71,31 +73,47 @@ class PersonalCenterFragment : Fragment(), FSAFActivityCallbacks {
|
||||
val file = UriUtils.uri2File(uri)
|
||||
// 开始导入数据
|
||||
// 656e6372797000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
val job = CoroutineUtils.launchWithLoading(requireContext(), loadingMessage = "生成数据...") {
|
||||
val importOMDBHelper: ImportOMDBHelper = importOMDBHiltFactory.obtainImportOMDBHelper(requireContext(), file, File(file.parentFile, "config.json"))
|
||||
val job = CoroutineUtils.launchWithLoading(
|
||||
requireContext(),
|
||||
loadingMessage = "生成数据..."
|
||||
) {
|
||||
val importOMDBHelper: ImportOMDBHelper =
|
||||
importOMDBHiltFactory.obtainImportOMDBHelper(
|
||||
requireContext(),
|
||||
file,
|
||||
File(file.parentFile, "config.json")
|
||||
)
|
||||
viewModel.obtainOMDBZipData(importOMDBHelper)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
R.id.personal_center_menu_import_data -> { // 导入zip数据
|
||||
fileChooser.openChooseFileDialog(object: FileChooserCallback() {
|
||||
fileChooser.openChooseFileDialog(object : FileChooserCallback() {
|
||||
override fun onCancel(reason: String) {
|
||||
}
|
||||
|
||||
override fun onResult(uri: Uri) {
|
||||
val file = UriUtils.uri2File(uri)
|
||||
// 开始导入数据
|
||||
CoroutineUtils.launchWithLoading(requireContext(), loadingMessage = "导入数据...") {
|
||||
val importOMDBHelper: ImportOMDBHelper = importOMDBHiltFactory.obtainImportOMDBHelper(requireContext(), file, File(file.parentFile, "config.json"))
|
||||
CoroutineUtils.launchWithLoading(
|
||||
requireContext(),
|
||||
loadingMessage = "导入数据..."
|
||||
) {
|
||||
val importOMDBHelper: ImportOMDBHelper =
|
||||
importOMDBHiltFactory.obtainImportOMDBHelper(
|
||||
requireContext(),
|
||||
file,
|
||||
File(file.parentFile, "config.json")
|
||||
)
|
||||
viewModel.importOMDBData(importOMDBHelper)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
R.id.personal_center_menu_import_yuan_data->{
|
||||
R.id.personal_center_menu_import_yuan_data -> {
|
||||
// 用户选中导入数据,打开文件选择器,用户选择导入的数据文件目录
|
||||
fileChooser.openChooseFileDialog(object: FileChooserCallback() {
|
||||
fileChooser.openChooseFileDialog(object : FileChooserCallback() {
|
||||
override fun onCancel(reason: String) {
|
||||
}
|
||||
|
||||
@@ -108,6 +126,9 @@ class PersonalCenterFragment : Fragment(), FSAFActivityCallbacks {
|
||||
R.id.personal_center_menu_test -> {
|
||||
viewModel.readRealmData()
|
||||
}
|
||||
R.id.personal_center_menu_task_list -> {
|
||||
findNavController().navigate(R.id.TaskListFragment)
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.navinfo.omqs.ui.fragment.tasklist
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.Observer
|
||||
import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.bean.TaskBean
|
||||
import com.navinfo.omqs.databinding.AdapterTaskListBinding
|
||||
import com.navinfo.omqs.http.taskdownload.TaskDownloadManager
|
||||
import com.navinfo.omqs.tools.FileManager.Companion.FileDownloadStatus
|
||||
import com.navinfo.omqs.ui.other.BaseRecyclerViewAdapter
|
||||
import com.navinfo.omqs.ui.other.BaseViewHolder
|
||||
|
||||
/**
|
||||
* 离线地图城市列表 RecyclerView 适配器
|
||||
*
|
||||
* 在 RecycleView 的 ViewHolder 中监听 ViewModel 的 LiveData,然后此时传递的 lifecycleOwner 是对应的 Fragment。由于 ViewHolder 的生命周期是比 Fragment 短的,所以当 ViewHolder 销毁时,由于 Fragment 的 Lifecycle 还没有结束,此时 ViewHolder 会发生内存泄露(监听的 LiveData 没有解绑)
|
||||
* 这种场景下有两种解决办法:
|
||||
*使用 LiveData 的 observeForever 然后在 ViewHolder 销毁前手动调用 removeObserver
|
||||
*使用 LifecycleRegistry 给 ViewHolder 分发生命周期(这里使用了这个)
|
||||
*/
|
||||
class TaskListAdapter(
|
||||
private val downloadManager: TaskDownloadManager, private val context: Context
|
||||
) : BaseRecyclerViewAdapter<TaskBean>() {
|
||||
|
||||
|
||||
private val downloadBtnClick = View.OnClickListener() {
|
||||
if (it.tag != null) {
|
||||
val taskBean = data[it.tag as Int]
|
||||
when (taskBean.status) {
|
||||
FileDownloadStatus.NONE, FileDownloadStatus.UPDATE, FileDownloadStatus.PAUSE, FileDownloadStatus.ERROR -> {
|
||||
Log.e("jingo", "开始下载 ${taskBean.status}")
|
||||
downloadManager.start(taskBean.id)
|
||||
}
|
||||
FileDownloadStatus.LOADING, FileDownloadStatus.WAITING -> {
|
||||
Log.e("jingo", "暂停 ${taskBean.status}")
|
||||
downloadManager.pause(taskBean.id)
|
||||
}
|
||||
else -> {
|
||||
Log.e("jingo", "暂停 ${taskBean.status}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
|
||||
val viewBinding =
|
||||
AdapterTaskListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
return BaseViewHolder(viewBinding)
|
||||
}
|
||||
|
||||
override fun onViewRecycled(holder: BaseViewHolder) {
|
||||
super.onViewRecycled(holder)
|
||||
//页面滑动时会用holder重构页面,但是对进度条的监听回调会一直返回,扰乱UI,所以当当前holder去重构的时候,移除监听
|
||||
downloadManager.removeObserver(holder.tag.toInt())
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
|
||||
val binding: AdapterTaskListBinding =
|
||||
holder.viewBinding as AdapterTaskListBinding
|
||||
val taskBean = data[position]
|
||||
//tag 方便onclick里拿到数据
|
||||
holder.tag = taskBean.id.toString()
|
||||
changeViews(binding, taskBean)
|
||||
downloadManager.addTask(taskBean)
|
||||
downloadManager.observer(taskBean.id, holder, DownloadObserver(taskBean.id, binding))
|
||||
binding.taskDownloadBtn.tag = position
|
||||
binding.taskDownloadBtn.setOnClickListener(downloadBtnClick)
|
||||
binding.taskName.text = taskBean.evaluationTaskName
|
||||
// binding.offlineMapCitySize.text = cityBean.getFileSizeText()
|
||||
}
|
||||
|
||||
inner class DownloadObserver(val id: Int, val binding: AdapterTaskListBinding) :
|
||||
Observer<TaskBean> {
|
||||
override fun onChanged(t: TaskBean?) {
|
||||
if (id == t?.id)
|
||||
changeViews(binding, t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun changeViews(binding: AdapterTaskListBinding, cityBean: TaskBean) {
|
||||
binding.taskProgress.progress =
|
||||
(cityBean.currentSize * 100 / cityBean.fileSize).toInt()
|
||||
when (cityBean.status) {
|
||||
FileDownloadStatus.NONE -> {
|
||||
if (binding.taskProgress.visibility == View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.taskDownloadBtn.text = "下载"
|
||||
}
|
||||
FileDownloadStatus.WAITING -> {
|
||||
if (binding.taskProgress.visibility != View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.taskDownloadBtn.text = "等待中"
|
||||
}
|
||||
FileDownloadStatus.LOADING -> {
|
||||
if (binding.taskProgress.visibility != View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.taskDownloadBtn.text = "暂停"
|
||||
}
|
||||
FileDownloadStatus.PAUSE -> {
|
||||
if (binding.taskProgress.visibility != View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.taskDownloadBtn.text = "继续"
|
||||
}
|
||||
FileDownloadStatus.ERROR -> {
|
||||
if (binding.taskProgress.visibility != View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.taskDownloadBtn.text = "重试"
|
||||
}
|
||||
FileDownloadStatus.DONE -> {
|
||||
if (binding.taskProgress.visibility == View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.taskDownloadBtn.text = "已完成"
|
||||
}
|
||||
FileDownloadStatus.UPDATE -> {
|
||||
if (binding.taskProgress.visibility == View.VISIBLE) binding.taskProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.taskDownloadBtn.text = "更新"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemViewRes(position: Int): Int {
|
||||
return R.layout.adapter_offline_map_city
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.navinfo.omqs.ui.fragment.tasklist
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.navinfo.omqs.databinding.FragmentTaskListBinding
|
||||
import com.navinfo.omqs.http.taskdownload.TaskDownloadManager
|
||||
import com.navinfo.omqs.ui.fragment.BaseFragment
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TaskListFragment : BaseFragment(){
|
||||
@Inject
|
||||
lateinit var downloadManager: TaskDownloadManager
|
||||
private var _binding: FragmentTaskListBinding? = null
|
||||
private val viewModel by viewModels<TaskListViewModel>()
|
||||
private val binding get() = _binding!!
|
||||
private val adapter: TaskListAdapter by lazy {
|
||||
TaskListAdapter(
|
||||
downloadManager,
|
||||
requireContext()
|
||||
)
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = FragmentTaskListBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
//// 设置 RecyclerView 的固定大小,避免在滚动时重新计算视图大小和布局,提高性能
|
||||
binding.taskRecyclerview.setHasFixedSize(true)
|
||||
binding.taskRecyclerview.layoutManager = layoutManager
|
||||
binding.taskRecyclerview.adapter = adapter
|
||||
viewModel.liveDataTaskList.observe(viewLifecycleOwner) {
|
||||
adapter.refreshData(it)
|
||||
}
|
||||
viewModel.getTaskList(requireContext())
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.navinfo.omqs.ui.fragment.tasklist
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.omqs.bean.TaskBean
|
||||
import com.navinfo.omqs.http.NetResult
|
||||
import com.navinfo.omqs.http.NetworkService
|
||||
import com.navinfo.omqs.tools.FileManager
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import io.realm.Realm
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class TaskListViewModel @Inject constructor(
|
||||
private val networkService: NetworkService
|
||||
) : ViewModel() {
|
||||
|
||||
val liveDataTaskList = MutableLiveData<List<TaskBean>>()
|
||||
|
||||
fun getTaskList(context: Context) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
Log.e("jingo","realm hashCOde ${realm.hashCode()}")
|
||||
when (val result = networkService.getTaskList("02911")) {
|
||||
is NetResult.Success -> {
|
||||
if (result.data != null) {
|
||||
realm.executeTransaction {
|
||||
realm.copyToRealmOrUpdate(result.data.obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
is NetResult.Error -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(context, "${result.exception.message}", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
is NetResult.Failure -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(context, "${result.code}:${result.msg}", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
is NetResult.Loading -> {}
|
||||
else -> {}
|
||||
}
|
||||
val objects = realm.where(TaskBean::class.java).findAll()
|
||||
liveDataTaskList.postValue(realm.copyFromRealm(objects))
|
||||
// realm.close()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user