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

95 lines
3.5 KiB
Java
Raw Normal View History

2020-11-02 13:00:07 +00:00
package awais.instagrabber.asyncs;
import java.util.List;
import awais.instagrabber.customviews.helpers.PostFetcher;
import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.enums.PostItemType;
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-02 13:00:07 +00:00
import awais.instagrabber.webservices.ProfileService;
import awais.instagrabber.webservices.ServiceCallback;
import kotlinx.coroutines.Dispatchers;
2020-11-02 13:00:07 +00:00
public class SavedPostFetchService implements PostFetcher.PostFetchService {
private final ProfileService profileService;
private final GraphQLRepository graphQLRepository;
private final long profileId;
2020-11-02 13:00:07 +00:00
private final PostItemType type;
2020-12-20 18:35:16 +00:00
private final boolean isLoggedIn;
2020-11-02 13:00:07 +00:00
private String nextMaxId;
private final String collectionId;
2020-11-02 13:00:07 +00:00
private boolean moreAvailable;
public SavedPostFetchService(final long profileId, final PostItemType type, final boolean isLoggedIn, final String collectionId) {
2020-11-02 13:00:07 +00:00
this.profileId = profileId;
this.type = type;
2020-12-20 18:35:16 +00:00
this.isLoggedIn = isLoggedIn;
this.collectionId = collectionId;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
2020-12-20 18:35:16 +00:00
profileService = isLoggedIn ? ProfileService.getInstance() : null;
2020-11-02 13:00:07 +00:00
}
@Override
public void fetch(final FetchListener<List<Media>> fetchListener) {
2020-12-20 18:35:16 +00:00
final ServiceCallback<PostsFetchResponse> callback = new ServiceCallback<PostsFetchResponse>() {
2020-11-02 13:00:07 +00:00
@Override
2020-12-20 18:35:16 +00:00
public void onSuccess(final PostsFetchResponse result) {
2020-11-02 13:00:07 +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-02 13:00:07 +00:00
if (fetchListener != null) {
2020-12-20 18:35:16 +00:00
fetchListener.onResult(result.getFeedModels());
2020-11-02 13:00:07 +00:00
}
}
@Override
public void onFailure(final Throwable t) {
// Log.e(TAG, "onFailure: ", t);
if (fetchListener != null) {
fetchListener.onFailure(t);
}
}
};
switch (type) {
case LIKED:
profileService.fetchLiked(nextMaxId, callback);
break;
case TAGGED:
2020-12-20 18:35:16 +00:00
if (isLoggedIn) profileService.fetchTagged(profileId, nextMaxId, callback);
else graphQLRepository.fetchTaggedPosts(
profileId,
30,
nextMaxId,
CoroutineUtilsKt.getContinuation((postsFetchResponse, throwable) -> {
if (throwable != null) {
callback.onFailure(throwable);
return;
}
callback.onSuccess(postsFetchResponse);
}, Dispatchers.getIO())
);
2020-11-02 13:00:07 +00:00
break;
case COLLECTION:
2020-11-02 13:00:07 +00:00
case SAVED:
profileService.fetchSaved(nextMaxId, collectionId, callback);
2020-11-02 13:00:07 +00:00
break;
default:
callback.onFailure(null);
2020-11-02 13:00:07 +00:00
}
}
@Override
public void reset() {
nextMaxId = null;
}
@Override
public boolean hasNextPage() {
return moreAvailable;
}
}