package com.example.timert.data.viewModel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import com.example.timert.data.database.ExerciseDatabase
import com.example.timert.data.entity.Exercise
import com.example.timert.data.repository.ExerciseRepository
import kotlinx.coroutines.launch

class ExerciseViewModel(application: Application):AndroidViewModel(application){
    private val repository: ExerciseRepository
    val allExercise: LiveData<List<Exercise>>

    init{
        val exercisesDao = ExerciseDatabase.getDatabase(application, viewModelScope).exerciseDao()
        repository = ExerciseRepository(exercisesDao)
        allExercise = repository.allExercise
    }
    fun insert(exercise: Exercise) = viewModelScope.launch {
        repository.insert(exercise)
    }

    fun deleteExerciseById(id: Int) = viewModelScope.launch {
        repository.deleteExerciseById(id)
    }
}