| | |
| | | package com.example.firstapp.ui.dashboard |
| | | |
| | | import androidx.lifecycle.LiveData |
| | | import androidx.lifecycle.MutableLiveData |
| | | import androidx.lifecycle.ViewModel |
| | | import android.app.Application |
| | | import androidx.lifecycle.AndroidViewModel |
| | | import androidx.lifecycle.asLiveData |
| | | import androidx.lifecycle.viewModelScope |
| | | import com.example.firstapp.AppDatabase |
| | | import com.example.firstapp.database.entity.Code |
| | | import com.example.firstapp.database.repository.CodeRepository |
| | | import kotlinx.coroutines.launch |
| | | |
| | | class DashboardViewModel : ViewModel() { |
| | | class DashboardViewModel(application: Application) : AndroidViewModel(application) { |
| | | |
| | | private val _text = MutableLiveData<String>().apply { |
| | | value = "This is dashboard Fragment" |
| | | private val repository: CodeRepository |
| | | |
| | | init { |
| | | val codeDao = AppDatabase.getInstance(application).codeDao() |
| | | repository = CodeRepository(codeDao) |
| | | } |
| | | val text: LiveData<String> = _text |
| | | |
| | | fun getPackages(date: Long, dateType: String) = |
| | | repository.getPackages(date, dateType).asLiveData() |
| | | |
| | | fun getCourierStats(date: Long) = repository.getCourierStats(date).asLiveData() |
| | | |
| | | fun getDailyStats() = repository.getDailyStats().asLiveData() |
| | | |
| | | fun insert(code: Code) = viewModelScope.launch { |
| | | repository.insert(code) |
| | | } |
| | | } |