BarInsta/app/src/main/java/awais/instagrabber/asyncs/LocationPostFetchService.java

76 lines
2.8 KiB
Java
Raw Normal View History

2020-11-01 12:56:04 +00:00
package awais.instagrabber.asyncs;
import java.util.List;
import awais.instagrabber.customviews.helpers.PostFetcher;
import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.repositories.responses.Location;
import awais.instagrabber.repositories.responses.Media;
2020-12-20 18:35:16 +00:00
import awais.instagrabber.repositories.responses.PostsFetchResponse;
import awais.instagrabber.utils.CoroutineUtilsKt;
import awais.instagrabber.webservices.GraphQLRepository;
2020-11-01 12:56:04 +00:00
import awais.instagrabber.webservices.LocationService;
import awais.instagrabber.webservices.ServiceCallback;
import kotlinx.coroutines.Dispatchers;
2020-11-01 12:56:04 +00:00
public class LocationPostFetchService implements PostFetcher.PostFetchService {
private final LocationService locationService;
private final GraphQLRepository graphQLRepository;
private final Location locationModel;
2020-11-01 12:56:04 +00:00
private String nextMaxId;
private boolean moreAvailable;
private final boolean isLoggedIn;
2020-11-01 12:56:04 +00:00
public LocationPostFetchService(final Location locationModel, final boolean isLoggedIn) {
2020-11-01 12:56:04 +00:00
this.locationModel = locationModel;
this.isLoggedIn = isLoggedIn;
2020-12-20 18:35:16 +00:00
locationService = isLoggedIn ? LocationService.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
2020-11-01 12:56:04 +00:00
}
@Override
public void fetch(final FetchListener<List<Media>> fetchListener) {
final ServiceCallback<PostsFetchResponse> cb = new ServiceCallback<PostsFetchResponse>() {
2020-11-01 12:56:04 +00:00
@Override
2020-12-20 18:35:16 +00:00
public void onSuccess(final PostsFetchResponse result) {
2020-11-01 12:56:04 +00:00
if (result == null) return;
2020-12-20 18:35:16 +00:00
nextMaxId = result.getNextCursor();
2021-06-29 14:12:07 +00:00
moreAvailable = result.getHasNextPage();
2020-11-01 12:56:04 +00:00
if (fetchListener != null) {
2020-12-20 18:35:16 +00:00
fetchListener.onResult(result.getFeedModels());
2020-11-01 12:56:04 +00:00
}
}
@Override
public void onFailure(final Throwable t) {
// Log.e(TAG, "onFailure: ", t);
if (fetchListener != null) {
fetchListener.onFailure(t);
}
}
};
if (isLoggedIn) locationService.fetchPosts(locationModel.getPk(), nextMaxId, cb);
else graphQLRepository.fetchLocationPosts(
locationModel.getPk(),
nextMaxId,
CoroutineUtilsKt.getContinuation((postsFetchResponse, throwable) -> {
if (throwable != null) {
cb.onFailure(throwable);
return;
}
cb.onSuccess(postsFetchResponse);
}, Dispatchers.getIO())
);
2020-11-01 12:56:04 +00:00
}
@Override
public void reset() {
nextMaxId = null;
}
@Override
public boolean hasNextPage() {
return moreAvailable;
}
}