package com.wall.photography.wallpaper.fragment

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.R
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.room.MyDataBase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class CategoryViewModel : ViewModel() {
    var data: MutableLiveData<List<Bean>> = MutableLiveData()


    suspend fun getData() {
        return run {
            val mutableListOf = mutableListOf<Bean>()
            val strings = MyApp.application.resources.getStringArray(R.array.category_name)
            for (i in 1 until 8) {
                val categoryCovert =
                    MyDataBase.getInstance().getBeanDao().getCategoryCovert(strings[i])
                Log.d(MyApp.TAG, "-------categoryCovert=${categoryCovert?.cateName} ${Thread.currentThread().name}")
                if (categoryCovert != null) {
                    mutableListOf.add(categoryCovert)
                }
            }
            withContext(Dispatchers.Main){
                data.value = mutableListOf
            }
        }

    }
}