cloudroam
2025-03-03 4cb9946eff3626389ae93feef4250dd3d45fb694
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)
        }
    }
 
}