0

hope you are not having a bad day.:D

I am retrieving posts from the server with pagination which I have done smoothly using Paging Library. The user can like the post or follow the poster, my problem is that I want to handle the 'follow' or 'like' network API calls knowing the data status -loading, success, or error- at each post inside the PagedList that are viewed by the PagedListAdapter.

I am using the Resource class from Google App Architecture Guide.

public class Resource<T> {

    private final DataStatus status;
    private T data;
    private ErrorStatus errorStatus;

    public Resource(DataStatus status) {
        this.status = status;
    }

    private Resource(DataStatus status, T data) {
        this.status = status;
        this.data = data;
    }

    private Resource(DataStatus status, ErrorStatus errorStatus, T data) {
        this.status = status;
        this.data = data;
        this.errorStatus = errorStatus;
    }

    public T getData() {
        return data;
    }

    public DataStatus getStatus() {
        return status;
    }

    static <T> Resource<T> loading(T data) {
        return new Resource<T>(DataStatus.LOADING, data);
    }

    public static <T> Resource<T> success(T data) {
        return new Resource<>(DataStatus.SUCCESS, data);
    }

    static <T> Resource<T> error(ErrorStatus errorStatus, T data) {
        return new Resource<>(DataStatus.ERROR, errorStatus, data);
    }
}

Thanks a lot.

AMosa3d
  • 1
  • 2
  • Could you clarify what you mean by handle the network API call? What exactly do you want to do? I'm having trouble connecting the dots between these separate APIs to DataSource load state. If you're trying to update an item, you should write to a backing db / cache and call DataSource.invalidate(). If you need to access DataSource load state, the Paging2 APIs don't have this concept built in so you need to track it yourself. Paging3 (which is in alpha) has LoadState built-in which you can observe with an adapter API. – dlam Aug 19 '20 at 09:10
  • I guess I am trying to update an item and to be able to view a small progressbar inside the viewHolder until the update has been applied back at the server. :D – AMosa3d Aug 20 '20 at 13:55
  • Unfortunately, the only way to update an individual item at the moment is to update backing dataset and DataSource.invalidate. This will trigger a refresh, but DiffUtil should handle things smoothly on the UI and should generally be performant enough even though it seems like a lot. Paging3 is working on better support for granular item updates which you can follow the progress for here: https://issuetracker.google.com/issues/149482600 – dlam Aug 20 '20 at 22:18
  • @dlam I am really very thankful :D – AMosa3d Aug 21 '20 at 12:48
  • Thanks! Support from the community really helps, glad paging is of use to you! – dlam Aug 22 '20 at 09:08

0 Answers0