feat(crash-status-display-item): makes the app not crash when creating the status display items

This is the first part of a 2 part patch, because crashes can still happpen when the item is being "binded", which makes it necessary to handle those errors as well, which we currently DON'T do.

Also the Error item is still needing to be better, so there is also that to work on
This commit is contained in:
LucasGGamerM 2024-05-19 10:20:15 -03:00
parent 44e3e5faaf
commit a082a3d325
2 changed files with 242 additions and 186 deletions

View File

@ -0,0 +1,50 @@
package org.joinmastodon.android.ui.displayitems;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.joinmastodon.android.R;
import org.joinmastodon.android.fragments.BaseStatusListFragment;
import org.joinmastodon.android.model.Attachment;
import org.joinmastodon.android.ui.utils.UiUtils;
public class ErrorStatusDisplayItem extends StatusDisplayItem{
private final Exception exception;
public ErrorStatusDisplayItem(String parentID, BaseStatusListFragment<?> parentFragment, Exception exception) {
super(parentID, parentFragment);
this.exception=exception;
}
@Override
public Type getType() {
return Type.ERROR_ITEM;
}
public static class Holder extends StatusDisplayItem.Holder<ErrorStatusDisplayItem> {
private final TextView title, domain;
public Holder(Context context, ViewGroup parent) {
super(context, R.layout.display_item_file, parent);
title=findViewById(R.id.title);
domain=findViewById(R.id.domain);
findViewById(R.id.inner).setOnClickListener(this::onClick);
}
@Override
public void onBind(ErrorStatusDisplayItem item) {
title.setText(item.exception.getMessage());
// title.setEllipsize(item.attachment.description != null ? TextUtils.TruncateAt.END : TextUtils.TruncateAt.MIDDLE);
// domain.setText(url.getHost());
}
private void onClick(View v) {
// UiUtils.openURL(itemView.getContext(), item.parentFragment.getAccountID(), getUrl());
}
}
}

View File

@ -139,6 +139,7 @@ public abstract class StatusDisplayItem{
case SPOILER, FILTER_SPOILER -> new SpoilerStatusDisplayItem.Holder(activity, parent, type);
case SECTION_HEADER -> null; // new SectionHeaderStatusDisplayItem.Holder(activity, parent);
case NOTIFICATION_HEADER -> new NotificationHeaderStatusDisplayItem.Holder(activity, parent);
case ERROR_ITEM -> new ErrorStatusDisplayItem.Holder(activity, parent);
case DUMMY -> new DummyStatusDisplayItem.Holder(activity);
};
}
@ -164,6 +165,7 @@ public abstract class StatusDisplayItem{
Status statusForContent=status.getContentStatus();
Bundle args=new Bundle();
args.putString("account", accountID);
try{
ScheduledStatus scheduledStatus=parentObject instanceof ScheduledStatus s ? s : null;
HeaderStatusDisplayItem header=null;
@ -376,6 +378,9 @@ public abstract class StatusDisplayItem{
? List.of(warning, gap)
: Collections.singletonList(warning)
);
} catch(Exception e) {
return new ArrayList<>(Collections.singletonList(new ErrorStatusDisplayItem(parentID, fragment, e)));
}
}
public static void buildPollItems(String parentID, BaseStatusListFragment fragment, Poll poll, Status status, List<StatusDisplayItem> items){
@ -411,6 +416,7 @@ public abstract class StatusDisplayItem{
SECTION_HEADER,
HEADER_CHECKABLE,
NOTIFICATION_HEADER,
ERROR_ITEM,
FILTER_SPOILER,
DUMMY
}