package com.ccwangluo.accelerator.ui.news

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter

class NewsCollectionAdapter(fragment: Fragment, val titles: Array<String>) :
    FragmentStateAdapter(fragment) {
    override fun getItemCount(): Int = titles.size
    var list = mutableListOf<NewsObjectFragment>()

    override fun createFragment(position: Int): Fragment {
        val fragment = NewsObjectFragment()
        fragment.arguments = Bundle().apply {
            // Our object is just an integer :-P
            putInt(ARG_OBJECT, position)
        }
        list.add(fragment)
        return fragment
    }

    fun refresh(index: Int) {
        if (index < list.size) {
            list.get(index).refresh()
        }
    }

    companion object {
        val ARG_OBJECT = "object"
    }
}
