mirror of
				https://framagit.org/tom79/fedilab-tube
				synced 2025-06-05 21:09:11 +02:00 
			
		
		
		
	Truncate long descriptions for instances with a way to expand them.
This commit is contained in:
		| @@ -64,6 +64,7 @@ public class InstanceData { | ||||
|         @SerializedName("isNSFW") | ||||
|         private boolean isNSFW; | ||||
|         private SpannableStringBuilder spannableStringBuilder; | ||||
|         private boolean truncatedDescription = true; | ||||
|  | ||||
|         public boolean isAutoBlacklistUserVideosEnabled() { | ||||
|             return autoBlacklistUserVideosEnabled; | ||||
| @@ -240,6 +241,14 @@ public class InstanceData { | ||||
|         public void setCategories(List<Integer> categories) { | ||||
|             this.categories = categories; | ||||
|         } | ||||
|  | ||||
|         public boolean isTruncatedDescription() { | ||||
|             return truncatedDescription; | ||||
|         } | ||||
|  | ||||
|         public void setTruncatedDescription(boolean truncatedDescription) { | ||||
|             this.truncatedDescription = truncatedDescription; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static class InstanceInfo { | ||||
| @@ -277,6 +286,7 @@ public class InstanceData { | ||||
|         @SerializedName("terms") | ||||
|         private String terms; | ||||
|         private String host; | ||||
|         private boolean truncatedDescription = true; | ||||
|  | ||||
|         public AboutInstance() { | ||||
|         } | ||||
| @@ -329,6 +339,14 @@ public class InstanceData { | ||||
|             this.host = host; | ||||
|         } | ||||
|  | ||||
|         public boolean isTruncatedDescription() { | ||||
|             return truncatedDescription; | ||||
|         } | ||||
|  | ||||
|         public void setTruncatedDescription(boolean truncatedDescription) { | ||||
|             this.truncatedDescription = truncatedDescription; | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         public int describeContents() { | ||||
|             return 0; | ||||
|   | ||||
| @@ -26,7 +26,9 @@ import android.os.Handler; | ||||
| import android.os.Looper; | ||||
| import android.text.Html; | ||||
| import android.text.SpannableString; | ||||
| import android.text.TextUtils; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| @@ -89,6 +91,31 @@ public class AboutInstanceAdapter extends RecyclerView.Adapter<RecyclerView.View | ||||
|         holder.binding.aboutInstanceHost.setText(aboutInstance.getHost()); | ||||
|  | ||||
|         SpannableString spannableString; | ||||
|  | ||||
|         if (aboutInstance.getShortDescription() != null && aboutInstance.getShortDescription().trim().length() > 0) { | ||||
|             if (aboutInstance.isTruncatedDescription()) { | ||||
|                 holder.binding.aboutInstanceDescription.setMaxLines(3); | ||||
|                 holder.binding.aboutInstanceDescription.setEllipsize(TextUtils.TruncateAt.END); | ||||
|  | ||||
|             } else { | ||||
|                 holder.binding.aboutInstanceDescription.setMaxLines(Integer.MAX_VALUE); | ||||
|                 holder.binding.aboutInstanceDescription.setEllipsize(null); | ||||
|             } | ||||
|             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | ||||
|                 spannableString = new SpannableString(Html.fromHtml(aboutInstance.getShortDescription(), FROM_HTML_MODE_LEGACY)); | ||||
|             else | ||||
|                 spannableString = new SpannableString(Html.fromHtml(aboutInstance.getShortDescription())); | ||||
|             holder.binding.aboutInstanceDescription.setText(spannableString, TextView.BufferType.SPANNABLE); | ||||
|  | ||||
|             holder.binding.aboutInstanceDescription.setOnClickListener(v -> { | ||||
|                 aboutInstance.setTruncatedDescription(!aboutInstance.isTruncatedDescription()); | ||||
|                 notifyItemChanged(i); | ||||
|             }); | ||||
|             holder.binding.aboutInstanceDescription.setVisibility(View.VISIBLE); | ||||
|         } else { | ||||
|             holder.binding.aboutInstanceDescription.setVisibility(View.GONE); | ||||
|         } | ||||
|  | ||||
|         if (aboutInstance.getShortDescription() != null) { | ||||
|             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | ||||
|                 spannableString = new SpannableString(Html.fromHtml(aboutInstance.getShortDescription(), FROM_HTML_MODE_LEGACY)); | ||||
|   | ||||
| @@ -19,12 +19,15 @@ import android.app.Activity; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.net.Uri; | ||||
| import android.os.Build; | ||||
| import android.text.Html; | ||||
| import android.text.SpannableString; | ||||
| import android.text.SpannableStringBuilder; | ||||
| import android.text.Spanned; | ||||
| import android.text.TextUtils; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.Button; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
| @@ -35,9 +38,11 @@ import java.util.List; | ||||
|  | ||||
| import app.fedilab.fedilabtube.R; | ||||
| import app.fedilab.fedilabtube.client.data.InstanceData.Instance; | ||||
| import app.fedilab.fedilabtube.databinding.DrawerInstanceBinding; | ||||
| import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan; | ||||
|  | ||||
| import static android.app.Activity.RESULT_OK; | ||||
| import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; | ||||
| import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation; | ||||
|  | ||||
|  | ||||
| @@ -56,8 +61,8 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde | ||||
|     @Override | ||||
|     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||||
|         context = parent.getContext(); | ||||
|         LayoutInflater layoutInflater = LayoutInflater.from(context); | ||||
|         return new ViewHolder(layoutInflater.inflate(R.layout.drawer_instance, parent, false)); | ||||
|         DrawerInstanceBinding itemBinding = DrawerInstanceBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false); | ||||
|         return new ViewHolder(itemBinding); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -66,14 +71,33 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde | ||||
|         final ViewHolder holder = (ViewHolder) viewHolder; | ||||
|  | ||||
|         if (instance.getShortDescription() != null && instance.getShortDescription().trim().length() > 0) { | ||||
|             holder.description.setText(instance.getShortDescription()); | ||||
|             holder.description.setVisibility(View.VISIBLE); | ||||
|             if (instance.isTruncatedDescription()) { | ||||
|                 holder.binding.description.setText(instance.getShortDescription()); | ||||
|                 holder.binding.description.setMaxLines(3); | ||||
|                 holder.binding.description.setEllipsize(TextUtils.TruncateAt.END); | ||||
|  | ||||
|             } else { | ||||
|                 holder.binding.description.setText(instance.getShortDescription()); | ||||
|                 holder.binding.description.setMaxLines(Integer.MAX_VALUE); | ||||
|                 holder.binding.description.setEllipsize(null); | ||||
|             } | ||||
|             SpannableString spannableString; | ||||
|             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | ||||
|                 spannableString = new SpannableString(Html.fromHtml(instance.getShortDescription(), FROM_HTML_MODE_LEGACY)); | ||||
|             else | ||||
|                 spannableString = new SpannableString(Html.fromHtml(instance.getShortDescription())); | ||||
|             holder.binding.description.setText(spannableString, TextView.BufferType.SPANNABLE); | ||||
|             holder.binding.description.setOnClickListener(v -> { | ||||
|                 instance.setTruncatedDescription(!instance.isTruncatedDescription()); | ||||
|                 notifyItemChanged(position); | ||||
|             }); | ||||
|             holder.binding.description.setVisibility(View.VISIBLE); | ||||
|         } else { | ||||
|             holder.description.setVisibility(View.GONE); | ||||
|             holder.binding.description.setVisibility(View.GONE); | ||||
|         } | ||||
|  | ||||
|         holder.name.setText(instance.getName()); | ||||
|         holder.host.setText(instance.getHost()); | ||||
|         holder.binding.name.setText(instance.getName()); | ||||
|         holder.binding.host.setText(instance.getHost()); | ||||
|  | ||||
|  | ||||
|         SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); | ||||
| @@ -95,7 +119,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde | ||||
|             } | ||||
|         } | ||||
|         if (instance.getSpannableStringBuilder() != null) { | ||||
|             holder.tags.setText(instance.getSpannableStringBuilder()); | ||||
|             holder.binding.tags.setText(instance.getSpannableStringBuilder()); | ||||
|         } | ||||
|  | ||||
|         if (peertubeInformation != null && peertubeInformation.getLanguages() != null) { | ||||
| @@ -107,21 +131,21 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde | ||||
|                 } | ||||
|             } | ||||
|             if (languages.toString().trim().length() == 0) { | ||||
|                 holder.languages.setVisibility(View.GONE); | ||||
|                 holder.binding.languages.setVisibility(View.GONE); | ||||
|             } else { | ||||
|                 holder.languages.setText(languages); | ||||
|                 holder.languages.setVisibility(View.VISIBLE); | ||||
|                 holder.binding.languages.setText(languages); | ||||
|                 holder.binding.languages.setVisibility(View.VISIBLE); | ||||
|             } | ||||
|         } | ||||
|         if (instance.getDefaultNSFWPolicy().compareTo("do_not_list") != 0) { | ||||
|             holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy())); | ||||
|             holder.sensitive_content.setVisibility(View.VISIBLE); | ||||
|             holder.binding.sensitiveContent.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy())); | ||||
|             holder.binding.sensitiveContent.setVisibility(View.VISIBLE); | ||||
|         } else { | ||||
|             holder.sensitive_content.setVisibility(View.GONE); | ||||
|             holder.binding.sensitiveContent.setVisibility(View.GONE); | ||||
|         } | ||||
|         holder.followers_instance.setText(context.getString(R.string.followers_instance, String.valueOf(instance.getTotalInstanceFollowers()))); | ||||
|         holder.binding.followersInstance.setText(context.getString(R.string.followers_instance, String.valueOf(instance.getTotalInstanceFollowers()))); | ||||
|  | ||||
|         holder.pickup.setOnClickListener(v -> { | ||||
|         holder.binding.pickup.setOnClickListener(v -> { | ||||
|             Intent data = new Intent(); | ||||
|             String instanceHost = instance.getHost(); | ||||
|             data.setData(Uri.parse(instanceHost)); | ||||
| @@ -142,21 +166,12 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde | ||||
|  | ||||
|  | ||||
|     static class ViewHolder extends RecyclerView.ViewHolder { | ||||
|         DrawerInstanceBinding binding; | ||||
|  | ||||
|         Button pickup; | ||||
|         TextView name, host, description; | ||||
|         TextView tags, followers_instance, languages, sensitive_content; | ||||
|  | ||||
|         public ViewHolder(@NonNull View itemView) { | ||||
|             super(itemView); | ||||
|             pickup = itemView.findViewById(R.id.pickup); | ||||
|             name = itemView.findViewById(R.id.name); | ||||
|             host = itemView.findViewById(R.id.host); | ||||
|             description = itemView.findViewById(R.id.description); | ||||
|             followers_instance = itemView.findViewById(R.id.followers_instance); | ||||
|             tags = itemView.findViewById(R.id.tags); | ||||
|             languages = itemView.findViewById(R.id.languages); | ||||
|             sensitive_content = itemView.findViewById(R.id.sensitive_content); | ||||
|         ViewHolder(DrawerInstanceBinding itemView) { | ||||
|             super(itemView.getRoot()); | ||||
|             binding = itemView; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user