From 6ba076ae3e1142070ea03f2839e63ad9b1a86963 Mon Sep 17 00:00:00 2001 From: Mishiranu Date: Fri, 18 Sep 2020 03:56:10 +0300 Subject: [PATCH] Slightly rework autohide rules --- .../content/async/DeserializePostsTask.java | 22 +------ .../dashchan/content/async/ReadPostsTask.java | 2 +- .../content/async/ReadSearchTask.java | 2 +- .../content/async/ReadSinglePostTask.java | 2 +- .../content/async/ReadThreadsTask.java | 2 +- .../dashchan/content/model/PostItem.java | 41 ++++++------ .../content/storage/AutohideStorage.java | 8 ++- .../ui/navigator/manager/DialogUnit.java | 4 +- .../ui/navigator/manager/HidePerformer.java | 63 +++++++++++++------ .../ui/navigator/manager/InteractionUnit.java | 23 +++---- .../ui/navigator/manager/ViewUnit.java | 4 +- .../dashchan/ui/navigator/page/PostsPage.java | 4 +- 12 files changed, 90 insertions(+), 87 deletions(-) diff --git a/src/com/mishiranu/dashchan/content/async/DeserializePostsTask.java b/src/com/mishiranu/dashchan/content/async/DeserializePostsTask.java index dc434e34..4d1b5226 100644 --- a/src/com/mishiranu/dashchan/content/async/DeserializePostsTask.java +++ b/src/com/mishiranu/dashchan/content/async/DeserializePostsTask.java @@ -1,27 +1,9 @@ -/* - * Copyright 2014-2016 Fukurou Mishiranu - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package com.mishiranu.dashchan.content.async; import chan.content.model.Post; import chan.content.model.Posts; - import com.mishiranu.dashchan.content.CacheManager; import com.mishiranu.dashchan.content.model.PostItem; - import java.util.ArrayList; public class DeserializePostsTask extends CancellableTask { @@ -65,7 +47,7 @@ protected Boolean doInBackground(Void... params) { } postItems = new ArrayList<>(posts.length); for (Post post : posts) { - postItems.add(new PostItem(post, chanName, boardName)); + postItems.add(PostItem.createPost(post, chanName, boardName)); } return true; } @@ -80,4 +62,4 @@ public void cancel() { cancel(true); holder.cancel(); } -} \ No newline at end of file +} diff --git a/src/com/mishiranu/dashchan/content/async/ReadPostsTask.java b/src/com/mishiranu/dashchan/content/async/ReadPostsTask.java index 0792836b..550c743e 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadPostsTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadPostsTask.java @@ -208,7 +208,7 @@ protected Boolean doInBackground(HttpHolder holder, Void... params) { } } for (Patch patch : handleResult.patches) { - patch.postItem = new PostItem(patch.newPost, chanName, boardName); + patch.postItem = PostItem.createPost(patch.newPost, chanName, boardName); } callback.onRequestPreloadPosts(handleResult.patches, cachedPosts != null ? cachedPosts.length() : 0); if (validator == null) { diff --git a/src/com/mishiranu/dashchan/content/async/ReadSearchTask.java b/src/com/mishiranu/dashchan/content/async/ReadSearchTask.java index 15db3af3..7700dc85 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadSearchTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadSearchTask.java @@ -95,7 +95,7 @@ protected ArrayList doInBackground(HttpHolder holder, Void... params) Collections.sort(posts, this); ArrayList postItems = new ArrayList<>(posts.size()); for (int i = 0; i < posts.size() && !Thread.interrupted(); i++) { - PostItem postItem = new PostItem(posts.get(i), chanName, boardName); + PostItem postItem = PostItem.createPost(posts.get(i), chanName, boardName); postItem.setOrdinalIndex(i); postItem.preload(); postItems.add(postItem); diff --git a/src/com/mishiranu/dashchan/content/async/ReadSinglePostTask.java b/src/com/mishiranu/dashchan/content/async/ReadSinglePostTask.java index fc2e9705..502a2f99 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadSinglePostTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadSinglePostTask.java @@ -42,7 +42,7 @@ protected PostItem doInBackground(HttpHolder holder, Void... params) { .ReadSinglePostData(boardName, postNumber, holder)); Post post = result != null ? result.post : null; startTime = 0L; - return new PostItem(post, chanName, boardName); + return PostItem.createPost(post, chanName, boardName); } catch (HttpException e) { errorItem = e.getErrorItemAndHandle(); if (errorItem.httpResponseCode == HttpURLConnection.HTTP_NOT_FOUND || diff --git a/src/com/mishiranu/dashchan/content/async/ReadThreadsTask.java b/src/com/mishiranu/dashchan/content/async/ReadThreadsTask.java index 3c2e58a3..6ef9b0f2 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadThreadsTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadThreadsTask.java @@ -83,7 +83,7 @@ protected Boolean doInBackground(HttpHolder holder, Void... params) { if (postItems == null) { postItems = new ArrayList<>(); } - postItems.add(new PostItem(thread, chanName, boardName)); + postItems.add(PostItem.createThread(thread, chanName, boardName)); } } } diff --git a/src/com/mishiranu/dashchan/content/model/PostItem.java b/src/com/mishiranu/dashchan/content/model/PostItem.java index e181d736..fc1cf3d6 100644 --- a/src/com/mishiranu/dashchan/content/model/PostItem.java +++ b/src/com/mishiranu/dashchan/content/model/PostItem.java @@ -29,12 +29,15 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.List; public class PostItem implements AttachmentItem.Binder, ChanMarkup.MarkupExtra, Comparable { + private final Post post; + private final ThreadData threadData; private final String chanName; private final String boardName; - private ArrayList attachmentItems; - private ArrayList> icons; + private final List attachmentItems; + private final List> icons; public static final int ORDINAL_INDEX_NONE = -1; public static final int ORDINAL_INDEX_DELETED = -2; @@ -56,9 +59,6 @@ public class PostItem implements AttachmentItem.Binder, ChanMarkup.MarkupExtra, private boolean expanded = false; - private final Post post; - private final ThreadData threadData; - private static class ThreadData { public int postsCount; public int filesCount; @@ -75,31 +75,28 @@ public enum HideState {UNDEFINED, HIDDEN, SHOWN} private String hideReason; private boolean unread = false; - public PostItem(Post post, String chanName, String boardName) { - this.post = post; - threadData = null; - this.chanName = chanName; - this.boardName = boardName; - init(); + public static PostItem createPost(Post post, String chanName, String boardName) { + return new PostItem(post, null, chanName, boardName); } - public PostItem(Posts thread, String chanName, String boardName) { + public static PostItem createThread(Posts thread, String chanName, String boardName) { Post[] posts = thread.getPosts(); - this.post = posts[0]; - threadData = new ThreadData(); + Post post = posts[0]; + ThreadData threadData = new ThreadData(); threadData.postsCount = thread.getPostsCount(); threadData.filesCount = thread.getFilesCount(); threadData.postsWithFilesCount = thread.getPostsWithFilesCount(); threadData.firstAndLastPosts = posts; - this.chanName = chanName; - this.boardName = boardName; - init(); + return new PostItem(post, threadData, chanName, boardName); } - private void init() { + private PostItem(Post post, ThreadData threadData, String chanName, String boardName) { + this.post = post; + this.threadData = threadData; + this.chanName = chanName; + this.boardName = boardName; attachmentItems = AttachmentItem.obtain(this); if (isThreadItem()) { - ThreadData threadData = this.threadData; if (attachmentItems != null) { threadData.gallerySet = new GalleryItem.GallerySet(false); threadData.gallerySet.setThreadTitle(getSubjectOrComment()); @@ -558,7 +555,7 @@ public LinkSpan[] getLinkSpansAfterComment() { return linkSpans; } - public ArrayList> getIcons() { + public List> getIcons() { return icons; } @@ -566,7 +563,7 @@ public boolean hasAttachments() { return attachmentItems != null; } - public ArrayList getAttachmentItems() { + public List getAttachmentItems() { return attachmentItems; } @@ -618,7 +615,7 @@ public PostItem[] getThreadLastPosts() { PostItem[] postItems = new PostItem[posts.length - 1]; int startIndex = threadData.postsCount - posts.length + 1; for (int i = 0; i < postItems.length; i++) { - PostItem postItem = new PostItem(posts[i + 1], chanName, boardName); + PostItem postItem = createPost(posts[i + 1], chanName, boardName); postItem.setOrdinalIndex(startIndex > 0 ? startIndex + i : ORDINAL_INDEX_NONE); postItems[i] = postItem; } diff --git a/src/com/mishiranu/dashchan/content/storage/AutohideStorage.java b/src/com/mishiranu/dashchan/content/storage/AutohideStorage.java index e9492254..3dc7e281 100644 --- a/src/com/mishiranu/dashchan/content/storage/AutohideStorage.java +++ b/src/com/mishiranu/dashchan/content/storage/AutohideStorage.java @@ -210,15 +210,17 @@ public String find(String data) { return null; } - public String getReason(boolean subject, boolean name, String text, String findResult) { + public enum ReasonSource {NAME, SUBJECT, COMMENT} + + public String getReason(ReasonSource reasonSource, String text, String findResult) { StringBuilder builder = new StringBuilder(); if (optionSage) { builder.append("sage "); } - if (optionSubject && subject) { + if (optionSubject && reasonSource == ReasonSource.SUBJECT) { builder.append("subject "); } - if (optionName && name) { + if (optionName && reasonSource == ReasonSource.NAME) { builder.append("name "); } if (!StringUtils.isEmpty(findResult)) { diff --git a/src/com/mishiranu/dashchan/ui/navigator/manager/DialogUnit.java b/src/com/mishiranu/dashchan/ui/navigator/manager/DialogUnit.java index b5cbb9e6..7dcd4c33 100644 --- a/src/com/mishiranu/dashchan/ui/navigator/manager/DialogUnit.java +++ b/src/com/mishiranu/dashchan/ui/navigator/manager/DialogUnit.java @@ -1144,7 +1144,7 @@ public void showPostDescriptionDialog(Collection icons, String chanNam } public void performSendDeletePosts(String chanName, String boardName, String threadNumber, - ArrayList postNumbers) { + List postNumbers) { ChanConfiguration.Deleting deleting = ChanConfiguration.get(chanName).safe().obtainDeleting(boardName); if (deleting == null) { return; @@ -1163,7 +1163,7 @@ public void performSendDeletePosts(String chanName, String boardName, String thr } public void performSendReportPosts(String chanName, String boardName, String threadNumber, - ArrayList postNumbers) { + List postNumbers) { ChanConfiguration.Reporting reporting = ChanConfiguration.get(chanName).safe().obtainReporting(boardName); if (reporting == null) { return; diff --git a/src/com/mishiranu/dashchan/ui/navigator/manager/HidePerformer.java b/src/com/mishiranu/dashchan/ui/navigator/manager/HidePerformer.java index f62ed889..8f212246 100644 --- a/src/com/mishiranu/dashchan/ui/navigator/manager/HidePerformer.java +++ b/src/com/mishiranu/dashchan/ui/navigator/manager/HidePerformer.java @@ -1,6 +1,8 @@ package com.mishiranu.dashchan.ui.navigator.manager; import android.content.Context; +import android.net.Uri; +import android.util.Pair; import chan.content.model.Posts; import chan.util.StringUtils; import com.mishiranu.dashchan.R; @@ -9,9 +11,11 @@ import com.mishiranu.dashchan.text.SimilarTextEstimator; import com.mishiranu.dashchan.util.ToastUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; public class HidePerformer implements PostItem.HidePerformer { private static final int MAX_COMMENT_LENGTH = 1000; @@ -101,11 +105,11 @@ private String checkHiddenGlobalAutohide(PostItem postItem) { boolean sage = postItem.isSage(); String subject = null; String comment = null; - String name = null; + List names = null; ArrayList autohideItems = autohideStorage.getItems(); for (int i = 0; i < autohideItems.size(); i++) { AutohideStorage.AutohideItem autohideItem = autohideItems.get(i); - // AND selection (only if chan, board, thread, op and sage matches to rule) + // AND selection (only if chan, board, thread, op, and sage match the rule) if (autohideItem.chanNames == null || autohideItem.chanNames.contains(chanName)) { if (StringUtils.isEmpty(autohideItem.boardName) || boardName == null || autohideItem.boardName.equals(boardName)) { @@ -114,24 +118,47 @@ private String checkHiddenGlobalAutohide(PostItem postItem) { if ((!autohideItem.optionOriginalPost || autohideItem.optionOriginalPost == originalPost) && (!autohideItem.optionSage || autohideItem.optionSage == sage)) { String result; - // OR selection (hide if subj, exp or name matches to rule) - if (subject == null) { - subject = postItem.getSubject(); + // OR selection (hide if subject, comment, or name match the rule) + if (autohideItem.optionSubject) { + if (subject == null) { + subject = postItem.getSubject(); + } + if ((result = autohideItem.find(subject)) != null) { + return autohideItem.getReason(AutohideStorage.AutohideItem + .ReasonSource.SUBJECT, comment, result); + } } - if (autohideItem.optionSubject && (result = autohideItem.find(subject)) != null) { - return autohideItem.getReason(true, false, comment, result); + if (autohideItem.optionComment) { + if (comment == null) { + comment = postItem.getComment().toString(); + } + if ((result = autohideItem.find(comment)) != null) { + return autohideItem.getReason(AutohideStorage.AutohideItem + .ReasonSource.COMMENT, comment, result); + } } - if (comment == null) { - comment = postItem.getComment().toString(); - } - if (autohideItem.optionComment && (result = autohideItem.find(comment)) != null) { - return autohideItem.getReason(false, false, comment, result); - } - if (name == null) { - name = postItem.getFullName().toString(); - } - if (autohideItem.optionName && (result = autohideItem.find(name)) != null) { - return autohideItem.getReason(false, true, name, result); + if (autohideItem.optionName) { + if (names == null) { + String name = postItem.getFullName().toString(); + List> icons = postItem.getIcons(); + if (icons != null && !icons.isEmpty()) { + names = new ArrayList<>(1 + icons.size()); + names.add(name); + for (Pair icon : icons) { + if (icon.second != null) { + names.add(icon.second); + } + } + } else { + names = Collections.singletonList(name); + } + } + for (String name : names) { + if ((result = autohideItem.find(name)) != null) { + return autohideItem.getReason(AutohideStorage.AutohideItem + .ReasonSource.NAME, name, result); + } + } } } } diff --git a/src/com/mishiranu/dashchan/ui/navigator/manager/InteractionUnit.java b/src/com/mishiranu/dashchan/ui/navigator/manager/InteractionUnit.java index 8f305e36..2a88b8ed 100644 --- a/src/com/mishiranu/dashchan/ui/navigator/manager/InteractionUnit.java +++ b/src/com/mishiranu/dashchan/ui/navigator/manager/InteractionUnit.java @@ -22,7 +22,8 @@ import com.mishiranu.dashchan.util.ListViewUtils; import com.mishiranu.dashchan.util.NavigationUtils; import com.mishiranu.dashchan.widget.AttachmentView; -import java.util.ArrayList; +import java.util.Collections; +import java.util.List; public class InteractionUnit { private final UiManager uiManager; @@ -170,7 +171,7 @@ public void update(int index, boolean mayShowDialog, GalleryOverlay.NavigatePost @Override public void onClick(View v) { UiManager.Holder holder = ListViewUtils.getViewHolder(v, UiManager.Holder.class); - ArrayList attachmentItems = holder.getPostItem().getAttachmentItems(); + List attachmentItems = holder.getPostItem().getAttachmentItems(); if (attachmentItems != null) { GalleryItem.GallerySet gallerySet = holder.getGallerySet(); int startImageIndex = uiManager.view().findImageIndex(gallerySet.getItems(), holder.getPostItem()); @@ -329,20 +330,14 @@ public boolean handlePostContextMenu(final PostItem postItem, Replyable replyabl } if (!postItem.isDeleted()) { if (board.allowReporting) { - dialogMenu.add(R.string.report, () -> { - ArrayList postNumbers = new ArrayList<>(1); - postNumbers.add(postItem.getPostNumber()); - uiManager.dialog().performSendReportPosts(postItem.getChanName(), postItem.getBoardName(), - postItem.getThreadNumber(), postNumbers); - }); + dialogMenu.add(R.string.report, () -> uiManager.dialog() + .performSendReportPosts(postItem.getChanName(), postItem.getBoardName(), + postItem.getThreadNumber(), Collections.singletonList(postItem.getPostNumber()))); } if (board.allowDeleting) { - dialogMenu.add(R.string.delete, () -> { - ArrayList postNumbers = new ArrayList<>(1); - postNumbers.add(postItem.getPostNumber()); - uiManager.dialog().performSendDeletePosts(postItem.getChanName(), postItem.getBoardName(), - postItem.getThreadNumber(), postNumbers); - }); + dialogMenu.add(R.string.delete, () -> uiManager.dialog() + .performSendDeletePosts(postItem.getChanName(), postItem.getBoardName(), + postItem.getThreadNumber(), Collections.singletonList(postItem.getPostNumber()))); } } if (allowMyMarkEdit) { diff --git a/src/com/mishiranu/dashchan/ui/navigator/manager/ViewUnit.java b/src/com/mishiranu/dashchan/ui/navigator/manager/ViewUnit.java index 6bfd755e..5714abbe 100644 --- a/src/com/mishiranu/dashchan/ui/navigator/manager/ViewUnit.java +++ b/src/com/mishiranu/dashchan/ui/navigator/manager/ViewUnit.java @@ -525,7 +525,7 @@ private void handlePostViewIcons(PostViewHolder holder) { Context context = uiManager.getContext(); PostItem postItem = holder.postItem; String chanName = postItem.getChanName(); - ArrayList> icons = postItem.getIcons(); + List> icons = postItem.getIcons(); if (icons != null && Preferences.isDisplayIcons()) { if (holder.badgeImages == null) { holder.badgeImages = new ArrayList<>(); @@ -738,7 +738,7 @@ public boolean onTouch(View v, MotionEvent event) { String emailToCopy = null; switch (type) { case TYPE_BADGES: { - ArrayList> postIcons = holder.postItem.getIcons(); + List> postIcons = holder.postItem.getIcons(); ChanLocator locator = ChanLocator.get(holder.postItem.getChanName()); for (Pair postIcon : postIcons) { Uri uri = postIcon.first; diff --git a/src/com/mishiranu/dashchan/ui/navigator/page/PostsPage.java b/src/com/mishiranu/dashchan/ui/navigator/page/PostsPage.java index 4cf8b11e..36a9d97f 100644 --- a/src/com/mishiranu/dashchan/ui/navigator/page/PostsPage.java +++ b/src/com/mishiranu/dashchan/ui/navigator/page/PostsPage.java @@ -619,7 +619,7 @@ page.threadNumber, getParcelableExtra(ParcelableExtra.FACTORY).threadTitle, int postsWithFiles = 0; int links = 0; for (PostItem postItem : getAdapter()) { - ArrayList attachmentItems = postItem.getAttachmentItems(); + List attachmentItems = postItem.getAttachmentItems(); if (attachmentItems != null) { int itFiles = 0; for (AttachmentItem attachmentItem : attachmentItems) { @@ -857,7 +857,7 @@ public SearchSubmitResult onSearchSubmit(String query) { String subject = postItem.getSubject().toLowerCase(locale); String name = postItem.getFullName().toString().toLowerCase(locale); fileNames.clear(); - ArrayList attachmentItems = postItem.getAttachmentItems(); + List attachmentItems = postItem.getAttachmentItems(); if (attachmentItems != null) { for (AttachmentItem attachmentItem : attachmentItems) { String fileName = attachmentItem.getFileName();