Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not an issue, just wanna ask about adapter #3

Open
MrBrowner opened this issue Jun 30, 2024 · 13 comments
Open

Not an issue, just wanna ask about adapter #3

MrBrowner opened this issue Jun 30, 2024 · 13 comments
Assignees
Labels
enhancement New feature or request

Comments

@MrBrowner
Copy link

MrBrowner commented Jun 30, 2024

Hey, Thanks you for the library, it's easy to use.

but, how can I add custom functions in adapter?
Can I also add Filterable?

If, YES then how can i add to it.
Thanks in advance.

@Vshnv
Copy link
Owner

Vshnv commented Jun 30, 2024

Nice to know the library was useful for you. 😄.

Could you explain what you mean by custom functions in adapter? Im not sure I understand what you're reffering to.
Do you mean functions inside the adapter? If so, yea, kotlin allows you to define functions inside the scope of a lambda too. You can also alternatively define it in the top level and use it.

fun createPersonAdapter() = adapt<Person> {
    fun generateAgeText(person: Person) {
        return "Aged ${person.age}"
    }
    create {
        val personBinding = LayoutPersonBinding.inflate(LayoutInflater.from(it.context), it, false)
        ViewSource.BindingViewSource(personBinding, ViewBinding::getRoot)
    }.bind {
        binding.name.text = data.name
        binding.age.text = generateAgeText(data)
    }
}

For filterable, I assume you mean filtering the submitted data. Currently no, the entire data passed to the adapter is used as the data set for the adapter. But this could definitely be something I could add on for the next version if you're interested.

@Vshnv Vshnv added the enhancement New feature or request label Jun 30, 2024
@Vshnv Vshnv self-assigned this Jun 30, 2024
@MrBrowner
Copy link
Author

I would also be happy to make contribution. 😄
But I don't know the whole design/principle of Kotlin, for making this lib.

@MrBrowner
Copy link
Author

Could you explain what you mean by custom functions in adapter?

example:

  1. a fun which gives me filtered list from storedList (i.e. currentList in adapter)
fun getSelectedItems(person: Person): List<Person> {
    return currentList.filter { it.isSelected == true }
}
  1. I currently use filterable (to use search feature) as follows
class CLASS_NAME (
    private var itemList: List<Person>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder> (), Filterable {
    private var filteredItemList: List<Person>

    init { this.filteredItemList = itemList }

    override fun getItemCount(): Int {
        return filteredItemList.size
    }
    ...

    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(charSequence: CharSequence): FilterResults {...}

            override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {...}
        }
    }
}

@MrBrowner
Copy link
Author

done, built the things I wanted to include.

Thanks, and wanna be friends?

@MrBrowner
Copy link
Author

MrBrowner commented Jul 5, 2024

i have one more doubt.

on row item binding click listener, do i have to directly call activity fun or var ref.
OR make a listener ?

eg.

create {...}
.bind {
    ...
    binding.root.setSafeOnClickListener { _ ->
        listener.onRowUiClick(index, data) // listener is val defined in activity.
    
        openNextPage() // activity fun defined to perform next task after on click.
    }
}

which way is correct & better?

@Vshnv
Copy link
Owner

Vshnv commented Jul 7, 2024

done, built the things I wanted to include.
 
Thanks, and wanna be friends?

Thats great! If you find anything you think would be useful in the library, always welcoming a PR 😄. 
And sure! 😄

i have one more doubt.
 
on row item binding click listener, do i have to directly call activity fun or var ref. OR make a listener ?
 
eg.
 

create {...}
.bind {
     ...
     binding.root.setSafeOnClickListener { _ ->
         listener.onRowUiClick(index, data) // listener is val defined in activity.
     
         openNextPage() // activity fun defined to perform next task after on click.
     }
}

 
which way is correct & better?

I usually define a function that creates the adapter using adapt and pass the callbacks to the function, that way I can reuse the adapter multiple times.
Heres an example:

fun createArticleAdapter(onArticleClicked: (index: Int, data: Article) -> Unit= adapt<Article> {
       create(...).bind {
             binding.root.setOnClickListener {
                   onArticleClicked(index, data)
             }
       }
}

This way I can recreate the adapter where required with its own custom callback.

class MainActivity: AppCompatActivity {
        private val articleAdaoter = createArticleAdapter(this::handleArticleClicked)
        
        
        private fun handleArticleClicked(index: Int, data: Article) -> Unit {
            // handle callback
        }
}

@MrBrowner
Copy link
Author

MrBrowner commented Jul 9, 2024

Nice.

So, there is another doubt. Here is the code.

binding.root.setOnLongClickListener { v ->
    v?.let { view ->
        data.isUiSelected = !data.isUiSelected
        adapter.notifyDataSetChanged() // 1.
        adapter.notifyItemChanged(index) // 2.
    }
    true
}

where i'm updating the data after long click.
But, to update the ui i have to notify the adapter. How should i do points (1 or 2).

currently, it's not possible.

@Vshnv
Copy link
Owner

Vshnv commented Jul 11, 2024

Point 1 should be fine since Adapt implements Async diffing by default. Implementating contentEquals and itemEquals would be useful to help diffing: https://vshnv.github.io/adapt/dsl/

Having access to adapter inside bind and withLifecycle is definitely neccessary. I'll add adapter to bindscope in next release.

For now what you can do is define a function outside the adapt scope thus capturing the adapter variable and then call it from inside the setOnLongClickListener

@MrBrowner
Copy link
Author

hey @Vshnv,

can we have a scheduled meet?
I want to understand the concept of this lib.

Thanks & Regards
Browner

@Vshnv
Copy link
Owner

Vshnv commented Jul 25, 2024

Hey,
Sure, sounds great.
Feel free to select a time that suits you:
https://calendly.com/vaishnavanil7th/adapt-android-lib-discussion

@MrBrowner
Copy link
Author

I'm mostly available on Sunday or Saturday. 🫥

@Vshnv
Copy link
Owner

Vshnv commented Aug 8, 2024

Hey, sorry for the delay. I got caught up in work. Added Saturdays and Sundays
https://calendly.com/vaishnavanil7th/adapt-android-lib-discussion

@MrBrowner
Copy link
Author

selected this coming Saturday 11am for meet time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants