package com.base.appzxhy.ui.main.mylist

import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.databinding.FragmentMyListPageBinding
import com.base.appzxhy.ui.adapter.ShortPlayAdapter
import com.base.appzxhy.utils.SpJsonUtils
import com.base.appzxhy.utils.SpJsonUtils.KEY_SHORTPLAY_FAVORITE
import com.bytedance.sdk.shortplay.api.ShortPlay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MyListPageFragment : BaseFragment<FragmentMyListPageBinding>(FragmentMyListPageBinding::inflate) {

    private val shortPlayAdapter = ShortPlayAdapter(R.layout.item_shortplay_style_4)
    override fun initView() {
        super.initView()
        binding.rv.adapter = shortPlayAdapter

    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    private fun refreshData() = lifecycleScope.launch(Dispatchers.IO) {
        val list = SpJsonUtils.getSpJsonList<ShortPlay>(KEY_SHORTPLAY_FAVORITE)
        launch(Dispatchers.Main) {
            binding.llEmpty.isVisible = list.isEmpty()
            shortPlayAdapter.submitList(list)
            val pfg = parentFragment as MyListFragment
            pfg.changeEditEnable(list.isNotEmpty())
        }
    }

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