diff --git a/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestWPCom.java b/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestWPCom.java index c630a0c3cf..cec3818fac 100644 --- a/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestWPCom.java +++ b/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestWPCom.java @@ -156,6 +156,7 @@ public void testInstantiateAndCreateNewComment() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); } @@ -174,6 +175,7 @@ public void testLikeAndUnlikeComment() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(firstComment.getId()); + assertNotNull(comment); assertTrue(comment.getILike()); // Unlike comment @@ -185,6 +187,7 @@ public void testLikeAndUnlikeComment() throws InterruptedException { // Check comment has been modified in the DB comment = mCommentStore.getCommentByLocalId(firstComment.getId()); + assertNotNull(comment); assertFalse(comment.getILike()); } @@ -205,6 +208,7 @@ public void testDeleteCommentOnce() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); // Delete @@ -216,6 +220,7 @@ public void testDeleteCommentOnce() throws InterruptedException { // Make sure the comment is still here but state changed comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertEquals(CommentStatus.TRASH.toString(), comment.getStatus()); } @@ -236,6 +241,7 @@ public void testDeleteCommentTwice() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); // Delete once (ie. move to trash) @@ -326,6 +332,7 @@ public void testInstantiateAndCreateReplyComment() throws InterruptedException { CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); // Using .contains() in the assert below because server might wrap the response in

+ assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); assertNotSame(mNewComment.getRemoteCommentId(), comment.getRemoteCommentId()); assertNotSame(mNewComment.getRemoteSiteId(), comment.getRemoteSiteId()); @@ -415,6 +422,7 @@ public void testCommentResponseContainsURL() throws InterruptedException { // Check the new comment response contains URL CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertNotNull(comment.getUrl()); } diff --git a/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestXMLRPC.java b/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestXMLRPC.java index 238075c8d9..e06481f2cd 100644 --- a/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestXMLRPC.java +++ b/example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_CommentTestXMLRPC.java @@ -109,6 +109,7 @@ public void testInstantiateAndCreateNewComment() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); } @@ -172,6 +173,7 @@ public void testInstantiateAndCreateReplyComment() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertEquals(comment.getContent(), mNewComment.getContent()); assertEquals(comment.getAuthorId(), firstComment.getAuthorId()); assertEquals(comment.getParentId(), firstComment.getRemoteCommentId()); @@ -196,6 +198,7 @@ public void testEditValidComment() throws InterruptedException { assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); CommentModel comment = mCommentStore.getCommentByLocalId(firstComment.getId()); + assertNotNull(comment); assertEquals(comment.getContent(), firstComment.getContent()); } @@ -235,6 +238,7 @@ public void testDeleteCommentOnce() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); // Delete @@ -246,6 +250,7 @@ public void testDeleteCommentOnce() throws InterruptedException { // Make sure the comment is still here but state changed comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertEquals(CommentStatus.TRASH.toString(), comment.getStatus()); } @@ -266,6 +271,7 @@ public void testDeleteCommentTwice() throws InterruptedException { // Check comment has been modified in the DB CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertTrue(comment.getContent().contains(mNewComment.getContent())); // Delete once (ie. move to trash) @@ -302,6 +308,7 @@ public void testCommentResponseContainsURL() throws InterruptedException { // Check the new comment response contains URL CommentModel comment = mCommentStore.getCommentByLocalId(mNewComment.getId()); + assertNotNull(comment); assertNotNull(comment.getUrl()); } diff --git a/example/src/test/java/org/wordpress/android/fluxc/common/CommentsMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/common/CommentsMapperTest.kt index 4f87b33e79..564422e777 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/common/CommentsMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/common/CommentsMapperTest.kt @@ -161,11 +161,11 @@ class CommentsMapperTest { authorEmail = entity.authorEmail authorProfileImageUrl = entity.authorProfileImageUrl postTitle = entity.postTitle - status = entity.status - datePublished = entity.datePublished + status = entity.status ?: "" + datePublished = entity.datePublished ?: "" publishedTimestamp = entity.publishedTimestamp - content = entity.content - url = entity.authorProfileImageUrl + content = entity.content ?: "" + url = entity.authorProfileImageUrl ?: "" hasParent = entity.hasParent parentId = entity.parentId iLike = entity.iLike diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/model/CommentModel.java b/fluxc/src/main/java/org/wordpress/android/fluxc/model/CommentModel.java index e0993633f3..9d3eac7b12 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/model/CommentModel.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/model/CommentModel.java @@ -1,5 +1,8 @@ package org.wordpress.android.fluxc.model; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import com.yarolegovich.wellsql.core.Identifiable; import com.yarolegovich.wellsql.core.annotation.Column; import com.yarolegovich.wellsql.core.annotation.PrimaryKey; @@ -11,6 +14,7 @@ import java.io.Serializable; @Table +@SuppressWarnings("NotNullFieldNotInitialized") public class CommentModel extends Payload implements Identifiable, Serializable { private static final long serialVersionUID = 3454722213760369852L; @@ -23,19 +27,19 @@ public class CommentModel extends Payload implements Identifia @Column private long mRemoteSiteId; // Comment author - @Column private String mAuthorUrl; - @Column private String mAuthorName; - @Column private String mAuthorEmail; + @Nullable @Column private String mAuthorUrl; + @Nullable @Column private String mAuthorName; + @Nullable @Column private String mAuthorEmail; @Column private long mAuthorId; - @Column private String mAuthorProfileImageUrl; + @Nullable @Column private String mAuthorProfileImageUrl; // Comment data - @Column private String mPostTitle; - @Column private String mStatus; - @Column private String mDatePublished; + @Nullable @Column private String mPostTitle; + @NonNull @Column private String mStatus; + @NonNull @Column private String mDatePublished; @Column private long mPublishedTimestamp; - @Column private String mContent; - @Column private String mUrl; + @NonNull @Column private String mContent; + @NonNull @Column private String mUrl; // Parent Comment Data @Column private boolean mHasParent; @@ -54,9 +58,6 @@ public void setId(int id) { mId = id; } - // not stored in db - denotes the hierarchical level of this comment - public transient int level = 0; - public long getRemoteCommentId() { return mRemoteCommentId; } @@ -73,67 +74,75 @@ public void setRemotePostId(long remotePostId) { mRemotePostId = remotePostId; } + @Nullable public String getAuthorUrl() { return mAuthorUrl; } - public void setAuthorUrl(String authorUrl) { + public void setAuthorUrl(@Nullable String authorUrl) { this.mAuthorUrl = authorUrl; } + @Nullable public String getAuthorName() { return mAuthorName; } - public void setAuthorName(String authorName) { + public void setAuthorName(@Nullable String authorName) { this.mAuthorName = authorName; } + @Nullable public String getAuthorEmail() { return mAuthorEmail; } - public void setAuthorEmail(String authorEmail) { + public void setAuthorEmail(@Nullable String authorEmail) { this.mAuthorEmail = authorEmail; } + @Nullable public String getAuthorProfileImageUrl() { return mAuthorProfileImageUrl; } - public void setAuthorProfileImageUrl(String authorProfileImageUrl) { + public void setAuthorProfileImageUrl(@Nullable String authorProfileImageUrl) { this.mAuthorProfileImageUrl = authorProfileImageUrl; } + @Nullable public String getPostTitle() { return mPostTitle; } - public void setPostTitle(String postTitle) { + public void setPostTitle(@Nullable String postTitle) { this.mPostTitle = postTitle; } + @NonNull public String getStatus() { return mStatus; } - public void setStatus(String status) { + public void setStatus(@NonNull String status) { this.mStatus = status; } + @NonNull public String getDatePublished() { return mDatePublished; } - public void setDatePublished(String datePublished) { + public void setDatePublished(@NonNull String datePublished) { this.mDatePublished = datePublished; } + @NonNull public String getContent() { return mContent; } - public void setContent(String content) { + public void setContent(@NonNull String content) { this.mContent = content; } @@ -169,11 +178,12 @@ public void setILike(boolean iLike) { mILike = iLike; } + @NonNull public String getUrl() { return mUrl; } - public void setUrl(String url) { + public void setUrl(@NonNull String url) { mUrl = url; } diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/model/comments/CommentsMapper.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/model/comments/CommentsMapper.kt index 1cf142805c..1a1cd4b8b3 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/model/comments/CommentsMapper.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/model/comments/CommentsMapper.kt @@ -64,11 +64,11 @@ class CommentsMapper @Inject constructor( this.authorEmail = entity.authorEmail this.authorProfileImageUrl = entity.authorProfileImageUrl this.postTitle = entity.postTitle - this.status = entity.status - this.datePublished = entity.datePublished + this.status = entity.status ?: "" + this.datePublished = entity.datePublished ?: "" this.publishedTimestamp = entity.publishedTimestamp - this.content = entity.content - this.url = entity.url + this.content = entity.content ?: "" + this.url = entity.url ?: "" this.hasParent = entity.hasParent this.parentId = entity.parentId this.iLike = entity.iLike diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/CommentSqlUtils.java b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/CommentSqlUtils.java index 9640759ec2..5ce7f4aa08 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/CommentSqlUtils.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/CommentSqlUtils.java @@ -21,14 +21,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.Date; import java.util.List; import static org.wordpress.android.fluxc.model.LikeModel.TIMESTAMP_THRESHOLD; public class CommentSqlUtils { - public static int insertOrUpdateComment(CommentModel comment) { + public static int insertOrUpdateComment(@Nullable CommentModel comment) { if (comment == null) { return 0; } @@ -62,7 +61,7 @@ public static int insertOrUpdateComment(CommentModel comment) { } } - public static int removeComment(CommentModel comment) { + public static int removeComment(@Nullable CommentModel comment) { if (comment == null) { return 0; } @@ -72,29 +71,26 @@ public static int removeComment(CommentModel comment) { .execute(); } - public static int removeComments(SiteModel site) { - if (site == null) { - return 0; - } - + public static int removeComments(@NonNull SiteModel site) { return WellSql.delete(CommentModel.class) .where().equals(CommentModelTable.LOCAL_SITE_ID, site.getId()).endWhere() .execute(); } - public static int removeCommentGaps(SiteModel site, List comments, int maxEntriesInResponse, - int requestOffset, @Nullable CommentStatus... statuses) { - if (site == null || comments == null || comments.isEmpty()) { + public static int removeCommentGaps( + @NonNull SiteModel site, + @NonNull List comments, + int maxEntriesInResponse, + int requestOffset, + @Nullable CommentStatus... statuses) { + if (comments.isEmpty()) { return 0; } - Collections.sort(comments, new Comparator() { - @Override - public int compare(CommentModel o1, CommentModel o2) { - long x = o2.getPublishedTimestamp(); - long y = o1.getPublishedTimestamp(); - return (x < y) ? -1 : ((x == y) ? 0 : 1); - } + comments.sort((o1, o2) -> { + long x = o2.getPublishedTimestamp(); + long y = o1.getPublishedTimestamp(); + return Long.compare(x, y); }); ArrayList remoteIds = new ArrayList<>(); @@ -105,12 +101,18 @@ public int compare(CommentModel o1, CommentModel o2) { long startOfRange = comments.get(0).getPublishedTimestamp(); long endOfRange = comments.get(comments.size() - 1).getPublishedTimestamp(); + List sourceStatuses; + if (statuses != null) { + sourceStatuses = Arrays.asList(statuses); + } else { + sourceStatuses = Collections.emptyList(); + } ArrayList targetStatuses = new ArrayList<>(); - if (Arrays.asList(statuses).contains(CommentStatus.ALL)) { + if (sourceStatuses.contains(CommentStatus.ALL)) { targetStatuses.add(CommentStatus.APPROVED); targetStatuses.add(CommentStatus.UNAPPROVED); } else { - targetStatuses.addAll(Arrays.asList(statuses)); + targetStatuses.addAll(sourceStatuses); } int numOfDeletedComments = 0; @@ -155,6 +157,7 @@ public static int deleteAllComments() { return WellSql.delete(CommentModel.class).execute(); } + @Nullable public static CommentModel getCommentByLocalCommentId(int localId) { List results = WellSql.select(CommentModel.class) .where().equals(CommentModelTable.ID, localId).endWhere().getAsModel(); @@ -177,16 +180,18 @@ public static CommentModel getCommentBySiteAndRemoteId(@NonNull SiteModel site, return results.get(0); } - private static SelectQuery getCommentsQueryForSite(SiteModel site, CommentStatus... statuses) { + @NonNull + private static SelectQuery getCommentsQueryForSite( + @NonNull SiteModel site, + @NonNull CommentStatus... statuses) { return getCommentsQueryForSite(site, 0, statuses); } - private static SelectQuery getCommentsQueryForSite(SiteModel site, int limit, - CommentStatus... statuses) { - if (site == null) { - return null; - } - + @NonNull + private static SelectQuery getCommentsQueryForSite( + @NonNull SiteModel site, + int limit, + @NonNull CommentStatus... statuses) { SelectQuery query = WellSql.select(CommentModel.class); if (limit > 0) { @@ -204,29 +209,32 @@ private static SelectQuery getCommentsQueryForSite(SiteModel site, return selectQueryBuilder.endGroup().endWhere(); } - public static List getCommentsForSite(SiteModel site, @Order int order, CommentStatus... statuses) { + @NonNull + public static List getCommentsForSite( + @NonNull SiteModel site, + @Order int order, + @NonNull CommentStatus... statuses) { return getCommentsForSite(site, order, 0, statuses); } - public static List getCommentsForSite(SiteModel site, @Order int order, int limit, - CommentStatus... statuses) { - if (site == null) { - return Collections.emptyList(); - } - + @NonNull + public static List getCommentsForSite( + @NonNull SiteModel site, + @Order int order, + int limit, + @NonNull CommentStatus... statuses) { return getCommentsQueryForSite(site, limit, statuses) .orderBy(CommentModelTable.DATE_PUBLISHED, order) .getAsModel(); } - public static int getCommentsCountForSite(SiteModel site, CommentStatus... statuses) { - if (site == null) { - return 0; - } - + public static int getCommentsCountForSite( + @NonNull SiteModel site, + @NonNull CommentStatus... statuses) { return (int) getCommentsQueryForSite(site, statuses).count(); } + @SuppressWarnings("resource") public static int deleteCommentLikesAndPurgeExpired(long siteId, long remoteCommentId) { int numDeleted = WellSql.delete(LikeModel.class) .where() @@ -272,11 +280,10 @@ public static int deleteCommentLikesAndPurgeExpired(long siteId, long remoteComm return numDeleted; } - public static int insertOrUpdateCommentLikes(long siteId, long remoteCommentId, LikeModel like) { - if (null == like) { - return 0; - } - + public static int insertOrUpdateCommentLikes( + long siteId, + long remoteCommentId, + @NonNull LikeModel like) { List likeResult; // If the like already exists and has an id, we want to update it. @@ -300,6 +307,7 @@ public static int insertOrUpdateCommentLikes(long siteId, long remoteCommentId, } } + @NonNull public static List getCommentLikesByCommentId(long siteId, long remoteCommentId) { return WellSql.select(LikeModel.class) .where() diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentStore.java b/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentStore.java index 9db63123ff..b09dfd0473 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentStore.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentStore.java @@ -192,9 +192,9 @@ public enum CommentErrorType { } public static class CommentError implements OnChangedError { - public CommentErrorType type; - public String message; - public CommentError(CommentErrorType type, @NonNull String message) { + @NonNull public CommentErrorType type; + @NonNull public String message; + public CommentError(@NonNull CommentErrorType type, @NonNull String message) { this.type = type; this.message = message; } @@ -205,25 +205,32 @@ public CommentError(CommentErrorType type, @NonNull String message) { public static class OnCommentChanged extends OnChanged { public int rowsAffected; public int offset; - public CommentAction causeOfChange; + @NonNull public CommentAction causeOfChange; @Nullable public CommentStatus requestedStatus; - public List changedCommentsLocalIds = new ArrayList<>(); - public OnCommentChanged(int rowsAffected) { + @NonNull public List changedCommentsLocalIds = new ArrayList<>(); + public OnCommentChanged(int rowsAffected, @NonNull CommentAction causeOfChange) { this.rowsAffected = rowsAffected; + this.causeOfChange = causeOfChange; } } public static class OnCommentLikesChanged extends OnChanged { - public CommentAction causeOfChange; + @NonNull public CommentAction causeOfChange; public final long siteId; public final long commentId; - public List commentLikes = new ArrayList<>(); + @NonNull public List commentLikes = new ArrayList<>(); public final boolean hasMore; - public OnCommentLikesChanged(long siteId, long commentId, boolean hasMore) { + public OnCommentLikesChanged( + long siteId, + long commentId, + boolean hasMore, + @NonNull CommentAction causeOfChange + ) { this.siteId = siteId; this.commentId = commentId; this.hasMore = hasMore; + this.causeOfChange = causeOfChange; } } @@ -247,14 +254,20 @@ public OnCommentLikesChanged(long siteId, long commentId, boolean hasMore) { * @param statuses Array of status or CommentStatus.ALL to get all of them. * @param limit Maximum number of comments to return. 0 is unlimited. */ + @NonNull @SuppressLint("WrongConstant") - public List getCommentsForSite(SiteModel site, boolean orderByDateAscending, int limit, - CommentStatus... statuses) { + public List getCommentsForSite( + @NonNull SiteModel site, + boolean orderByDateAscending, + int limit, + @NonNull CommentStatus... statuses) { @Order int order = orderByDateAscending ? SelectQuery.ORDER_ASCENDING : SelectQuery.ORDER_DESCENDING; return CommentSqlUtils.getCommentsForSite(site, order, limit, statuses); } - public int getNumberOfCommentsForSite(SiteModel site, CommentStatus... statuses) { + public int getNumberOfCommentsForSite( + @NonNull SiteModel site, + @NonNull CommentStatus... statuses) { return CommentSqlUtils.getCommentsCountForSite(site, statuses); } @@ -264,6 +277,7 @@ public CommentModel getCommentBySiteAndRemoteId(@NonNull SiteModel site, long re return CommentSqlUtils.getCommentBySiteAndRemoteId(site, remoteCommentId); } + @Nullable public CommentModel getCommentByLocalId(int localId) { return CommentSqlUtils.getCommentByLocalCommentId(localId); } @@ -366,9 +380,8 @@ private void createNewComment(@NonNull RemoteCreateCommentPayload payload) { } } - private void handleCreatedNewComment(RemoteCommentResponsePayload payload) { - OnCommentChanged event = new OnCommentChanged(1); - event.causeOfChange = CommentAction.CREATE_NEW_COMMENT; + private void handleCreatedNewComment(@NonNull RemoteCommentResponsePayload payload) { + OnCommentChanged event = new OnCommentChanged(1, CommentAction.CREATE_NEW_COMMENT); // Update the comment from the DB if (!payload.isError()) { @@ -381,37 +394,33 @@ private void handleCreatedNewComment(RemoteCommentResponsePayload payload) { emitChange(event); } - private void updateComment(CommentModel payload) { + private void updateComment(@NonNull CommentModel payload) { int rowsAffected = 0; if (!payload.isError()) { rowsAffected = CommentSqlUtils.insertOrUpdateComment(payload); } - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.UPDATE_COMMENT); event.changedCommentsLocalIds.add(payload.getId()); - event.causeOfChange = CommentAction.UPDATE_COMMENT; emitChange(event); } - private void removeComment(CommentModel payload) { + private void removeComment(@NonNull CommentModel payload) { int rowsAffected = CommentSqlUtils.removeComment(payload); - OnCommentChanged event = new OnCommentChanged(rowsAffected); - event.causeOfChange = CommentAction.REMOVE_COMMENT; + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.REMOVE_COMMENT); event.changedCommentsLocalIds.add(payload.getId()); emitChange(event); } - private void removeComments(SiteModel payload) { + private void removeComments(@NonNull SiteModel payload) { int rowsAffected = CommentSqlUtils.removeComments(payload); - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.REMOVE_COMMENTS); // Doesn't make sense to update here event.changedCommentsLocalIds - event.causeOfChange = CommentAction.REMOVE_COMMENTS; emitChange(event); } private void removeAllComments() { int rowsAffected = CommentSqlUtils.deleteAllComments(); - OnCommentChanged event = new OnCommentChanged(rowsAffected); - event.causeOfChange = CommentAction.REMOVE_ALL_COMMENTS; + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.REMOVE_ALL_COMMENTS); emitChange(event); } @@ -429,12 +438,11 @@ private void deleteComment(@NonNull RemoteCommentPayload payload) { } } - private void handleDeletedCommentResponse(RemoteCommentResponsePayload payload) { - OnCommentChanged event = new OnCommentChanged(0); + private void handleDeletedCommentResponse(@NonNull RemoteCommentResponsePayload payload) { + OnCommentChanged event = new OnCommentChanged(0, CommentAction.DELETE_COMMENT); if (payload.comment != null) { event.changedCommentsLocalIds.add(payload.comment.getId()); } - event.causeOfChange = CommentAction.DELETE_COMMENT; event.error = payload.error; if (!payload.isError()) { // Delete once means "send to trash", so we don't want to remove it from the DB, just update it's @@ -458,9 +466,9 @@ private void fetchComments(@NonNull FetchCommentsPayload payload) { } } - private void handleFetchCommentsResponse(FetchCommentsResponsePayload payload) { + private void handleFetchCommentsResponse(@NonNull FetchCommentsResponsePayload payload) { int rowsAffected = 0; - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.FETCH_COMMENTS); if (!payload.isError()) { // Find comments that were deleted or moved to a different status on the server and remove them from // local DB. @@ -472,7 +480,6 @@ private void handleFetchCommentsResponse(FetchCommentsResponsePayload payload) { event.changedCommentsLocalIds.add(comment.getId()); } } - event.causeOfChange = CommentAction.FETCH_COMMENTS; event.error = payload.error; event.requestedStatus = payload.requestedStatus; event.offset = payload.offset; @@ -487,23 +494,21 @@ private void pushComment(@NonNull RemoteCommentPayload payload) { mCommentXMLRPCClient.pushComment(payload.site, payload.comment); } } else { - OnCommentChanged event = new OnCommentChanged(0); - event.causeOfChange = CommentAction.PUSH_COMMENT; + OnCommentChanged event = new OnCommentChanged(0, CommentAction.PUSH_COMMENT); event.error = new CommentError(CommentErrorType.INVALID_INPUT, "Comment can't be null"); emitChange(event); } } - private void handlePushCommentResponse(RemoteCommentResponsePayload payload) { + private void handlePushCommentResponse(@NonNull RemoteCommentResponsePayload payload) { int rowsAffected = 0; if (!payload.isError()) { rowsAffected = CommentSqlUtils.insertOrUpdateComment(payload.comment); } - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.PUSH_COMMENT); if (payload.comment != null) { event.changedCommentsLocalIds.add(payload.comment.getId()); } - event.causeOfChange = CommentAction.PUSH_COMMENT; event.error = payload.error; emitChange(event); } @@ -524,16 +529,15 @@ private long getPrioritizedRemoteCommentId(@NonNull RemoteCommentPayload payload } } - private void handleFetchCommentResponse(RemoteCommentResponsePayload payload) { + private void handleFetchCommentResponse(@NonNull RemoteCommentResponsePayload payload) { int rowsAffected = 0; if (!payload.isError()) { rowsAffected = CommentSqlUtils.insertOrUpdateComment(payload.comment); } - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.FETCH_COMMENT); if (payload.comment != null) { event.changedCommentsLocalIds.add(payload.comment.getId()); } - event.causeOfChange = CommentAction.FETCH_COMMENT; event.error = payload.error; emitChange(event); } @@ -548,8 +552,7 @@ private void likeComment(@NonNull RemoteLikeCommentPayload payload) { if (payload.site.isUsingWpComRestApi()) { mCommentRestClient.likeComment(payload.site, getPrioritizedRemoteCommentId(payload), comment, payload.like); } else { - OnCommentChanged event = new OnCommentChanged(0); - event.causeOfChange = CommentAction.LIKE_COMMENT; + OnCommentChanged event = new OnCommentChanged(0, CommentAction.LIKE_COMMENT); if (payload.comment != null) { event.changedCommentsLocalIds.add(payload.comment.getId()); } @@ -558,16 +561,15 @@ private void likeComment(@NonNull RemoteLikeCommentPayload payload) { } } - private void handleLikedCommentResponse(RemoteCommentResponsePayload payload) { + private void handleLikedCommentResponse(@NonNull RemoteCommentResponsePayload payload) { int rowsAffected = 0; if (!payload.isError()) { rowsAffected = CommentSqlUtils.insertOrUpdateComment(payload.comment); } - OnCommentChanged event = new OnCommentChanged(rowsAffected); + OnCommentChanged event = new OnCommentChanged(rowsAffected, CommentAction.LIKE_COMMENT); if (payload.comment != null) { event.changedCommentsLocalIds.add(payload.comment.getId()); } - event.causeOfChange = CommentAction.LIKE_COMMENT; event.error = payload.error; emitChange(event); } @@ -581,11 +583,12 @@ private void fetchCommentLikes(@NonNull FetchCommentLikesPayload payload) { ); } - private void handleFetchedCommentLikes(FetchedCommentLikesResponsePayload payload) { + private void handleFetchedCommentLikes(@NonNull FetchedCommentLikesResponsePayload payload) { OnCommentLikesChanged event = new OnCommentLikesChanged( payload.siteId, payload.commentRemoteId, - payload.hasMore + payload.hasMore, + CommentAction.FETCHED_COMMENT_LIKES ); if (!payload.isError()) { if (!payload.isRequestNextPage) { @@ -607,7 +610,6 @@ private void handleFetchedCommentLikes(FetchedCommentLikesResponsePayload payloa event.commentLikes.addAll(cachedLikes); } - event.causeOfChange = CommentAction.FETCHED_COMMENT_LIKES; event.error = payload.error; emitChange(event); } diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentsStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentsStore.kt index a73f48ce62..1da5a809a7 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentsStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/CommentsStore.kt @@ -654,8 +654,7 @@ class CommentsStore @Inject constructor( ) private suspend fun onPushComment(payload: RemoteCommentPayload): OnCommentChanged { if (payload.comment == null) { - return OnCommentChanged(0).apply { - this.causeOfChange = CommentAction.PUSH_COMMENT + return OnCommentChanged(0, CommentAction.PUSH_COMMENT).apply { this.error = CommentError(INVALID_INPUT, "Comment can't be null") } } @@ -715,9 +714,8 @@ class CommentsStore @Inject constructor( status: CommentStatus? = null, offset: Int? = null ): OnCommentChanged { - return OnCommentChanged(rowsAffected).apply { + return OnCommentChanged(rowsAffected, actionType).apply { this.changedCommentsLocalIds.addAll(commentLocalIds) - this.causeOfChange = actionType this.error = error status?.let { this.requestedStatus = it diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/utils/CommentErrorUtils.java b/fluxc/src/main/java/org/wordpress/android/fluxc/utils/CommentErrorUtils.java index 9191d3569a..14b15564c4 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/utils/CommentErrorUtils.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/utils/CommentErrorUtils.java @@ -1,5 +1,6 @@ package org.wordpress.android.fluxc.utils; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.wordpress.android.fluxc.model.CommentModel; @@ -17,15 +18,19 @@ import java.util.ArrayList; public class CommentErrorUtils { - public static RemoteCommentResponsePayload commentErrorToFetchCommentPayload(BaseNetworkError error, - @Nullable CommentModel comment) { + @NonNull + public static RemoteCommentResponsePayload commentErrorToFetchCommentPayload( + @NonNull BaseNetworkError error, + @Nullable CommentModel comment) { RemoteCommentResponsePayload payload = new RemoteCommentResponsePayload(comment); payload.error = new CommentError(genericToCommentError(error), getErrorMessage(error)); return payload; } - public static FetchCommentsResponsePayload commentErrorToFetchCommentsPayload(BaseNetworkError error, - SiteModel site) { + @NonNull + public static FetchCommentsResponsePayload commentErrorToFetchCommentsPayload( + @NonNull BaseNetworkError error, + @NonNull SiteModel site) { FetchCommentsResponsePayload payload = new FetchCommentsResponsePayload( new ArrayList<>(), site, 0, 0, null ); @@ -33,8 +38,9 @@ public static FetchCommentsResponsePayload commentErrorToFetchCommentsPayload(Ba return payload; } + @NonNull public static FetchedCommentLikesResponsePayload commentErrorToFetchedCommentLikesPayload( - BaseNetworkError error, + @NonNull BaseNetworkError error, long siteId, long commentId, boolean requestNextPage, @@ -51,25 +57,25 @@ public static FetchedCommentLikesResponsePayload commentErrorToFetchedCommentLik return payload; } - public static RemoteCommentResponsePayload commentErrorToPushCommentPayload(BaseNetworkError error, - CommentModel comment) { + @NonNull + public static RemoteCommentResponsePayload commentErrorToPushCommentPayload( + @NonNull BaseNetworkError error, + @NonNull CommentModel comment) { RemoteCommentResponsePayload payload = new RemoteCommentResponsePayload(comment); payload.error = new CommentError(genericToCommentError(error), getErrorMessage(error)); return payload; } - public static CommentError networkToCommentError(BaseNetworkError error) { + @NonNull + public static CommentError networkToCommentError(@NonNull BaseNetworkError error) { return new CommentError(genericToCommentError(error), getErrorMessage(error)); } - private static CommentErrorType genericToCommentError(BaseNetworkError error) { + @NonNull + private static CommentErrorType genericToCommentError(@NonNull BaseNetworkError error) { CommentErrorType errorType = CommentErrorType.GENERIC_ERROR; - if (error.isGeneric()) { - switch (error.type) { - case INVALID_RESPONSE: - errorType = CommentErrorType.INVALID_RESPONSE; - break; - } + if (error.isGeneric() && error.type == GenericErrorType.INVALID_RESPONSE) { + errorType = CommentErrorType.INVALID_RESPONSE; } if (error instanceof WPComGsonNetworkError) { WPComGsonNetworkError wpComGsonNetworkError = (WPComGsonNetworkError) error; @@ -101,7 +107,8 @@ private static CommentErrorType genericToCommentError(BaseNetworkError error) { return errorType; } - private static String getErrorMessage(BaseNetworkError error) { + @NonNull + private static String getErrorMessage(@NonNull BaseNetworkError error) { return error.message; } }