From a1df8f4d3855afe7b2060a8e0d151bf2847113b6 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Fri, 18 Nov 2022 15:47:27 +0700 Subject: Sort type converters (#1211) * Create and add to Retrofit SortTypeConverterFactory * Clean up SortType class Mark fields as final, annotate nullability * Use SortType enums as query arguments * Use optional query arguments to simplify code Retrofit ignores null values, so there is no need to remove them from arguments manually --- .../infinityforreddit/AppModule.java | 4 + .../infinityforreddit/SortType.java | 17 ++-- .../infinityforreddit/apis/RedditAPI.java | 96 ++++++--------------- .../comment/CommentDataSource.java | 54 +++--------- .../network/SortTypeConverter.java | 29 +++++++ .../network/SortTypeConverterFactory.java | 30 +++++++ .../infinityforreddit/post/PostPagingSource.java | 99 +++++----------------- 7 files changed, 135 insertions(+), 194 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverterFactory.java (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 1b709651..d38f04a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -22,6 +22,7 @@ import dagger.Module; import dagger.Provides; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LoopAvailableExoCreator; +import ml.docilealligator.infinityforreddit.network.SortTypeConverterFactory; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -50,6 +51,7 @@ class AppModule { return new Retrofit.Builder() .baseUrl(APIUtils.OAUTH_API_BASE_URI) .client(okHttpClient) + .addConverterFactory(SortTypeConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create()) .addCallAdapterFactory(GuavaCallAdapterFactory.create()) .build(); @@ -61,6 +63,7 @@ class AppModule { Retrofit provideOauthWithoutAuthenticatorRetrofit() { return new Retrofit.Builder() .baseUrl(APIUtils.OAUTH_API_BASE_URI) + .addConverterFactory(SortTypeConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } @@ -71,6 +74,7 @@ class AppModule { Retrofit provideRetrofit() { return new Retrofit.Builder() .baseUrl(APIUtils.API_BASE_URI) + .addConverterFactory(SortTypeConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create()) .addCallAdapterFactory(GuavaCallAdapterFactory.create()) .build(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java index be4b1b89..537a5292 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortType.java @@ -1,23 +1,30 @@ package ml.docilealligator.infinityforreddit; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + public class SortType { - private Type type; - private Time time; + @NonNull + private final Type type; + @Nullable + private final Time time; - public SortType(Type type) { - this.type = type; + public SortType(@NonNull Type type) { + this(type, null); } - public SortType(Type type, Time time) { + public SortType(@NonNull Type type, @Nullable Time time) { this.type = type; this.time = time; } + @NonNull public Type getType() { return type; } + @Nullable public Time getTime() { return time; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 705e8490..caada0cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -4,6 +4,7 @@ import com.google.common.util.concurrent.ListenableFuture; import java.util.Map; +import ml.docilealligator.infinityforreddit.SortType; import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; @@ -58,30 +59,30 @@ public interface RedditAPI { @GET("user/{username}/comments.json?raw_json=1") Call getUserComments(@Path("username") String username, @Query("after") String after, - @Query("sort") String sortType); + @Query("sort") SortType.Type sortType); @GET("user/{username}/comments.json?raw_json=1") Call getUserComments(@Path("username") String username, @Query("after") String after, - @Query("sort") String sortType, @Query("t") String sortTime); + @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); @GET("user/{username}/comments.json?raw_json=1") Call getUserCommentsOauth(@HeaderMap Map headers, @Path("username") String username, - @Query("after") String after, @Query("sort") String sortType); + @Query("after") String after, @Query("sort") SortType.Type sortType); @GET("user/{username}/comments.json?raw_json=1") Call getUserCommentsOauth(@HeaderMap Map headers, @Path("username") String username, - @Query("after") String after, @Query("sort") String sortType, - @Query("t") String sortTime); + @Query("after") String after, @Query("sort") SortType.Type sortType, + @Query("t") SortType.Time sortTime); @GET("user/{username}/{where}.json?&type=comments&raw_json=1&limit=25") Call getUserSavedCommentsOauth(@Path("username") String username, @Path("where") String where, - @Query("after") String lastItem, @Query("sort") String sortType, + @Query("after") String lastItem, @Query("sort") SortType.Type sortType, @HeaderMap Map headers); @GET("user/{username}/{where}.json?&type=comments&raw_json=1&limit=25") Call getUserSavedCommentsOauth(@Path("username") String username, @Path("where") String where, - @Query("after") String lastItem, @Query("sort") String sortType, - @Query("t") String sortTime, @HeaderMap Map headers); + @Query("after") String lastItem, @Query("sort") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @HeaderMap Map headers); @FormUrlEncoded @POST("api/subscribe") @@ -282,105 +283,58 @@ public interface RedditAPI { Call getWikiPage(@Path("subredditName") String subredditName, @Path("wikiPage") String wikiPage); @GET("{sortType}?raw_json=1") - ListenableFuture> getBestPostsListenableFuture(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map headers); - - @GET("{sortType}?raw_json=1") - ListenableFuture> getBestPostsListenableFuture(@Path("sortType") String sortType, @Query("t") String sortTime, + ListenableFuture> getBestPostsListenableFuture(@Path("sortType") SortType.Type sortType, @Query("t") SortType.Time sortTime, @Query("after") String lastItem, @HeaderMap Map headers); @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25&always_show_media=1") - ListenableFuture> getSubredditBestPostsOauthListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") String sortType, - @Query("after") String lastItem, @HeaderMap Map headers); - - @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25&always_show_media=1") - ListenableFuture> getSubredditBestPostsOauthListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") String sortType, - @Query("t") String sortTime, @Query("after") String lastItem, + ListenableFuture> getSubredditBestPostsOauthListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @Query("after") String lastItem, @HeaderMap Map headers); @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25&always_show_media=1") - ListenableFuture> getSubredditBestPostsListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") String sortType, - @Query("after") String lastItem); - - @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=25&always_show_media=1") - ListenableFuture> getSubredditBestPostsListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") String sortType, - @Query("t") String sortTime, @Query("after") String lastItem); - - @GET("user/{username}/{where}.json?&type=links&raw_json=1&limit=25") - ListenableFuture> getUserPostsOauthListenableFuture(@Path("username") String username, @Path("where") String where, - @Query("after") String lastItem, @Query("sort") String sortType, @HeaderMap Map headers); + ListenableFuture> getSubredditBestPostsListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @Query("after") String lastItem); @GET("user/{username}/{where}.json?&type=links&raw_json=1&limit=25") ListenableFuture> getUserPostsOauthListenableFuture(@Path("username") String username, @Path("where") String where, - @Query("after") String lastItem, @Query("sort") String sortType, - @Query("t") String sortTime, @HeaderMap Map headers); - - @GET("user/{username}/submitted.json?raw_json=1&limit=25") - ListenableFuture> getUserPostsListenableFuture(@Path("username") String username, @Query("after") String lastItem, - @Query("sort") String sortType); + @Query("after") String lastItem, @Query("sort") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @HeaderMap Map headers); @GET("user/{username}/submitted.json?raw_json=1&limit=25") ListenableFuture> getUserPostsListenableFuture(@Path("username") String username, @Query("after") String lastItem, - @Query("sort") String sortType, @Query("t") String sortTime); - - @GET("search.json?include_over_18=1&raw_json=1&type=link") - ListenableFuture> searchPostsOauthListenableFuture(@Query("q") String query, @Query("after") String after, - @Query("sort") String sort, @Query("source") String source, - @HeaderMap Map headers); + @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); @GET("search.json?include_over_18=1&raw_json=1&type=link") ListenableFuture> searchPostsOauthListenableFuture(@Query("q") String query, @Query("after") String after, - @Query("sort") String sort, @Query("t") String sortTime, + @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("source") String source, @HeaderMap Map headers); @GET("search.json?include_over_18=1&raw_json=1&type=link") ListenableFuture> searchPostsListenableFuture(@Query("q") String query, @Query("after") String after, - @Query("sort") String sort, @Query("source") String source); - - @GET("search.json?include_over_18=1&raw_json=1&type=link") - ListenableFuture> searchPostsListenableFuture(@Query("q") String query, @Query("after") String after, - @Query("sort") String sort, @Query("t") String sortTime, + @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("source") String source); @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&type=link&restrict_sr=true") ListenableFuture> searchPostsInSpecificSubredditOauthListenableFuture(@Path("subredditName") String subredditName, - @Query("q") String query, @Query("sort") String sort, - @Query("after") String after, - @HeaderMap Map headers); - - @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&type=link&restrict_sr=true") - ListenableFuture> searchPostsInSpecificSubredditOauthListenableFuture(@Path("subredditName") String subredditName, - @Query("q") String query, @Query("sort") String sort, - @Query("t") String sortTime, @Query("after") String after, + @Query("q") String query, @Query("sort") SortType.Type sort, + @Query("t") SortType.Time sortTime, @Query("after") String after, @HeaderMap Map headers); @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&type=link&restrict_sr=true") ListenableFuture> searchPostsInSpecificSubredditListenableFuture(@Path("subredditName") String subredditName, - @Query("q") String query, @Query("sort") String sort, - @Query("after") String after); - - @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&type=link&restrict_sr=true") - ListenableFuture> searchPostsInSpecificSubredditListenableFuture(@Path("subredditName") String subredditName, - @Query("q") String query, @Query("sort") String sort, - @Query("t") String sortTime, @Query("after") String after); - - @GET("{multipath}?raw_json=1") - ListenableFuture> getMultiRedditPostsListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, - @Query("after") String after); + @Query("q") String query, @Query("sort") SortType.Type sort, + @Query("t") SortType.Time sortTime, @Query("after") String after); @GET("{multipath}?raw_json=1") ListenableFuture> getMultiRedditPostsListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, - @Query("after") String after, @Query("t") String sortTime); + @Query("after") String after, @Query("t") SortType.Time sortTime); @GET("{multipath}.json?raw_json=1") ListenableFuture> getMultiRedditPostsOauthListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, - @Query("after") String after, @Query("t") String sortTime, + @Query("after") String after, @Query("t") SortType.Time sortTime, @HeaderMap Map headers); - @GET("{multipath}.json?raw_json=1") - ListenableFuture> getMultiRedditPostsOauthListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, - @Query("after") String after, @HeaderMap Map headers); - @GET("{sortType}?raw_json=1") Call getBestPosts(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map headers); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java index 5ebd49e0..09758d33 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java @@ -78,30 +78,16 @@ public class CommentDataSource extends PageKeyedDataSource { RedditAPI api = retrofit.create(RedditAPI.class); Call commentsCall; if (areSavedComments) { - if (sortType.getTime() != null) { - commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, - null, sortType.getType().value, sortType.getTime().value, - APIUtils.getOAuthHeader(accessToken)); - } else { - commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, - null, sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); - } + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, + null, sortType.getType(), sortType.getTime(), + APIUtils.getOAuthHeader(accessToken)); } else { if (accessToken == null) { - if (sortType.getTime() != null) { - commentsCall = api.getUserComments(username, null, sortType.getType().value, - sortType.getTime().value); - } else { - commentsCall = api.getUserComments(username, null, sortType.getType().value); - } + commentsCall = api.getUserComments(username, null, sortType.getType(), + sortType.getTime()); } else { - if (sortType.getTime() != null) { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, - null, sortType.getType().value, sortType.getTime().value); - } else { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, - null, sortType.getType().value); - } + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, + null, sortType.getType(), sortType.getTime()); } } commentsCall.enqueue(new Callback() { @@ -157,29 +143,15 @@ public class CommentDataSource extends PageKeyedDataSource { RedditAPI api = retrofit.create(RedditAPI.class); Call commentsCall; if (areSavedComments) { - if (sortType.getTime() != null) { - commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, - sortType.getType().value, sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, - sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); - } + commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, + sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } else { if (accessToken == null) { - if (sortType.getTime() != null) { - commentsCall = api.getUserComments(username, params.key, sortType.getType().value, - sortType.getTime().value); - } else { - commentsCall = api.getUserComments(username, params.key, sortType.getType().value); - } + commentsCall = api.getUserComments(username, params.key, sortType.getType(), + sortType.getTime()); } else { - if (sortType.getTime() != null) { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), - username, params.key, sortType.getType().value, sortType.getTime().value); - } else { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), - username, params.key, sortType.getType().value); - } + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), + username, params.key, sortType.getType(), sortType.getTime()); } } commentsCall.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverter.java new file mode 100644 index 00000000..b69f9da6 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverter.java @@ -0,0 +1,29 @@ +package ml.docilealligator.infinityforreddit.network; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.io.IOException; + +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Converter; + +/** + * A {@link Converter} for {@link SortType.Type sort type} and {@link SortType.Time sort time} to + * {@link String} parameters + */ +public class SortTypeConverter implements Converter { + /* package */ static SortTypeConverter INSTANCE = new SortTypeConverter<>(); + + @Nullable + @Override + public String convert(@NonNull T value) throws IOException { + if (value instanceof SortType.Type) { + return ((SortType.Type) value).value; + } else if (value instanceof SortType.Time) { + return ((SortType.Time) value).value; + } else { + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverterFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverterFactory.java new file mode 100644 index 00000000..84a1ed6c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/network/SortTypeConverterFactory.java @@ -0,0 +1,30 @@ +package ml.docilealligator.infinityforreddit.network; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import ml.docilealligator.infinityforreddit.SortType; +import retrofit2.Converter; +import retrofit2.Retrofit; + +/** + * A {@link Converter.Factory} for {@link SortType.Type sort type} and {@link SortType.Time sort time} to + * {@link String} parameters + */ +public class SortTypeConverterFactory extends Converter.Factory { + public static SortTypeConverterFactory create() { + return new SortTypeConverterFactory(); + } + + @Nullable + @Override + public Converter stringConverter(@NonNull Type type, @NonNull Annotation[] annotations, @NonNull Retrofit retrofit) { + if (type == SortType.Type.class || type == SortType.Time.class) { + return SortTypeConverter.INSTANCE; + } + return null; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java index 0bf7eefc..bd2b51e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java @@ -214,12 +214,8 @@ public class PostPagingSource extends ListenableFuturePagingSource } else { afterKey = loadParams.getKey(); } - if (sortType.getTime() != null) { - bestPost = api.getBestPostsListenableFuture(sortType.getType().value, sortType.getTime().value, afterKey, - APIUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPostsListenableFuture(sortType.getType().value, afterKey, APIUtils.getOAuthHeader(accessToken)); - } + bestPost = api.getBestPostsListenableFuture(sortType.getType(), sortType.getTime(), afterKey, + APIUtils.getOAuthHeader(accessToken)); ListenableFuture> pageFuture = Futures.transform(bestPost, this::transformData, executor); @@ -234,19 +230,10 @@ public class PostPagingSource extends ListenableFuturePagingSource private ListenableFuture> loadSubredditPosts(@NonNull LoadParams loadParams, RedditAPI api) { ListenableFuture> subredditPost; if (accessToken == null) { - if (sortType.getTime() != null) { - subredditPost = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType().value, sortType.getTime().value, loadParams.getKey()); - } else { - subredditPost = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType().value, loadParams.getKey()); - } + subredditPost = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType(), sortType.getTime(), loadParams.getKey()); } else { - if (sortType.getTime() != null) { - subredditPost = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); - } else { - subredditPost = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, sortType.getType().value, - loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); - } + subredditPost = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, sortType.getType(), + sortType.getTime(), loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); } ListenableFuture> pageFuture = Futures.transform(subredditPost, this::transformData, executor); @@ -262,20 +249,11 @@ public class PostPagingSource extends ListenableFuturePagingSource private ListenableFuture> loadUserPosts(@NonNull LoadParams loadParams, RedditAPI api) { ListenableFuture> userPosts; if (accessToken == null) { - if (sortType.getTime() != null) { - userPosts = api.getUserPostsListenableFuture(subredditOrUserName, loadParams.getKey(), sortType.getType().value, - sortType.getTime().value); - } else { - userPosts = api.getUserPostsListenableFuture(subredditOrUserName, loadParams.getKey(), sortType.getType().value); - } + userPosts = api.getUserPostsListenableFuture(subredditOrUserName, loadParams.getKey(), sortType.getType(), + sortType.getTime()); } else { - if (sortType.getTime() != null) { - userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, userWhere, loadParams.getKey(), sortType.getType().value, - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, userWhere, loadParams.getKey(), sortType.getType().value, - APIUtils.getOAuthHeader(accessToken)); - } + userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, userWhere, loadParams.getKey(), sortType.getType(), + sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } ListenableFuture> pageFuture = Futures.transform(userPosts, this::transformData, executor); @@ -292,40 +270,20 @@ public class PostPagingSource extends ListenableFuturePagingSource ListenableFuture> searchPosts; if (subredditOrUserName == null) { if (accessToken == null) { - if (sortType.getTime() != null) { - searchPosts = api.searchPostsListenableFuture(query, loadParams.getKey(), sortType.getType().value, sortType.getTime().value, - trendingSource); - } else { - searchPosts = api.searchPostsListenableFuture(query, loadParams.getKey(), sortType.getType().value, trendingSource); - } + searchPosts = api.searchPostsListenableFuture(query, loadParams.getKey(), sortType.getType(), sortType.getTime(), + trendingSource); } else { - if (sortType.getTime() != null) { - searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType().value, - sortType.getTime().value, trendingSource, APIUtils.getOAuthHeader(accessToken)); - } else { - searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType().value, trendingSource, - APIUtils.getOAuthHeader(accessToken)); - } + searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType(), + sortType.getTime(), trendingSource, APIUtils.getOAuthHeader(accessToken)); } } else { if (accessToken == null) { - if (sortType.getTime() != null) { - searchPosts = api.searchPostsInSpecificSubredditListenableFuture(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, loadParams.getKey()); - } else { - searchPosts = api.searchPostsInSpecificSubredditListenableFuture(subredditOrUserName, query, - sortType.getType().value, loadParams.getKey()); - } + searchPosts = api.searchPostsInSpecificSubredditListenableFuture(subredditOrUserName, query, + sortType.getType(), sortType.getTime(), loadParams.getKey()); } else { - if (sortType.getTime() != null) { - searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query, - sortType.getType().value, sortType.getTime().value, loadParams.getKey(), - APIUtils.getOAuthHeader(accessToken)); - } else { - searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query, - sortType.getType().value, loadParams.getKey(), - APIUtils.getOAuthHeader(accessToken)); - } + searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query, + sortType.getType(), sortType.getTime(), loadParams.getKey(), + APIUtils.getOAuthHeader(accessToken)); } } @@ -342,19 +300,10 @@ public class PostPagingSource extends ListenableFuturePagingSource private ListenableFuture> loadMultiRedditPosts(@NonNull LoadParams loadParams, RedditAPI api) { ListenableFuture> multiRedditPosts; if (accessToken == null) { - if (sortType.getTime() != null) { - multiRedditPosts = api.getMultiRedditPostsListenableFuture(multiRedditPath, loadParams.getKey(), sortType.getTime().value); - } else { - multiRedditPosts = api.getMultiRedditPostsListenableFuture(multiRedditPath, loadParams.getKey()); - } + multiRedditPosts = api.getMultiRedditPostsListenableFuture(multiRedditPath, loadParams.getKey(), sortType.getTime()); } else { - if (sortType.getTime() != null) { - multiRedditPosts = api.getMultiRedditPostsOauthListenableFuture(multiRedditPath, loadParams.getKey(), - sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); - } else { - multiRedditPosts = api.getMultiRedditPostsOauthListenableFuture(multiRedditPath, loadParams.getKey(), - APIUtils.getOAuthHeader(accessToken)); - } + multiRedditPosts = api.getMultiRedditPostsOauthListenableFuture(multiRedditPath, loadParams.getKey(), + sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } ListenableFuture> pageFuture = Futures.transform(multiRedditPosts, this::transformData, executor); @@ -369,11 +318,7 @@ public class PostPagingSource extends ListenableFuturePagingSource private ListenableFuture> loadAnonymousHomePosts(@NonNull LoadParams loadParams, RedditAPI api) { ListenableFuture> anonymousHomePosts; - if (sortType.getTime() != null) { - anonymousHomePosts = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType().value, sortType.getTime().value, loadParams.getKey()); - } else { - anonymousHomePosts = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType().value, loadParams.getKey()); - } + anonymousHomePosts = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType(), sortType.getTime(), loadParams.getKey()); ListenableFuture> pageFuture = Futures.transform(anonymousHomePosts, this::transformData, executor); -- cgit v1.2.3