2020-10-27 11:33:21 +00:00
|
|
|
package awais.instagrabber.asyncs;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import awais.instagrabber.customviews.helpers.PostFetcher;
|
|
|
|
import awais.instagrabber.interfaces.FetchListener;
|
2021-01-07 12:36:33 +00:00
|
|
|
import awais.instagrabber.repositories.responses.Media;
|
2020-10-27 11:33:21 +00:00
|
|
|
import awais.instagrabber.repositories.responses.PostsFetchResponse;
|
2021-01-07 12:36:33 +00:00
|
|
|
import awais.instagrabber.repositories.responses.User;
|
2020-12-20 18:35:16 +00:00
|
|
|
import awais.instagrabber.webservices.GraphQLService;
|
2020-10-27 11:33:21 +00:00
|
|
|
import awais.instagrabber.webservices.ProfileService;
|
|
|
|
import awais.instagrabber.webservices.ServiceCallback;
|
|
|
|
|
|
|
|
public class ProfilePostFetchService implements PostFetcher.PostFetchService {
|
|
|
|
private static final String TAG = "ProfilePostFetchService";
|
|
|
|
private final ProfileService profileService;
|
2020-12-20 18:35:16 +00:00
|
|
|
private final GraphQLService graphQLService;
|
2021-01-07 12:36:33 +00:00
|
|
|
private final User profileModel;
|
2020-12-20 18:35:16 +00:00
|
|
|
private final boolean isLoggedIn;
|
|
|
|
private String nextMaxId;
|
|
|
|
private boolean moreAvailable;
|
2020-10-27 11:33:21 +00:00
|
|
|
|
2021-01-07 12:36:33 +00:00
|
|
|
public ProfilePostFetchService(final User profileModel, final boolean isLoggedIn) {
|
2020-10-27 11:33:21 +00:00
|
|
|
this.profileModel = profileModel;
|
2020-12-20 18:35:16 +00:00
|
|
|
this.isLoggedIn = isLoggedIn;
|
|
|
|
graphQLService = isLoggedIn ? null : GraphQLService.getInstance();
|
|
|
|
profileService = isLoggedIn ? ProfileService.getInstance() : null;
|
2020-10-27 11:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-07 12:36:33 +00:00
|
|
|
public void fetch(final FetchListener<List<Media>> fetchListener) {
|
|
|
|
final ServiceCallback<PostsFetchResponse> cb = new ServiceCallback<PostsFetchResponse>() {
|
2020-10-27 11:33:21 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final PostsFetchResponse result) {
|
|
|
|
if (result == null) return;
|
2020-12-20 18:35:16 +00:00
|
|
|
nextMaxId = result.getNextCursor();
|
|
|
|
moreAvailable = result.hasNextPage();
|
2020-10-27 11:33:21 +00:00
|
|
|
if (fetchListener != null) {
|
|
|
|
fetchListener.onResult(result.getFeedModels());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
// Log.e(TAG, "onFailure: ", t);
|
|
|
|
if (fetchListener != null) {
|
|
|
|
fetchListener.onFailure(t);
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 18:35:16 +00:00
|
|
|
};
|
2021-01-07 12:36:33 +00:00
|
|
|
if (isLoggedIn) profileService.fetchPosts(profileModel.getPk(), nextMaxId, cb);
|
2021-03-22 01:35:18 +00:00
|
|
|
else graphQLService.fetchProfilePosts(profileModel.getPk(), 30, nextMaxId, profileModel, cb);
|
2020-10-27 11:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 06:02:54 +00:00
|
|
|
public void reset() {
|
2020-12-20 18:35:16 +00:00
|
|
|
nextMaxId = null;
|
2020-10-27 11:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNextPage() {
|
2020-12-20 18:35:16 +00:00
|
|
|
return moreAvailable;
|
2020-10-27 11:33:21 +00:00
|
|
|
}
|
|
|
|
}
|