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

Support custom data source like VirtualizedList does #88

Closed
punksta opened this issue Dec 10, 2017 · 7 comments
Closed

Support custom data source like VirtualizedList does #88

punksta opened this issue Dec 10, 2017 · 7 comments

Comments

@punksta
Copy link

punksta commented Dec 10, 2017

Thanks for cool library.
DataProvider supports only array as data source. I want to use it with Immutable.js data structures

@naqvitalha
Copy link
Collaborator

It is already possible. DataProvider can be extended into something custom. Check the following file: https://github.com/Flipkart/recyclerlistview/blob/master/src/core/dependencies/DataProvider.ts

Inherit DataProvider and override getDataForIndex(). Start using immutable collection instead of array. Let me know if you need further help.

@punksta
Copy link
Author

punksta commented Dec 12, 2017

Worked, thanks.
this data: any[] stopped me. Maybe better to have an interface and then make existing DataProvider basic implementation.

@punksta punksta closed this as completed Dec 12, 2017
@black7375
Copy link

black7375 commented May 1, 2020

@naqvitalha have one question.

I looked at the source to override.

public getDataForIndex(index: number): any {
return this._data[index];
}

To redefine the function, need to access the _data, but it is private.

private _data: any[] = [];

What about creating any[], any types to interfaces in generics, abstract functions?

@wxsms
Copy link

wxsms commented May 15, 2020

@black7375 Use getAllData instead.

@black7375
Copy link

black7375 commented May 15, 2020

@wxsms
I asked because I couldn't write a data type like List<T> because of the type of _data(any[]), but I solved the problem by creating it again exclude interface.

Like this.

export abstract class GenericDataProvider<T, K = keyof T> extends BaseDataProvider{
  public rowHasChanged: (r1: T | any, r2: T | any) => boolean;

  // In JS context make sure stable id is a string
  public getStableId: (index: number) => string;
  protected m_firstIndexToProcess: number = 0;
  protected m_size: number = 0;
  protected m_data: K | any = List<T>([]);
  protected m_hasStableIds = false;
  protected m_requiresDataChangeHandling = false;

  constructor(rowHasChanged: (r1: T | any, r2: T | any) => boolean,
              getStableId?:  (index: number) => string) {
    super(rowHasChanged, getStableId);
    this.rowHasChanged = rowHasChanged;
    if (getStableId) {
      this.getStableId = getStableId;
      this.m_hasStableIds = true;
    } else {
      this.getStableId = (index) => index.toString();
    }
  }

  public abstract newInstance(
    rowHasChanged: (r1: T, r2: T) => boolean,
    getStableId?: (index: number) => string    ): GenericDataProvider<T, K>;
  public abstract getDataForIndex(index: number): T | undefined;
  public abstract cloneWithRows(newData: K | any,
                    firstModifiedIndex?: number): DataProvider;

  public getAllData(): K | any {
    return this.m_data;
  }
...

@wxsms
Copy link

wxsms commented May 15, 2020

@punksta Hi, could you pls show your solution here? I've tried the solution provided by @naqvitalha , by it doesn't work. It just show a blank screen without any errors. Thank you in advance!

@wxsms
Copy link

wxsms commented May 19, 2020

Never mind. found out that I have to override not only getDataForIndex , but also newInstance and cloneWithRows. and should extends BaseDataProvider, not DataProvider. This is not frinedly enough 😢 . But anyway, works now.

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

No branches or pull requests

4 participants