|
| 1 | +package org.schabi.newpipe.fragments.detail; |
| 2 | + |
| 3 | +import static android.text.TextUtils.isEmpty; |
| 4 | +import static org.schabi.newpipe.extractor.utils.Utils.isBlank; |
| 5 | +import static org.schabi.newpipe.util.text.TextLinkifier.SET_LINK_MOVEMENT_METHOD; |
| 6 | + |
| 7 | +import android.os.Bundle; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.LinearLayout; |
| 12 | + |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import androidx.annotation.Nullable; |
| 15 | +import androidx.annotation.StringRes; |
| 16 | +import androidx.appcompat.widget.TooltipCompat; |
| 17 | +import androidx.core.text.HtmlCompat; |
| 18 | + |
| 19 | +import com.google.android.material.chip.Chip; |
| 20 | + |
| 21 | +import org.schabi.newpipe.BaseFragment; |
| 22 | +import org.schabi.newpipe.R; |
| 23 | +import org.schabi.newpipe.databinding.FragmentDescriptionBinding; |
| 24 | +import org.schabi.newpipe.databinding.ItemMetadataBinding; |
| 25 | +import org.schabi.newpipe.databinding.ItemMetadataTagsBinding; |
| 26 | +import org.schabi.newpipe.extractor.StreamingService; |
| 27 | +import org.schabi.newpipe.extractor.stream.Description; |
| 28 | +import org.schabi.newpipe.util.NavigationHelper; |
| 29 | +import org.schabi.newpipe.util.external_communication.ShareUtils; |
| 30 | +import org.schabi.newpipe.util.text.TextLinkifier; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import io.reactivex.rxjava3.disposables.CompositeDisposable; |
| 35 | + |
| 36 | +public abstract class BaseDescriptionFragment extends BaseFragment { |
| 37 | + private final CompositeDisposable descriptionDisposables = new CompositeDisposable(); |
| 38 | + protected FragmentDescriptionBinding binding; |
| 39 | + |
| 40 | + @Override |
| 41 | + public View onCreateView(@NonNull final LayoutInflater inflater, |
| 42 | + @Nullable final ViewGroup container, |
| 43 | + @Nullable final Bundle savedInstanceState) { |
| 44 | + binding = FragmentDescriptionBinding.inflate(inflater, container, false); |
| 45 | + setupDescription(); |
| 46 | + setupMetadata(inflater, binding.detailMetadataLayout); |
| 47 | + addTagsMetadataItem(inflater, binding.detailMetadataLayout); |
| 48 | + return binding.getRoot(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onDestroy() { |
| 53 | + descriptionDisposables.clear(); |
| 54 | + super.onDestroy(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the description to display. |
| 59 | + * @return description object |
| 60 | + */ |
| 61 | + @Nullable |
| 62 | + protected abstract Description getDescription(); |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the streaming service. Used for generating description links. |
| 66 | + * @return streaming service |
| 67 | + */ |
| 68 | + @Nullable |
| 69 | + protected abstract StreamingService getService(); |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the streaming service ID. Used for tag links. |
| 73 | + * @return service ID |
| 74 | + */ |
| 75 | + protected abstract int getServiceId(); |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the URL of the described video or audio, used to generate description links. |
| 79 | + * @return stream URL |
| 80 | + */ |
| 81 | + @Nullable |
| 82 | + protected abstract String getStreamUrl(); |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the list of tags to display below the description. |
| 86 | + * @return tag list |
| 87 | + */ |
| 88 | + @Nullable |
| 89 | + public abstract List<String> getTags(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Add additional metadata to display. |
| 93 | + * @param inflater LayoutInflater |
| 94 | + * @param layout detailMetadataLayout |
| 95 | + */ |
| 96 | + protected abstract void setupMetadata(LayoutInflater inflater, LinearLayout layout); |
| 97 | + |
| 98 | + private void setupDescription() { |
| 99 | + final Description description = getDescription(); |
| 100 | + if (description == null || isEmpty(description.getContent()) |
| 101 | + || description == Description.EMPTY_DESCRIPTION) { |
| 102 | + binding.detailDescriptionView.setVisibility(View.GONE); |
| 103 | + binding.detailSelectDescriptionButton.setVisibility(View.GONE); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // start with disabled state. This also loads description content (!) |
| 108 | + disableDescriptionSelection(); |
| 109 | + |
| 110 | + binding.detailSelectDescriptionButton.setOnClickListener(v -> { |
| 111 | + if (binding.detailDescriptionNoteView.getVisibility() == View.VISIBLE) { |
| 112 | + disableDescriptionSelection(); |
| 113 | + } else { |
| 114 | + // enable selection only when button is clicked to prevent flickering |
| 115 | + enableDescriptionSelection(); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + private void enableDescriptionSelection() { |
| 121 | + binding.detailDescriptionNoteView.setVisibility(View.VISIBLE); |
| 122 | + binding.detailDescriptionView.setTextIsSelectable(true); |
| 123 | + |
| 124 | + final String buttonLabel = getString(R.string.description_select_disable); |
| 125 | + binding.detailSelectDescriptionButton.setContentDescription(buttonLabel); |
| 126 | + TooltipCompat.setTooltipText(binding.detailSelectDescriptionButton, buttonLabel); |
| 127 | + binding.detailSelectDescriptionButton.setImageResource(R.drawable.ic_close); |
| 128 | + } |
| 129 | + |
| 130 | + private void disableDescriptionSelection() { |
| 131 | + // show description content again, otherwise some links are not clickable |
| 132 | + final Description description = getDescription(); |
| 133 | + if (description != null) { |
| 134 | + TextLinkifier.fromDescription(binding.detailDescriptionView, |
| 135 | + description, HtmlCompat.FROM_HTML_MODE_LEGACY, |
| 136 | + getService(), getStreamUrl(), |
| 137 | + descriptionDisposables, SET_LINK_MOVEMENT_METHOD); |
| 138 | + } |
| 139 | + |
| 140 | + binding.detailDescriptionNoteView.setVisibility(View.GONE); |
| 141 | + binding.detailDescriptionView.setTextIsSelectable(false); |
| 142 | + |
| 143 | + final String buttonLabel = getString(R.string.description_select_enable); |
| 144 | + binding.detailSelectDescriptionButton.setContentDescription(buttonLabel); |
| 145 | + TooltipCompat.setTooltipText(binding.detailSelectDescriptionButton, buttonLabel); |
| 146 | + binding.detailSelectDescriptionButton.setImageResource(R.drawable.ic_select_all); |
| 147 | + } |
| 148 | + |
| 149 | + protected void addMetadataItem(final LayoutInflater inflater, |
| 150 | + final LinearLayout layout, |
| 151 | + final boolean linkifyContent, |
| 152 | + @StringRes final int type, |
| 153 | + @Nullable final String content) { |
| 154 | + if (isBlank(content)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + final ItemMetadataBinding itemBinding = |
| 159 | + ItemMetadataBinding.inflate(inflater, layout, false); |
| 160 | + |
| 161 | + itemBinding.metadataTypeView.setText(type); |
| 162 | + itemBinding.metadataTypeView.setOnLongClickListener(v -> { |
| 163 | + ShareUtils.copyToClipboard(requireContext(), content); |
| 164 | + return true; |
| 165 | + }); |
| 166 | + |
| 167 | + if (linkifyContent) { |
| 168 | + TextLinkifier.fromPlainText(itemBinding.metadataContentView, content, null, null, |
| 169 | + descriptionDisposables, SET_LINK_MOVEMENT_METHOD); |
| 170 | + } else { |
| 171 | + itemBinding.metadataContentView.setText(content); |
| 172 | + } |
| 173 | + |
| 174 | + itemBinding.metadataContentView.setClickable(true); |
| 175 | + |
| 176 | + layout.addView(itemBinding.getRoot()); |
| 177 | + } |
| 178 | + |
| 179 | + private void addTagsMetadataItem(final LayoutInflater inflater, final LinearLayout layout) { |
| 180 | + final List<String> tags = getTags(); |
| 181 | + |
| 182 | + if (tags != null && !tags.isEmpty()) { |
| 183 | + final var itemBinding = ItemMetadataTagsBinding.inflate(inflater, layout, false); |
| 184 | + |
| 185 | + tags.stream().sorted(String.CASE_INSENSITIVE_ORDER).forEach(tag -> { |
| 186 | + final Chip chip = (Chip) inflater.inflate(R.layout.chip, |
| 187 | + itemBinding.metadataTagsChips, false); |
| 188 | + chip.setText(tag); |
| 189 | + chip.setOnClickListener(this::onTagClick); |
| 190 | + chip.setOnLongClickListener(this::onTagLongClick); |
| 191 | + itemBinding.metadataTagsChips.addView(chip); |
| 192 | + }); |
| 193 | + |
| 194 | + layout.addView(itemBinding.getRoot()); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private void onTagClick(final View chip) { |
| 199 | + if (getParentFragment() != null) { |
| 200 | + NavigationHelper.openSearchFragment(getParentFragment().getParentFragmentManager(), |
| 201 | + getServiceId(), ((Chip) chip).getText().toString()); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private boolean onTagLongClick(final View chip) { |
| 206 | + ShareUtils.copyToClipboard(requireContext(), ((Chip) chip).getText().toString()); |
| 207 | + return true; |
| 208 | + } |
| 209 | +} |
0 commit comments