`fediverse:creator` in link cards (AND-182)

This commit is contained in:
Grishka 2024-06-15 00:32:56 +03:00
parent 264f9a933c
commit 115a6378d3
9 changed files with 270 additions and 74 deletions

View File

@ -36,6 +36,7 @@ public class Card extends BaseModel{
public String blurhash;
public List<History> history;
public Instant publishedAt;
public Account authorAccount;
public transient Drawable blurhashPlaceholder;
@ -49,6 +50,8 @@ public class Card extends BaseModel{
if(placeholder!=null)
blurhashPlaceholder=new BlurHashDrawable(placeholder, width, height);
}
if(authorAccount!=null)
authorAccount.postprocess();
}
@Override

View File

@ -5,30 +5,41 @@ import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.R;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.fragments.BaseStatusListFragment;
import org.joinmastodon.android.fragments.ProfileFragment;
import org.joinmastodon.android.model.Card;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.ui.OutlineProviders;
import org.joinmastodon.android.ui.drawables.BlurhashCrossfadeDrawable;
import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.utils.CustomEmojiHelper;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.parceler.Parcels;
import java.util.Objects;
import me.grishka.appkit.Nav;
import me.grishka.appkit.imageloader.ImageLoaderViewHolder;
import me.grishka.appkit.imageloader.requests.ImageLoaderRequest;
import me.grishka.appkit.imageloader.requests.UrlImageLoaderRequest;
import me.grishka.appkit.utils.V;
public class LinkCardStatusDisplayItem extends StatusDisplayItem{
private final Status status;
private final UrlImageLoaderRequest imgRequest;
private final UrlImageLoaderRequest imgRequest, authorAvaRequest;
private final SpannableStringBuilder parsedAuthorName;
private final CustomEmojiHelper authorNameEmojiHelper=new CustomEmojiHelper();
public LinkCardStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Status status){
super(parentID, parentFragment);
@ -37,6 +48,17 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
imgRequest=new UrlImageLoaderRequest(status.card.image, 1000, 1000);
else
imgRequest=null;
if(status.card.authorAccount!=null){
parsedAuthorName=new SpannableStringBuilder(status.card.authorAccount.displayName);
if(AccountSessionManager.get(parentFragment.getAccountID()).getLocalPreferences().customEmojiInNames)
HtmlParser.parseCustomEmoji(parsedAuthorName, status.card.authorAccount.emojis);
authorNameEmojiHelper.setText(parsedAuthorName);
authorAvaRequest=new UrlImageLoaderRequest(GlobalUserPreferences.playGifs ? status.card.authorAccount.avatar : status.card.authorAccount.avatarStatic, V.dp(50), V.dp(50));
}else{
parsedAuthorName=null;
authorAvaRequest=null;
}
}
@Override
@ -46,21 +68,26 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
@Override
public int getImageCount(){
return imgRequest==null ? 0 : 1;
return 1+(status.card.authorAccount!=null ? (1+authorNameEmojiHelper.getImageCount()) : 0);
}
@Override
public ImageLoaderRequest getImageRequest(int index){
return imgRequest;
return switch(index){
case 0 -> imgRequest;
case 1 -> authorAvaRequest;
default -> authorNameEmojiHelper.getImageRequest(index-2);
};
}
public static class Holder extends StatusDisplayItem.Holder<LinkCardStatusDisplayItem> implements ImageLoaderViewHolder{
private final TextView title, description, domain, timestamp;
private final ImageView photo;
private final TextView title, description, domain, timestamp, authorBefore, authorAfter, authorName;
private final ImageView photo, authorAva;
private BlurhashCrossfadeDrawable crossfadeDrawable=new BlurhashCrossfadeDrawable();
private boolean didClear;
private final View inner;
private final View inner, authorFooter, authorChip;
private final boolean isLarge;
private final Drawable logoIcon;
public Holder(Context context, ViewGroup parent, boolean isLarge){
super(context, isLarge ? R.layout.display_item_link_card : R.layout.display_item_link_card_compact, parent);
@ -71,9 +98,26 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
timestamp=findViewById(R.id.timestamp);
photo=findViewById(R.id.photo);
inner=findViewById(R.id.inner);
authorBefore=findViewById(R.id.author_before);
authorAfter=findViewById(R.id.author_after);
authorName=findViewById(R.id.author_name);
authorAva=findViewById(R.id.author_ava);
authorChip=findViewById(R.id.author_chip);
authorFooter=findViewById(R.id.author_footer);
inner.setOnClickListener(this::onClick);
inner.setOutlineProvider(OutlineProviders.roundedRect(12));
inner.setOutlineProvider(OutlineProviders.roundedRect(8));
inner.setClipToOutline(true);
if(!isLarge){
photo.setOutlineProvider(OutlineProviders.roundedRect(4));
photo.setClipToOutline(true);
}
authorAva.setOutlineProvider(OutlineProviders.roundedRect(3));
authorAva.setClipToOutline(true);
authorChip.setOnClickListener(this::onAuthorChipClick);
logoIcon=context.getResources().getDrawable(R.drawable.ic_ntf_logo, context.getTheme()).mutate();
logoIcon.setBounds(0, 0, V.dp(17), V.dp(17));
}
@SuppressLint("SetTextI18n")
@ -86,11 +130,36 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
description.setVisibility(TextUtils.isEmpty(card.description) ? View.GONE : View.VISIBLE);
}
String cardDomain=HtmlParser.normalizeDomain(Objects.requireNonNull(Uri.parse(card.url).getHost()));
if(isLarge && !TextUtils.isEmpty(card.authorName)){
domain.setText(itemView.getContext().getString(R.string.article_by_author, card.authorName)+" · "+cardDomain);
}else{
domain.setText(cardDomain);
domain.setText(TextUtils.isEmpty(card.providerName) ? cardDomain : card.providerName);
if(card.authorAccount!=null){
authorFooter.setVisibility(View.VISIBLE);
authorChip.setVisibility(View.VISIBLE);
authorBefore.setVisibility(View.VISIBLE);
String[] authorParts=itemView.getContext().getString(R.string.article_by_author, "{author}").split("\\{author\\}");
String before=authorParts[0].trim();
String after=authorParts.length>1 ? authorParts[1].trim() : "";
if(!TextUtils.isEmpty(before)){
authorBefore.setText(before);
}
if(TextUtils.isEmpty(after)){
authorAfter.setVisibility(View.GONE);
}else{
authorAfter.setVisibility(View.VISIBLE);
authorAfter.setText(after);
}
authorName.setText(item.parsedAuthorName);
authorBefore.setCompoundDrawablesRelative(logoIcon, null, null, null);
}else if(!TextUtils.isEmpty(card.authorName)){
authorFooter.setVisibility(View.VISIBLE);
authorBefore.setVisibility(View.VISIBLE);
authorBefore.setCompoundDrawables(null, null, null, null);
authorChip.setVisibility(View.GONE);
authorAfter.setVisibility(View.GONE);
authorBefore.setText(itemView.getContext().getString(R.string.article_by_author, card.authorName));
}else{
authorFooter.setVisibility(View.GONE);
}
if(card.publishedAt!=null){
timestamp.setVisibility(View.VISIBLE);
timestamp.setText(" · "+UiUtils.formatRelativeTimestamp(itemView.getContext(), card.publishedAt));
@ -119,6 +188,7 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
@Override
public void setImage(int index, Drawable drawable){
if(index==0){
crossfadeDrawable.setImageDrawable(drawable);
if(didClear)
crossfadeDrawable.animateAlpha(0f);
@ -128,16 +198,33 @@ public class LinkCardStatusDisplayItem extends StatusDisplayItem{
photo.setImageDrawable(null);
photo.setImageDrawable(crossfadeDrawable);
}
}else if(index==1){
authorAva.setImageDrawable(drawable);
}else{
item.authorNameEmojiHelper.setImageDrawable(index-2, drawable);
authorName.invalidate();
}
}
@Override
public void clearImage(int index){
if(index==0){
crossfadeDrawable.setCrossfadeAlpha(1f);
didClear=true;
}else{
setImage(index, null);
}
}
private void onClick(View v){
UiUtils.openURL(itemView.getContext(), item.parentFragment.getAccountID(), item.status.card.url, item.status);
}
private void onAuthorChipClick(View v){
Bundle args=new Bundle();
args.putString("account", item.parentFragment.getAccountID());
args.putParcelable("profileAccount", Parcels.wrap(item.status.card.authorAccount));
Nav.go(item.parentFragment.getActivity(), ProfileFragment.class, args);
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="top">
<shape>
<solid android:color="?colorM3OutlineVariant"/>
<size android:height="1dp"/>
</shape>
</item>
</layer-list>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/m3_on_surface_overlay">
<item>
<shape>
<solid android:color="?colorM3SurfaceVariant"/>
<corners android:radius="6dp"/>
</shape>
</item>
</ripple>

View File

@ -2,13 +2,13 @@
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/m3_on_surface_overlay">
<item android:id="@android:id/mask">
<shape>
<corners android:radius="11dp"/>
<corners android:radius="7dp"/>
<solid android:color="#000"/>
</shape>
</item>
<item>
<shape>
<corners android:radius="11dp"/>
<corners android:radius="7dp"/>
<stroke android:color="?colorM3OutlineVariant" android:width="1dp"/>
</shape>
</item>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="?colorM3OutlineVariant" android:width="0.5dp"/>
<corners android:radius="2.5dp"/>
</shape>

View File

@ -14,7 +14,10 @@
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:foreground="@drawable/fg_link_card"
android:padding="1dp"
android:paddingHorizontal="1dp"
android:paddingTop="1dp"
android:paddingBottom="16dp"
android:clipToPadding="false"
android:maxWidth="400dp">
<org.joinmastodon.android.ui.views.FixedAspectRatioImageView
android:id="@+id/photo"
@ -24,14 +27,42 @@
android:importantForAccessibility="no"
app:aspectRatio="1.7777777778"
tools:src="#0f0"/>
<org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="2dp">
<TextView
android:id="@+id/domain"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_small"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
tools:text="example.com"/>
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_small"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
tools:text="example.com"/>
</org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:textAppearance="@style/m3_title_medium"
android:maxLines="3"
android:maxLines="4"
android:ellipsize="end"
android:textColor="?colorM3OnSurface"
tools:text="Link title"/>
@ -45,33 +76,13 @@
android:textAppearance="@style/m3_body_medium"
android:textColor="?colorM3OnSurface"
tools:text="Link description"/>
<org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout
<include
layout="@layout/link_card_author_footer"
android:id="@+id/author_footer"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginHorizontal="16dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp">
<TextView
android:id="@+id/domain"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_medium"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
tools:text="example.com"/>
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_medium"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
tools:text="example.com"/>
</org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout>
android:layout_marginBottom="-16dp"/>
</LinearLayout>
</FrameLayout>

View File

@ -10,7 +10,7 @@
<RelativeLayout
android:id="@+id/inner"
android:layout_width="match_parent"
android:layout_height="128dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:foreground="@drawable/fg_link_card"
@ -18,40 +18,30 @@
android:maxWidth="400dp">
<ImageView
android:id="@+id/photo"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_alignParentEnd="true"
android:scaleType="centerCrop"
android:importantForAccessibility="no"
android:layout_marginVertical="12dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="16dp"
tools:src="#0f0"/>
<TextView
android:id="@+id/title"
<org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout
android:id="@+id/domain_and_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/photo"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:textAppearance="@style/m3_title_medium"
android:maxLines="3"
android:ellipsize="end"
android:textColor="?colorM3OnSurface"
tools:text="Link title"/>
<org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@id/photo"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp">
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="2dp">
<TextView
android:id="@+id/domain"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_medium"
android:textAppearance="@style/m3_body_small"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
@ -61,13 +51,32 @@
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="@style/m3_body_medium"
android:textAppearance="@style/m3_body_small"
android:textColor="?colorM3OnSurfaceVariant"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
tools:text="example.com"/>
</org.joinmastodon.android.ui.views.HeaderSubtitleLinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/domain_and_time"
android:layout_toStartOf="@id/photo"
android:layout_marginStart="12dp"
android:textAppearance="@style/m3_title_medium"
android:maxLines="3"
android:ellipsize="end"
android:textColor="?colorM3OnSurface"
tools:text="Link title"/>
<include
layout="@layout/link_card_author_footer"
android:id="@+id/author_footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/photo"/>
</RelativeLayout>
</FrameLayout>

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingHorizontal="16dp"
android:gravity="center_vertical"
android:background="@drawable/bg_link_card_author">
<TextView
android:id="@+id/author_before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="@style/m3_body_medium"
android:textColor="?colorM3OnSurfaceVariant"
android:drawablePadding="8dp"
android:drawableTint="?colorM3Outline"
tools:text="By"/>
<LinearLayout
android:id="@+id/author_chip"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:orientation="horizontal"
android:layout_marginHorizontal="8dp"
android:paddingHorizontal="6dp"
android:gravity="center_vertical"
android:background="@drawable/bg_link_card_author_chip">
<ImageView
android:id="@+id/author_ava"
android:layout_width="16dp"
android:layout_height="16dp"
android:importantForAccessibility="no"
android:foreground="@drawable/fg_link_card_author_chip_ava"
tools:src="#0f0"/>
<TextView
android:id="@+id/author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="14dp"
android:fontFamily="sans-serif-medium"
android:textColor="?colorM3OnSurface"
android:layout_marginStart="4dp"
tools:text="John Appleseed"/>
</LinearLayout>
<TextView
android:id="@+id/author_after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="@style/m3_body_medium"
android:textColor="?colorM3OnSurfaceVariant"
tools:text="some languages might need this"/>
</LinearLayout>