refactor(StatusDisplayItem): rebuild StatusItems with quote

This commit is contained in:
FineFindus 2024-07-06 12:47:40 +02:00
parent 6157d4942a
commit cdd9b0553f
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
2 changed files with 47 additions and 56 deletions

View File

@ -47,7 +47,6 @@ import org.joinmastodon.android.ui.displayitems.FooterStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.GapStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.HashtagStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.HeaderStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.LinkCardStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.MediaGridStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.PollFooterStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.PollOptionStatusDisplayItem;
@ -707,38 +706,22 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
toggleSpoiler(status, isForQuote, holder.getItemID());
}
public void onAddQuoteToStatus(Status status, Status parentStatus) {
int cardIndex=-1;
int textIndex=-1;
int i=0;
for(StatusDisplayItem item:displayItems){
if(item.parentID.equals(parentStatus.id)){
if(item instanceof LinkCardStatusDisplayItem){
cardIndex=i;
}else if(item instanceof TextStatusDisplayItem){
textIndex=i;
}
public void updateStatusWithQuote(Status status) {
int startIndex=-1;
int endIndex=-1;
for(int i=0; i<displayItems.size(); i++){
StatusDisplayItem item = displayItems.get(i);
if(item.parentID.equals(status.id)) {
startIndex= startIndex==-1 ? i : startIndex;
endIndex=i;
}
i++;
}
int flags= (StatusDisplayItem.FLAG_NO_FOOTER | StatusDisplayItem.FLAG_INSET | StatusDisplayItem.FLAG_NO_EMOJI_REACTIONS | StatusDisplayItem.FLAG_IS_FOR_QUOTE);
if (!GlobalUserPreferences.showMediaPreview)
flags |= StatusDisplayItem.FLAG_NO_MEDIA_PREVIEW;
if (cardIndex!=-1) {
ArrayList<StatusDisplayItem> items=StatusDisplayItem.buildItems(this, status, accountID, parentStatus, knownAccounts, null, flags);
displayItems.remove(cardIndex);
adapter.notifyItemRemoved(cardIndex);
displayItems.addAll(cardIndex, items);
adapter.notifyItemRangeInserted(cardIndex, items.size());
return;
}
if (textIndex!=-1) {
ArrayList<StatusDisplayItem> items=StatusDisplayItem.buildItems(this, status, accountID, parentStatus, knownAccounts, null, flags);
displayItems.addAll(textIndex+1, items);
adapter.notifyItemRangeInserted(textIndex+1, items.size());
if (startIndex!=-1 && endIndex!=-1) {
ArrayList<StatusDisplayItem> items=StatusDisplayItem.buildItems(this, status, accountID, status, knownAccounts, null, 0);
displayItems.subList(startIndex, endIndex+1).clear();
displayItems.addAll(startIndex, items);
adapter.notifyItemRangeChanged(startIndex, items.size());
}
}

View File

@ -48,6 +48,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Matcher;
@ -328,6 +329,8 @@ public abstract class StatusDisplayItem{
if(!statusForContent.mediaAttachments.isEmpty() && statusForContent.poll==null) // add spacing if immediately preceded by attachment
contentItems.add(new DummyStatusDisplayItem(parentID, fragment));
contentItems.addAll(buildItems(fragment, statusForContent.quote, accountID, parentObject, knownAccounts, filterContext, FLAG_NO_FOOTER|FLAG_INSET|FLAG_NO_EMOJI_REACTIONS|FLAG_IS_FOR_QUOTE));
} else {
tryAddNonOfficialQuote(statusForContent, fragment, accountID);
}
if(contentItems!=items && statusForContent.spoilerRevealed){
items.addAll(contentItems);
@ -381,32 +384,6 @@ public abstract class StatusDisplayItem{
}
}
// I actually forgot where I took this, but it works
Matcher matcher = QUOTE_PATTERN.matcher(statusForContent.getStrippedText());
String lastUrl = null;
while (matcher.find()) {
lastUrl = matcher.group(0);
// The regex doesn't capture the scheme, so I add one here manually, so that the looksLikeFediverseUrlMethod actually works
lastUrl = "https://" + lastUrl;
}
if (UiUtils.looksLikeFediverseUrl(lastUrl) && statusForContent.quote == null) {
new GetSearchResults(lastUrl, GetSearchResults.Type.STATUSES, true, null, 0, 0).setCallback(new Callback<>(){
@Override
public void onSuccess(SearchResults results){
if (!results.statuses.isEmpty()){
fragment.onAddQuoteToStatus(results.statuses.get(0), statusForContent);
}
}
@Override
public void onError(ErrorResponse error){
// Nothing
}
}).exec(accountID);
}
List<StatusDisplayItem> nonGapItems=gap!=null ? items.subList(0, items.size()-1) : items;
WarningFilteredStatusDisplayItem warning=applyingFilter==null ? null :
new WarningFilteredStatusDisplayItem(parentID, fragment, statusForContent, nonGapItems, applyingFilter);
@ -429,6 +406,37 @@ public abstract class StatusDisplayItem{
items.add(new PollFooterStatusDisplayItem(parentID, fragment, poll, status));
}
/**
* Tries to adds a non-official quote to a status.
* A non-official quote is a quote on an instance that does not support quotes officially.
*/
private static void tryAddNonOfficialQuote(Status status, BaseStatusListFragment fragment, String accountID) {
Matcher matcher=QUOTE_PATTERN.matcher(status.getStrippedText());
String quoteURL=null;
while (matcher.find()) {
quoteURL=matcher.group(0);
}
quoteURL="https://"+quoteURL;
if (UiUtils.looksLikeFediverseUrl(quoteURL)) {
new GetSearchResults(quoteURL, GetSearchResults.Type.STATUSES, true, null, 0, 0).setCallback(new Callback<>(){
@Override
public void onSuccess(SearchResults results){
if (!results.statuses.isEmpty()){
status.quote=results.statuses.get(0);
fragment.updateStatusWithQuote(status);
}
}
@Override
public void onError(ErrorResponse error){
Log.w("StatusDisplayItem", "onError: failed to find quote status" +error);
}
}).exec(accountID);
}
}
public enum Type{
HEADER,
REBLOG_OR_REPLY_LINE,