package com.example.firstapp.ui.home
|
|
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.viewModelScope
|
import com.example.firstapp.core.Core
|
import com.example.firstapp.database.entity.Code
|
import com.example.firstapp.model.ExpressGroup
|
import com.example.firstapp.model.ExpressPackage
|
import kotlinx.coroutines.launch
|
|
class HomeViewModel : ViewModel() {
|
|
// private val _text = MutableLiveData<String>().apply {
|
// value = "短信主页面"
|
// }
|
// val text: LiveData<String> = _text
|
//
|
// private val _codeList = MutableLiveData<List<Code>>()
|
//
|
// val codeList: LiveData<List<Code>> get() = _codeList
|
|
private val _expressItems = MutableLiveData<List<ExpressGroup>>()
|
val expressItems: LiveData<List<ExpressGroup>> = _expressItems
|
|
init {
|
// 初始化时加载数据
|
// loadData()
|
loadExpressData()
|
}
|
|
// 加载数据的方法
|
// fun loadData() {
|
// // 获取数据,并更新 LiveData
|
// _codeList.value = Core.code.getAllDesc() // 假设这是获取最新的 data 的方法
|
// }
|
//
|
// // 如果需要手动更新数据,可以调用这个方法
|
// fun updateData() {
|
// _codeList.value = Core.code.getAllDesc() // 重新获取并更新数据
|
// }
|
|
fun loadExpressData() {
|
viewModelScope.launch {
|
// 1. 获取所有驿站类型的提醒设置
|
val stations = Core.reminder.getByType("驿站")
|
|
// 2. 按驿站分组获取包裹信息
|
val groups = stations.map { station ->
|
val packages = Core.code.getByKeyword(station.nickname).map { code ->
|
ExpressPackage(
|
id = code.id, //ID
|
company = code.name, //快递公司
|
trackingNumber = code.code, // 取件码
|
createTime = code.createtime //快递时间
|
)
|
}
|
ExpressGroup(
|
stationName = station.nickname,
|
packages = packages
|
)
|
}
|
|
_expressItems.postValue(groups)
|
}
|
}
|
|
}
|