Git Product home page Git Product logo

Comments (5)

luizgrp avatar luizgrp commented on June 12, 2024

Good point 👍

We could change it to use the generated string for the section as key, but then this method getAdapterForSection(Section) would have to loop through the sections to find the tag before returning the SectionAdapter.

Or we could just not store any SectionAdapter and have them created on the go by the getAdapterForSection methods. In this case we would also have loop through the sections to check if the Section passed as parameter is valid. So not much benefit in relation to the previous option, as it will also have to instantiate a class on top of looping through the sections.

Both options above will and an extra loop through the sections, so I feel like we could compromise and keep the current solution.

What do you think? Do you have any other ideas?

from sectionedrecyclerviewadapter.

yccheok avatar yccheok commented on June 12, 2024

Hem...

I just feel that, having a class instance, for every Section, to perform a set of "helper function operation", doesn't look very good to me. It just smell redundant, and not efficient.

Usually, for "helper functions", I prefer to design them as public static set of functions.

But, such design would put burden on caller. We would need to have

public static int getPositionInSection(SectionedRecyclerViewAdapter sectionedAdapter, Section section, final int position)

instead

public int getPositionInSection(final int position)

My understanding is that, you wish to move out all "helper functions" implementation out from SectionedRecyclerViewAdapter class ?

Do you want to consider using default function feature of interface?

Something which looks like

interface SectionPositionIdentifier {
    SectionedRecyclerViewAdapter getSectionedRecyclerViewAdapter();
    
    // All helper functions implementation will go into SectionPositionIdentifier's default functions.
    default int getPositionInSection(final int position) {
        SectionedRecyclerViewAdapter sectionedRecyclerViewAdapter = getSectionedRecyclerViewAdapter();
        // Perform some computation based on sectionedRecyclerViewAdapter.
        return 0;
    }
}

public class SectionedRecyclerViewAdapter implements SectionPositionIdentifier {

    @Override
    public SectionedRecyclerViewAdapter getSectionedRecyclerViewAdapter() {
        return this;
    }        
}

from sectionedrecyclerviewadapter.

luizgrp avatar luizgrp commented on June 12, 2024

I agree with you. They are helper methods and I considered to build them as static methods, but then decided to make it convenient instead.

My understanding is that, you wish to move out all "helper functions" implementation out from SectionedRecyclerViewAdapter class ?

Reasoning is described in this pull request #153

Do you want to consider using default function feature of interface?

That would address items 1 and 3 of the "Pros" described in that pull request, but not items 2 and 4.
Also, the idea was to distinguish between an adapter to manipulate sections and another to manipulate items from a specific section.

Let me know your thoughts..

from sectionedrecyclerviewadapter.

yccheok avatar yccheok commented on June 12, 2024

You are right. The proposed default interface method doesn't address item 2 and 4.

In such case, your current HashMap might be the best available option.

But, I feel maybe we can have the following improvement in the following line?

https://github.com/luizgrp/SectionedRecyclerViewAdapter/blob/master/library/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/SectionedRecyclerViewAdapter.java#L224

Check the returned value of

sectionAdapters.put(section, new SectionAdapter(this, section));

It should return null. If not, throw runtime exception as this might imply the caller overrides equals and hashCode methods. Such action will cause 2 different instance of Sections returning same value in equals and hashCode methods.

This is kind of defensive way, to have early detection on problematic implementation.

Thanks.

from sectionedrecyclerviewadapter.

luizgrp avatar luizgrp commented on June 12, 2024

sounds good 👍 let's do it

from sectionedrecyclerviewadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.