mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.85k stars 493 forks source link

SelectExtensions.select() not working when create view #1027

Closed dsantix closed 2 years ago

dsantix commented 2 years ago

About this issue

Hi, mike. First, sorry for the bad English. I'm trying to use selectExtensions.select() when I start a fragment to retrieve the old choices and it's not working. Basically, I'm using the Material Chip as an item to select and save the positions and return when I come back to the Controller in the future. To be more understandable to you, this is my code. Is it possible to do that ?

Select only working when calling by a button after view created.

private val preferences : PreferenceHelper by injectLazy()
private lateinit var fastItemLeagueAdapter: GenericFastItemAdapter
private var lolLeagues: ArrayList<League>? = null
private var valLeagues: ArrayList<ValLeague>? = null
private var selectExtension:SelectExtension<GenericItem>? = null

override fun onViewCreated(view: View) {
    super.onViewCreated(view)

    fastItemLeagueAdapter = FastItemAdapter()
    fastItemLeagueAdapter.getExpandableExtension()
    selectExtension = fastItemLeagueAdapter.getSelectExtension()
    selectExtension?.apply {
            multiSelect = true
            isSelectable = true
            selectWithItemUpdate = true
            //Convert and return Set<Int>
            select(preferences.saveItemsPostionsLeagues().get().map { it.toInt() }.toSet())
            selectionListener = object : ISelectionListener<GenericItem>{
                override fun onSelectionChanged(item: GenericItem, selected: Boolean) {
                    Log.d("Selections", selections.toString())
                }
            }
        }

    val layoutManager = GridLayoutManager(view.context,3)
    layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
        override fun getSpanSize(position: Int): Int {
            return when(fastItemLeagueAdapter.getItemViewType(position)){
                R.id.fastadapter_leagues_item -> 1
                R.id.fastadapter_header_expandable_item -> layoutManager.spanCount
                else -> -1
            }
        }

    }

    binding.recyclerLeagues.layoutManager = layoutManager
    binding.recyclerLeagues.itemAnimator = SlideDownAlphaAnimator()
    binding.recyclerLeagues.adapter = fastItemLeagueAdapter

   //Fill with some data from Retrofit API
    val items = ArrayList<HeaderExpandableSectionItem>()
    val headerExpandable = HeaderExpandableSectionItem()
    val headerValExpandable = HeaderExpandableSectionItem()
    headerExpandable.withHeader(view.resources.getString(R.string.league_of_legends))
    headerValExpandable.withHeader(view.resources.getString(R.string.valorant))

    val lolLeagueItem = ArrayList<LeaguesBindingItem>()
    val valorantLeagueItem = ArrayList<LeaguesBindingItem>()

    viewScope.launchUI {
        lolLeagues = presenter.getLolLeagues()
        valLeagues = presenter.getValLeagues()

        for (league in lolLeagues!!){
            val leagueItem = LeaguesBindingItem()
            leagueItem.withModel(league)
            lolLeagueItem.add(leagueItem)
        }
        headerExpandable.subItems.addAll(lolLeagueItem)
        items.add(headerExpandable)

        for (valLeague in valLeagues!!){
            val leagueItem = LeaguesBindingItem()
            leagueItem.withModel(valLeague)
            valorantLeagueItem.add(leagueItem)
        }

        headerValExpandable.subItems.addAll(valorantLeagueItem)
        items.add(headerValExpandable)

        fastItemLeagueAdapter.add(items)
    }

    //EventHook to active checked icon
    fastItemLeagueAdapter.addEventHook(LeaguesBindingItem.LeagueClickEvent())

    //Save positions and items to a database and Preferences
    binding.materialButton2.setOnClickListener {
        val arraySelectedLeagues = arrayListOf<Any>()
        for (item in selectExtension?.selectedItems!!){
            val itemLeaguesBindingItem = item as LeaguesBindingItem
            when(itemLeaguesBindingItem.modelLeague){
                is ValLeague ->{
                    val item1 = itemLeaguesBindingItem.modelLeague as ValLeague
                    arraySelectedLeagues.add(item1)
                }
                is League ->{
                    val item2 = itemLeaguesBindingItem.modelLeague as League
                    arraySelectedLeagues.add(item2)
                }
            }
        }

        // save leagues to database

        val selectedLeaguesObject = ParseObject("LeaguesSelected")
        selectedLeaguesObject.put("selected_list", JSONArray(arraySelectedLeagues))
        selectedLeaguesObject.pinInBackground()
        selectedLeaguesObject.saveEventually()

        //Convert to Set<String> and save positions to preferences

        val selectItemsSavePositions = selectExtension?.selections?.map {
            it.toString()
        }?.toSet()

        if (selectItemsSavePositions != null) {
            preferences.saveItemsPostionsLeagues().set(selectItemsSavePositions)
        }
    }
}

Notes:

-Default value from SaveItemsPositionsLeagues is a EmptySet() -Try to set "select(...)" with viewScope.launchUi -Screenshot >

Screenshot_2021-12-15-17-37-38-744_com.sntsoftwares.esports.debug.jpg

Details

Checklist

mikepenz commented 2 years ago

@dsantix it looks like you are trying to select the items before they even exist within the adapter.

You need to make sure the adapter is already filled with the specific elements before doing the selection

dsantix commented 2 years ago

That's it, I was trying to select with Expandable while in Collapse. Resolved when I select after placing items and expanding Expandable

Thx u soo much for your answer and your time.