diff options
8 files changed, 103 insertions, 41 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 92d04927..9d8ea8a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -64,14 +64,14 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivit import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.FetchComment; -import ml.docilealligator.infinityforreddit.FetchPost; +import ml.docilealligator.infinityforreddit.Post.FetchPost; import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.PostCommentSortTypeBottomSheetFragment; -import ml.docilealligator.infinityforreddit.HidePost; +import ml.docilealligator.infinityforreddit.Post.HidePost; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; -import ml.docilealligator.infinityforreddit.ParsePost; +import ml.docilealligator.infinityforreddit.Post.ParsePost; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java index 9f0c93ba..ccbec648 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java @@ -1,10 +1,10 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.Post; import androidx.annotation.NonNull; import java.util.Locale; -import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/HidePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java index 39f6a2fb..ab031fa5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/HidePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java @@ -1,10 +1,11 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.Post; import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; +import ml.docilealligator.infinityforreddit.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java index 3a4845e1..ba59f855 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.Post; import android.os.AsyncTask; import android.text.Html; @@ -8,12 +8,11 @@ import org.json.JSONException; import org.json.JSONObject; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Calendar; +import java.util.LinkedHashSet; import java.util.Locale; import ml.docilealligator.infinityforreddit.Fragment.PostFragment; -import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; @@ -357,7 +356,7 @@ public class ParsePost { } public interface ParsePostsListingListener { - void onParsePostsListingSuccess(ArrayList<Post> newPostData, String lastItem); + void onParsePostsListingSuccess(LinkedHashSet<Post> newPostData, String lastItem); void onParsePostsListingFail(); } @@ -376,7 +375,7 @@ public class ParsePost { private boolean nsfw; private ParsePostsListingListener parsePostsListingListener; private ParsePostListener parsePostListener; - private ArrayList<Post> newPosts; + private LinkedHashSet<Post> newPosts; private Post post; private String lastItem; private boolean parseFailed; @@ -392,7 +391,7 @@ public class ParsePost { this.nPosts = nPosts; this.filter = filter; this.nsfw = nsfw; - newPosts = new ArrayList<>(); + newPosts = new LinkedHashSet<>(); parseFailed = false; } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index d8b98ad8..eccc4388 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -3,6 +3,8 @@ package ml.docilealligator.infinityforreddit.Post; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.Nullable; + import ml.docilealligator.infinityforreddit.Utils.RedditUtils; /** @@ -506,4 +508,12 @@ public class Post implements Parcelable { parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeString(crosspostParentId); } + + @Override + public boolean equals(@Nullable Object obj) { + if (!(obj instanceof Post)) { + return false; + } + return ((Post) obj).getFullName().equals(fullName); + } }
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java index 19536c47..e8e865b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -5,10 +5,11 @@ import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.ParsePost; import ml.docilealligator.infinityforreddit.RedditAPI; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; @@ -42,6 +43,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private int filter; private String userWhere; private String multiRedditPath; + private LinkedHashSet<Post> postLinkedHashSet; private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData; @@ -62,6 +64,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType; this.filter = filter; this.nsfw = nsfw; + postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String path, int postType, @@ -97,6 +100,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } this.filter = filter; this.nsfw = nsfw; + postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, @@ -113,6 +117,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { userWhere = where; this.filter = filter; this.nsfw = nsfw; + postLinkedHashSet = new LinkedHashSet<>(); } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, @@ -129,6 +134,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.sortType = sortType == null ? new SortType(SortType.Type.RELEVANCE) : sortType; this.filter = filter; this.nsfw = nsfw; + postLinkedHashSet = new LinkedHashSet<>(); } MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() { @@ -234,7 +240,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { String nextPageKey; if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { nextPageKey = null; @@ -242,14 +248,17 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { nextPageKey = lastItem; } + int currentPostsSize = postLinkedHashSet.size(); if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(true); } else if (nextPageKey != null) { loadBestPostsInitial(callback, nextPageKey); return; } else { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(false); } @@ -296,11 +305,18 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { loadBestPostsAfter(params, callback, lastItem); } else { - callback.onResult(newPosts, lastItem); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + callback.onResult(new ArrayList<>(), lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } } @@ -366,7 +382,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { String nextPageKey; if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { nextPageKey = null; @@ -375,13 +391,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(true); } else if (nextPageKey != null) { loadSubredditPostsInitial(callback, nextPageKey); return; } else { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(false); } @@ -439,11 +457,18 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { loadSubredditPostsAfter(params, callback, lastItem); } else { - callback.onResult(newPosts, lastItem); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + callback.onResult(new ArrayList<>(), lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } } @@ -493,7 +518,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { String nextPageKey; if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { nextPageKey = null; @@ -502,13 +527,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(true); } else if (nextPageKey != null) { loadUserPostsInitial(callback, nextPageKey); return; } else { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(false); } @@ -562,11 +589,18 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { loadUserPostsAfter(params, callback, lastItem); } else { - callback.onResult(newPosts, lastItem); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + callback.onResult(new ArrayList<>(), lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } } @@ -638,7 +672,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { String nextPageKey; if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { nextPageKey = null; @@ -647,13 +681,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(true); } else if (nextPageKey != null) { loadSearchPostsInitial(callback, nextPageKey); return; } else { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(false); } @@ -727,11 +763,18 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { loadSearchPostsAfter(params, callback, lastItem); } else { - callback.onResult(newPosts, lastItem); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + callback.onResult(new ArrayList<>(), lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } } @@ -780,7 +823,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { String nextPageKey; if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { nextPageKey = null; @@ -789,13 +832,15 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } if (newPosts.size() != 0) { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(true); } else if (nextPageKey != null) { loadMultiRedditPostsInitial(callback, nextPageKey); return; } else { - callback.onResult(newPosts, null, nextPageKey); + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); hasPostLiveData.postValue(false); } @@ -849,11 +894,18 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override - public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { loadMultiRedditPostsAfter(params, callback, lastItem); } else { - callback.onResult(newPosts, lastItem); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + callback.onResult(new ArrayList<>(), lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java index 6a93edf1..e7479e36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.Post; import android.graphics.Bitmap; import android.os.AsyncTask; @@ -20,7 +20,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; -import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.RedditUtils; import okhttp3.MediaType; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java index cfce40c8..cedf9730 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java @@ -39,7 +39,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NotificationUtils; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.SubmitPost; +import ml.docilealligator.infinityforreddit.Post.SubmitPost; import retrofit2.Retrofit; public class SubmitPostService extends Service { |