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
package cn.flightfeather.thirdappmodule.module.common
 
import cn.flightfeather.thirdappmodule.bean.entity.Subtask
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.SearchRepository
import cn.flightfeather.thirdappmodule.view.recyclerview.DataLoadModel
 
/**
 * @author riku
 * Date: 2022/2/17
 * 查询界面viewModel
 */
class SearchViewModel : BaseViewModel() {
    private val searchRepository = SearchRepository()
 
    // 搜索子任务
    var keyword = ""// 搜索关键字
    val subtaskResult = object : DataLoadModel<Subtask>(application) {
        override fun loadDataByRefresh() {
            searchSubtask(keyword, 1, this)
        }
 
        override fun loadDataByLoadMore(page: Int) {
            searchSubtask(keyword, page, this)
        }
    }
 
    /**
     * 根据关键字搜索子任务
     * @param keyword 关键词
     * @param page 分页数
     * @param callBack 结果回调函数
     */
    fun searchSubtask(keyword: String, page: Int, callBack: ResultCallBack<List<Subtask>>) {
        if (keyword.isEmpty()) {
            callBack.onSuccess(emptyList())
        } else {
            searchRepository.searchSubtask(application.currentUser.guid, keyword, page, callBack)
        }
    }
}