Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
fix and refactor removing statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
sk22 committed Oct 16, 2023
1 parent bdd2b90 commit 7e80ed6
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ protected void updatePoll(String parentID, Status statusForContent, Poll poll){
int firstOptionIndex=-1, footerIndex=-1;
int i=0;
for(StatusDisplayItem item:displayItems){
if(item.contentStatusID.equals(statusForContent.id)){
if(item.getContentID().equals(statusForContent.id)){
if(item instanceof PollOptionStatusDisplayItem && firstOptionIndex==-1){
firstOptionIndex=i;
}else if(item instanceof PollFooterStatusDisplayItem){
Expand All @@ -485,7 +485,7 @@ protected void updatePoll(String parentID, Status statusForContent, Poll poll){
List<StatusDisplayItem> pollItems=displayItems.subList(firstOptionIndex, footerIndex+1);
int prevSize=pollItems.size();
pollItems.clear();
StatusDisplayItem.buildPollItems(parentID, statusForContent.id, this, poll, pollItems);
StatusDisplayItem.buildPollItems(parentID, this, poll, pollItems);
if(prevSize!=pollItems.size()){
adapter.notifyItemRangeRemoved(firstOptionIndex, prevSize);
adapter.notifyItemRangeInserted(firstOptionIndex, pollItems.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ enum StatusEditChangeType{
}
String sep = getString(R.string.sk_separator);
items.add(0, new ReblogOrReplyLineStatusDisplayItem(s.id, this, action+" "+sep+" "+date, Collections.emptyList(), 0, null, null, s));
items.add(1, new DummyStatusDisplayItem(s.id, s.getContentStatus().id, this));
items.add(1, new DummyStatusDisplayItem(s.id, this));
}
return items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -144,12 +143,12 @@ protected void onStatusUpdated(Status status){
}
}

protected Status getContentStatusByID(String id){
public Status getContentStatusByID(String id){
Status s=getStatusByID(id);
return s==null ? null : s.getContentStatus();
}

protected Status getStatusByID(String id){
public Status getStatusByID(String id){
for(Status s:data){
if(s.id.equals(id)){
return s;
Expand Down Expand Up @@ -179,54 +178,51 @@ protected void onRemoveAccountPostsEvent(RemoveAccountPostsEvent ev){
private void iterateRemoveStatus(List<Status> l, String id){
Iterator<Status> it=l.iterator();
while(it.hasNext()){
if(Objects.equals(it.next().getContentStatus().id, id)){
if(it.next().getContentStatus().id.equals(id)){
it.remove();
}
}
}

private int removeStatusDisplayItems(Status status, int index, int ancestorFirstIndex, int ancestorLastIndex, int indexOffset){
private void removeStatusDisplayItems(Status status, int index, int ancestorFirstIndex, int ancestorLastIndex){
// did we find an ancestor that is also the status' neighbor?
if(ancestorFirstIndex>=0 && ancestorLastIndex==index-1){
for(int i=ancestorFirstIndex; i<=ancestorLastIndex; i++){
StatusDisplayItem item=displayItems.get(i);
// update ancestor to have no descendant anymore
if(item.contentStatusID.equals(status.inReplyToId)) item.hasDescendantNeighbor=false;
if(item.getContentID().equals(status.inReplyToId)) item.hasDescendantNeighbor=false;
}
adapter.notifyItemRangeChanged(ancestorFirstIndex-indexOffset, ancestorLastIndex-ancestorFirstIndex+1);
adapter.notifyItemRangeChanged(ancestorFirstIndex, ancestorLastIndex-ancestorFirstIndex+1);
}

if(index==-1) return 0;
if(index==-1) return;
int lastIndex;
for(lastIndex=index;lastIndex<displayItems.size();lastIndex++){
if(!displayItems.get(lastIndex).contentStatusID.equals(status.id))
if(!displayItems.get(lastIndex).getContentID().equals(status.id))
break;
}
int count=lastIndex-index;
displayItems.subList(index-indexOffset, lastIndex-indexOffset).clear();
adapter.notifyItemRangeRemoved(index-indexOffset, lastIndex-index);
return count;
displayItems.subList(index, lastIndex).clear();
adapter.notifyItemRangeRemoved(index, lastIndex-index);
}

protected void removeStatus(Status status){
Status removeStatus=status.getContentStatus();
String removeId=removeStatus.id;
iterateRemoveStatus(data, removeId);
iterateRemoveStatus(preloadedData, removeId);
int ancestorFirstIndex=-1, ancestorLastIndex=-1;
int offset=0;
for(int i=0;i<displayItems.size();i++){
StatusDisplayItem item=displayItems.get(i);
if(Objects.equals(item.contentStatusID, removeId)){
offset+=removeStatusDisplayItems(removeStatus, i, ancestorFirstIndex, ancestorLastIndex, offset);
if(item.getContentID().equals(removeId)){
removeStatusDisplayItems(removeStatus, i, ancestorFirstIndex, ancestorLastIndex);
ancestorFirstIndex=ancestorLastIndex=-1;
continue;
}
if(Objects.equals(item.contentStatusID, removeStatus.inReplyToId)){
if(item.getContentID().equals(removeStatus.inReplyToId)){
if(ancestorFirstIndex==-1) ancestorFirstIndex=i;
ancestorLastIndex=i;
}
}
iterateRemoveStatus(data, removeId);
iterateRemoveStatus(preloadedData, removeId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class AccountCardStatusDisplayItem extends StatusDisplayItem{
public CharSequence parsedName, parsedBio;

public AccountCardStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, String accountID, Account account, Notification notification){
super(parentID, null, parentFragment);
super(parentID, parentFragment);
this.account=account;
this.notification=notification;
avaRequest=new UrlImageLoaderRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AccountStatusDisplayItem extends StatusDisplayItem{
public final AccountViewModel account;

public AccountStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Account account){
super(parentID, null, parentFragment);
super(parentID, parentFragment);
this.account=new AccountViewModel(account, parentFragment.getAccountID());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AudioStatusDisplayItem extends StatusDisplayItem{
private final ImageLoaderRequest imageRequest;

public AudioStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Status status, Attachment attachment){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.status=status;
this.attachment=attachment;
imageRequest=new UrlImageLoaderRequest(TextUtils.isEmpty(attachment.previewUrl) ? status.account.avatarStatic : attachment.previewUrl, V.dp(100), V.dp(100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

public class DummyStatusDisplayItem extends StatusDisplayItem {

public DummyStatusDisplayItem(String parentID, String contentStatusID, BaseStatusListFragment<?> parentFragment) {
super(parentID, contentStatusID, parentFragment);
public DummyStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment) {
super(parentID, parentFragment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
private static final float ALPHA_DISABLED=0.55f;

public EmojiReactionsStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, Status status, String accountID, boolean hideEmpty, boolean forAnnouncement) {
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.status=status;
this.hideEmpty=hideEmpty;
this.forAnnouncement=forAnnouncement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ExtendedFooterStatusDisplayItem extends StatusDisplayItem{
private static final DateTimeFormatter TIME_FORMATTER=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT);

public ExtendedFooterStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, String accountID, Status status){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.status=status;
this.accountID=accountID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
public class FileStatusDisplayItem extends StatusDisplayItem{
private final Attachment attachment;

public FileStatusDisplayItem(String parentID, String contentStatusID, BaseStatusListFragment<?> parentFragment, Attachment attachment) {
super(parentID, contentStatusID, parentFragment);
public FileStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, Attachment attachment) {
super(parentID, parentFragment);
this.attachment=attachment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class FooterStatusDisplayItem extends StatusDisplayItem{
public boolean hideCounts;

public FooterStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Status status, String accountID){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.status=status;
this.accountID=accountID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GapStatusDisplayItem extends StatusDisplayItem{
private final Status status;

public GapStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, Status status){
super(parentID, null, parentFragment);
super(parentID, parentFragment);
this.status=status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class HashtagStatusDisplayItem extends StatusDisplayItem{
public final Hashtag tag;

public HashtagStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Hashtag tag){
super(parentID, null, parentFragment);
super(parentID, parentFragment);
this.tag=tag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class HeaderStatusDisplayItem extends StatusDisplayItem{
private Consumer<String> consumeReadAnnouncement;

public HeaderStatusDisplayItem(String parentID, Account user, Instant createdAt, BaseStatusListFragment parentFragment, String accountID, Status status, CharSequence extraText, Notification notification, ScheduledStatus scheduledStatus){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
AccountSession session = AccountSessionManager.get(accountID);
user=scheduledStatus != null ? session.self : user;
this.user=user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
private final UrlImageLoaderRequest imgRequest;

public LinkCardStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Status status){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.status=status;
if(status.card.image!=null)
imgRequest=new UrlImageLoaderRequest(status.card.image, 1000, 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class MediaGridStatusDisplayItem extends StatusDisplayItem{
public String sensitiveTitle;

public MediaGridStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, PhotoLayoutHelper.TiledLayoutResult tiledLayout, List<Attachment> attachments, Status status){
super(parentID, status.id, parentFragment);
super(parentID, parentFragment);
this.tiledLayout=tiledLayout;
this.viewPool=parentFragment.getAttachmentViewsPool();
this.attachments=attachments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class NotificationHeaderStatusDisplayItem extends StatusDisplayItem{
private final CharSequence timestamp;

public NotificationHeaderStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Notification notification, String accountID){
super(parentID, notification.status!=null ? notification.status.getContentStatus().id : null, parentFragment);
super(parentID, parentFragment);
this.notification=notification;
this.accountID=accountID;
this.timestamp=notification.createdAt==null ? null : UiUtils.formatRelativeTimestamp(context, notification.createdAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
public class PollFooterStatusDisplayItem extends StatusDisplayItem{
public final Poll poll;

public PollFooterStatusDisplayItem(String parentID, String contentStatusID, BaseStatusListFragment parentFragment, Poll poll){
super(parentID, contentStatusID, parentFragment);
public PollFooterStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Poll poll){
super(parentID, parentFragment);
this.poll=poll;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class PollOptionStatusDisplayItem extends StatusDisplayItem{
private final int optionIndex;
public final Poll poll;

public PollOptionStatusDisplayItem(String parentID, String contentStatusID, Poll poll, int optionIndex, BaseStatusListFragment parentFragment){
super(parentID, contentStatusID, parentFragment);
public PollOptionStatusDisplayItem(String parentID, Poll poll, int optionIndex, BaseStatusListFragment parentFragment){
super(parentID, parentFragment);
this.optionIndex=optionIndex;
option=poll.options.get(optionIndex);
this.poll=poll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ReblogOrReplyLineStatusDisplayItem(String parentID, BaseStatusListFragmen
}

public ReblogOrReplyLineStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, CharSequence text, List<Emoji> emojis, @DrawableRes int icon, StatusPrivacy visibility, @Nullable View.OnClickListener handleClick, CharSequence fullText, Status status) {
super(parentID, status.getContentStatus().id, parentFragment);
super(parentID, parentFragment);
SpannableStringBuilder ssb=new SpannableStringBuilder(text);
if(AccountSessionManager.get(parentFragment.getAccountID()).getLocalPreferences().customEmojiInNames)
HtmlParser.parseCustomEmoji(ssb, emojis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SectionHeaderStatusDisplayItem extends StatusDisplayItem{
public final Runnable onButtonClick;

public SectionHeaderStatusDisplayItem(BaseStatusListFragment parentFragment, String title, String buttonText, Runnable onButtonClick){
super("", null, parentFragment);
super("", parentFragment);
this.title=title;
this.buttonText=buttonText;
this.onButtonClick=onButtonClick;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class SpoilerStatusDisplayItem extends StatusDisplayItem{
private final int attachmentCount;

public SpoilerStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, String title, Status statusForContent, Type type){
super(parentID, statusForContent.id, parentFragment);
super(parentID, parentFragment);
this.status=statusForContent;
this.type=type;
this.attachmentCount=statusForContent.mediaAttachments.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.R;
import org.joinmastodon.android.api.session.AccountLocalPreferences;
Expand All @@ -22,6 +24,7 @@
import org.joinmastodon.android.fragments.HomeTabFragment;
import org.joinmastodon.android.fragments.ListTimelineFragment;
import org.joinmastodon.android.fragments.ProfileFragment;
import org.joinmastodon.android.fragments.StatusListFragment;
import org.joinmastodon.android.fragments.ThreadFragment;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Attachment;
Expand Down Expand Up @@ -53,7 +56,7 @@
import me.grishka.appkit.views.UsableRecyclerView;

public abstract class StatusDisplayItem{
public final String parentID, contentStatusID;
public final String parentID;
public final BaseStatusListFragment<?> parentFragment;
public boolean inset;
public int index;
Expand Down Expand Up @@ -83,12 +86,21 @@ public void setAncestryInfo(
this.isDirectDescendant = isDirectDescendant;
}

public StatusDisplayItem(String parentID, String contentStatusID, BaseStatusListFragment<?> parentFragment){
public StatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment){
this.parentID=parentID;
this.contentStatusID=contentStatusID;
this.parentFragment=parentFragment;
}

@NonNull
public String getContentID(){
if(parentFragment instanceof StatusListFragment slf){
Status s=slf.getContentStatusByID(parentID);
return s!=null ? s.id : parentID;
}else{
return parentID;
}
}

public abstract Type getType();

public int getImageCount(){
Expand Down Expand Up @@ -246,7 +258,7 @@ public static ArrayList<StatusDisplayItem> buildItems(BaseStatusListFragment<?>
}else if(!hasSpoiler && header!=null){
header.needBottomPadding=true;
}else if(hasSpoiler){
contentItems.add(new DummyStatusDisplayItem(parentID, statusForContent.id, fragment));
contentItems.add(new DummyStatusDisplayItem(parentID, fragment));
}

List<Attachment> imageAttachments=statusForContent.mediaAttachments.stream().filter(att->att.type.isImage()).collect(Collectors.toList());
Expand All @@ -270,11 +282,11 @@ else if(statusForContent.sensitive && AccountSessionManager.get(accountID).getLo
contentItems.add(new AudioStatusDisplayItem(parentID, fragment, statusForContent, att));
}
if(att.type==Attachment.Type.UNKNOWN){
contentItems.add(new FileStatusDisplayItem(parentID, statusForContent.id, fragment, att));
contentItems.add(new FileStatusDisplayItem(parentID, fragment, att));
}
}
if(statusForContent.poll!=null){
buildPollItems(parentID, statusForContent.id, fragment, statusForContent.poll, contentItems);
buildPollItems(parentID, fragment, statusForContent.poll, contentItems);
}
if(statusForContent.card!=null && statusForContent.mediaAttachments.isEmpty()){
contentItems.add(new LinkCardStatusDisplayItem(parentID, fragment, statusForContent));
Expand All @@ -301,7 +313,7 @@ else if(statusForContent.sensitive && AccountSessionManager.get(accountID).getLo
boolean inset=(flags & FLAG_INSET)!=0;
// add inset dummy so last content item doesn't clip out of inset bounds
if((inset || footer==null) && (flags & FLAG_CHECKABLE)==0){
items.add(new DummyStatusDisplayItem(parentID, statusForContent.id, fragment));
items.add(new DummyStatusDisplayItem(parentID, fragment));
// in case we ever need the dummy to display a margin for the media grid again:
// (i forgot why we apparently don't need this anymore)
// !contentItems.isEmpty() && contentItems
Expand All @@ -322,13 +334,13 @@ else if(statusForContent.sensitive && AccountSessionManager.get(accountID).getLo
new ArrayList<>(List.of(new WarningFilteredStatusDisplayItem(parentID, fragment, statusForContent, items, applyingFilter)));
}

public static void buildPollItems(String parentID, String contentStatusID, BaseStatusListFragment fragment, Poll poll, List<StatusDisplayItem> items){
public static void buildPollItems(String parentID, BaseStatusListFragment fragment, Poll poll, List<StatusDisplayItem> items){
int i=0;
for(Poll.Option opt:poll.options){
items.add(new PollOptionStatusDisplayItem(parentID, contentStatusID, poll, i, fragment));
items.add(new PollOptionStatusDisplayItem(parentID, poll, i, fragment));
i++;
}
items.add(new PollFooterStatusDisplayItem(parentID, contentStatusID, fragment, poll));
items.add(new PollFooterStatusDisplayItem(parentID, fragment, poll));
}

public enum Type{
Expand Down
Loading

0 comments on commit 7e80ed6

Please sign in to comment.