package com.dumpster.cleaner.ui.recyclebin

import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.dumpster.cleaner.R
import com.dumpster.cleaner.base.BaseFragment
import com.dumpster.cleaner.base.goToAc
import com.dumpster.cleaner.business.ads.AdsMgr
import com.dumpster.cleaner.databinding.FragmentRecycleBinBinding
import com.dumpster.cleaner.ui.main.MainActivity
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class RecycleBinFragment : BaseFragment<FragmentRecycleBinBinding>(FragmentRecycleBinBinding::inflate) {

    private val adapter = RecycleBinAdapter()
    override fun initView() {
        super.initView()
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
        binding.rv.adapter = adapter

        lifecycleScope.launch {
            (requireActivity() as MainActivity).viewModel.recycleBinFileFlow.collectLatest {
                binding.llEmpty.isVisible = it.isEmpty()
                adapter.submitList(it)
            }
        }
    }

    override fun initListener() {
        super.initListener()
        adapter.clickAction = {
            RecycleBinDetailActivity.recycleBinFile = it
            goToAc(RecycleBinDetailActivity::class.java)
        }
    }

}