package com.offline.music.task.player.db.tools

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.room.Room
import com.offline.music.task.player.ProApp
import com.offline.music.task.player.db.bean.Audio
import kotlinx.coroutines.launch

class CollectViewModel : ViewModel() {
    private var likeData: MutableLiveData<List<Audio>> = MutableLiveData<List<Audio>>()

    private val database = Room.databaseBuilder(
        ProApp.appContext, AppDatabase::class.java, "local_audio_viewer_database"
    ).build()

    private val audioFileDao = database.localAudioDao()

    init {
        viewModelScope.launch {
            likeData.value = audioFileDao.getCollectData()
        }
    }

    fun update() {
        viewModelScope.launch {
            likeData.value = audioFileDao.getCollectData()
        }
    }

    fun getList() = likeData

    override fun onCleared() {
        super.onCleared()
    }

}