Skip to content

Commit

Permalink
Slightly rework autohide rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishiranu committed Sep 18, 2020
1 parent 946063d commit 6ba076a
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 87 deletions.
22 changes: 2 additions & 20 deletions 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<Void, Void, Boolean> {
Expand Down Expand Up @@ -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;
}
Expand All @@ -80,4 +62,4 @@ public void cancel() {
cancel(true);
holder.cancel();
}
}
}
Expand Up @@ -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) {
Expand Down
Expand Up @@ -95,7 +95,7 @@ protected ArrayList<PostItem> doInBackground(HttpHolder holder, Void... params)
Collections.sort(posts, this);
ArrayList<PostItem> 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);
Expand Down
Expand Up @@ -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 ||
Expand Down
Expand Up @@ -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));
}
}
}
Expand Down
41 changes: 19 additions & 22 deletions src/com/mishiranu/dashchan/content/model/PostItem.java
Expand Up @@ -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<PostItem> {
private final Post post;
private final ThreadData threadData;
private final String chanName;
private final String boardName;
private ArrayList<AttachmentItem> attachmentItems;
private ArrayList<Pair<Uri, String>> icons;
private final List<AttachmentItem> attachmentItems;
private final List<Pair<Uri, String>> icons;

public static final int ORDINAL_INDEX_NONE = -1;
public static final int ORDINAL_INDEX_DELETED = -2;
Expand All @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -558,15 +555,15 @@ public LinkSpan[] getLinkSpansAfterComment() {
return linkSpans;
}

public ArrayList<Pair<Uri, String>> getIcons() {
public List<Pair<Uri, String>> getIcons() {
return icons;
}

public boolean hasAttachments() {
return attachmentItems != null;
}

public ArrayList<AttachmentItem> getAttachmentItems() {
public List<AttachmentItem> getAttachmentItems() {
return attachmentItems;
}

Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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)) {
Expand Down
Expand Up @@ -1144,7 +1144,7 @@ public void showPostDescriptionDialog(Collection<IconData> icons, String chanNam
}

public void performSendDeletePosts(String chanName, String boardName, String threadNumber,
ArrayList<String> postNumbers) {
List<String> postNumbers) {
ChanConfiguration.Deleting deleting = ChanConfiguration.get(chanName).safe().obtainDeleting(boardName);
if (deleting == null) {
return;
Expand All @@ -1163,7 +1163,7 @@ public void performSendDeletePosts(String chanName, String boardName, String thr
}

public void performSendReportPosts(String chanName, String boardName, String threadNumber,
ArrayList<String> postNumbers) {
List<String> postNumbers) {
ChanConfiguration.Reporting reporting = ChanConfiguration.get(chanName).safe().obtainReporting(boardName);
if (reporting == null) {
return;
Expand Down
63 changes: 45 additions & 18 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -101,11 +105,11 @@ private String checkHiddenGlobalAutohide(PostItem postItem) {
boolean sage = postItem.isSage();
String subject = null;
String comment = null;
String name = null;
List<String> names = null;
ArrayList<AutohideStorage.AutohideItem> 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)) {
Expand All @@ -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<Pair<Uri, String>> icons = postItem.getIcons();
if (icons != null && !icons.isEmpty()) {
names = new ArrayList<>(1 + icons.size());
names.add(name);
for (Pair<Uri, String> 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);
}
}
}
}
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AttachmentItem> attachmentItems = holder.getPostItem().getAttachmentItems();
List<AttachmentItem> attachmentItems = holder.getPostItem().getAttachmentItems();
if (attachmentItems != null) {
GalleryItem.GallerySet gallerySet = holder.getGallerySet();
int startImageIndex = uiManager.view().findImageIndex(gallerySet.getItems(), holder.getPostItem());
Expand Down Expand Up @@ -329,20 +330,14 @@ public boolean handlePostContextMenu(final PostItem postItem, Replyable replyabl
}
if (!postItem.isDeleted()) {
if (board.allowReporting) {
dialogMenu.add(R.string.report, () -> {
ArrayList<String> 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<String> 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) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/mishiranu/dashchan/ui/navigator/manager/ViewUnit.java
Expand Up @@ -525,7 +525,7 @@ private void handlePostViewIcons(PostViewHolder holder) {
Context context = uiManager.getContext();
PostItem postItem = holder.postItem;
String chanName = postItem.getChanName();
ArrayList<Pair<Uri, String>> icons = postItem.getIcons();
List<Pair<Uri, String>> icons = postItem.getIcons();
if (icons != null && Preferences.isDisplayIcons()) {
if (holder.badgeImages == null) {
holder.badgeImages = new ArrayList<>();
Expand Down Expand Up @@ -738,7 +738,7 @@ public boolean onTouch(View v, MotionEvent event) {
String emailToCopy = null;
switch (type) {
case TYPE_BADGES: {
ArrayList<Pair<Uri, String>> postIcons = holder.postItem.getIcons();
List<Pair<Uri, String>> postIcons = holder.postItem.getIcons();
ChanLocator locator = ChanLocator.get(holder.postItem.getChanName());
for (Pair<Uri, String> postIcon : postIcons) {
Uri uri = postIcon.first;
Expand Down
4 changes: 2 additions & 2 deletions src/com/mishiranu/dashchan/ui/navigator/page/PostsPage.java
Expand Up @@ -619,7 +619,7 @@ page.threadNumber, getParcelableExtra(ParcelableExtra.FACTORY).threadTitle,
int postsWithFiles = 0;
int links = 0;
for (PostItem postItem : getAdapter()) {
ArrayList<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
List<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
if (attachmentItems != null) {
int itFiles = 0;
for (AttachmentItem attachmentItem : attachmentItems) {
Expand Down Expand Up @@ -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<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
List<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
if (attachmentItems != null) {
for (AttachmentItem attachmentItem : attachmentItems) {
String fileName = attachmentItem.getFileName();
Expand Down

0 comments on commit 6ba076a

Please sign in to comment.