Skip to content

Commit

Permalink
Add button to allow selecting text in the description
Browse files Browse the repository at this point in the history
Since now selection is disabled by default, this fixes #5453
  • Loading branch information
Stypox committed Apr 1, 2021
1 parent 0cbd1a3 commit b440afe
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.TooltipCompat;
import androidx.core.text.HtmlCompat;

import org.schabi.newpipe.BaseFragment;
Expand Down Expand Up @@ -51,7 +52,7 @@ public View onCreateView(@NonNull final LayoutInflater inflater,
FragmentDescriptionBinding.inflate(inflater, container, false);
if (streamInfo != null) {
setupUploadDate(binding.detailUploadDateView);
setupDescription(binding.detailDescriptionView);
setupDescription(binding);
setupMetadata(inflater, binding.detailMetadataLayout);
}
return binding.getRoot();
Expand All @@ -74,27 +75,44 @@ private void setupUploadDate(final TextView uploadDateTextView) {
}
}

private void setupDescription(final TextView descriptionTextView) {
private void setupDescription(final FragmentDescriptionBinding binding) {
final Description description = streamInfo.getDescription();
if (description == null || isEmpty(description.getContent())
|| description == Description.emptyDescription) {
descriptionTextView.setVisibility(View.GONE);
binding.detailDescriptionView.setVisibility(View.GONE);
binding.detailSelectDescriptionButton.setVisibility(View.GONE);
return;
}

// set tooltip programmatically so that it works on older android versions
TooltipCompat.setTooltipText(binding.detailSelectDescriptionButton,
getString(R.string.description_select_note));

binding.detailSelectDescriptionButton.setOnClickListener(v -> {
// allow clicking button only once
binding.detailSelectDescriptionButton.setOnClickListener(null);
binding.detailSelectDescriptionButton.setClickable(false);
binding.detailSelectDescriptionButton.setFocusable(false);
binding.detailSelectDescriptionButton.setAlpha(0.3f);

// enable selection only when button is clicked to prevent flickering
binding.detailDescriptionNoteView.setVisibility(View.VISIBLE);
binding.detailDescriptionView.setTextIsSelectable(true);
});

switch (description.getType()) {
case Description.HTML:
descriptionDisposable = TextLinkifier.createLinksFromHtmlBlock(requireContext(),
description.getContent(), descriptionTextView,
description.getContent(), binding.detailDescriptionView,
HtmlCompat.FROM_HTML_MODE_LEGACY);
break;
case Description.MARKDOWN:
descriptionDisposable = TextLinkifier.createLinksFromMarkdownText(requireContext(),
description.getContent(), descriptionTextView);
description.getContent(), binding.detailDescriptionView);
break;
case Description.PLAIN_TEXT: default:
descriptionDisposable = TextLinkifier.createLinksFromPlainText(requireContext(),
description.getContent(), descriptionTextView);
description.getContent(), binding.detailDescriptionView);
break;
}
}
Expand Down Expand Up @@ -141,11 +159,11 @@ private void addMetadataItem(final LayoutInflater inflater,
final ItemMetadataBinding binding = ItemMetadataBinding.inflate(inflater, layout, false);
binding.metadataTypeView.setText(type);
binding.metadataTypeView.setOnClickListener(
v -> ShareUtils.copyToClipboard(layout.getContext(), content));
v -> ShareUtils.copyToClipboard(requireContext(), content));

if (linkifyContent) {
TextLinkifier.createLinksFromPlainText(layout.getContext(), content,
binding.metadataContentView);
TextLinkifier.createLinksFromPlainText(requireContext(),
content, binding.metadataContentView);
} else {
binding.metadataContentView.setText(content);
}
Expand Down Expand Up @@ -190,7 +208,7 @@ private void addPrivacyMetadataItem(final LayoutInflater inflater, final LinearL

if (contentRes != 0) {
addMetadataItem(inflater, layout, false,
R.string.metadata_privacy, layout.getContext().getString(contentRes));
R.string.metadata_privacy, requireContext().getString(contentRes));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_select_all_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#000000">
<path
android:fillColor="#000000"
android:pathData="M3,5h2L5,3c-1.1,0 -2,0.9 -2,2zM3,13h2v-2L3,11v2zM7,21h2v-2L7,19v2zM3,9h2L5,7L3,7v2zM13,3h-2v2h2L13,3zM19,3v2h2c0,-1.1 -0.9,-2 -2,-2zM5,21v-2L3,19c0,1.1 0.9,2 2,2zM3,17h2v-2L3,15v2zM9,3L7,3v2h2L9,3zM11,21h2v-2h-2v2zM19,13h2v-2h-2v2zM19,21c1.1,0 2,-0.9 2,-2h-2v2zM19,9h2L21,7h-2v2zM19,17h2v-2h-2v2zM15,21h2v-2h-2v2zM15,5h2L17,3h-2v2zM7,17h10L17,7L7,7v10zM9,9h6v6L9,15L9,9z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_select_all_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#ffffff">
<path
android:fillColor="#ffffff"
android:pathData="M3,5h2L5,3c-1.1,0 -2,0.9 -2,2zM3,13h2v-2L3,11v2zM7,21h2v-2L7,19v2zM3,9h2L5,7L3,7v2zM13,3h-2v2h2L13,3zM19,3v2h2c0,-1.1 -0.9,-2 -2,-2zM5,21v-2L3,19c0,1.1 0.9,2 2,2zM3,17h2v-2L3,15v2zM9,3L7,3v2h2L9,3zM11,21h2v-2h-2v2zM19,13h2v-2h-2v2zM19,21c1.1,0 2,-0.9 2,-2h-2v2zM19,9h2L21,7h-2v2zM19,17h2v-2h-2v2zM15,21h2v-2h-2v2zM15,5h2L17,3h-2v2zM7,17h10L17,7L7,7v10zM9,9h6v6L9,15L9,9z"/>
</vector>
56 changes: 48 additions & 8 deletions app/src/main/res/layout/fragment_description.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,78 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:animateLayoutChanges="true">

<TextView
android:id="@+id/detail_upload_date_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="16dp"
android:layout_marginEnd="8dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/video_item_detail_upload_date_text_size"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toStartOf="@+id/detail_select_description_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Published on Oct 2, 2009" />

<ImageView
android:id="@+id/detail_select_description_button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="2dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/description_select_note"
android:focusable="true"
android:padding="5dp"
app:layout_constraintBottom_toTopOf="@+id/barrier"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/ic_select_all" />

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierAllowsGoneWidgets="false"
app:barrierDirection="top"
app:constraint_referenced_ids="detail_description_note_view,detail_description_view" />

<TextView
android:id="@+id/detail_description_note_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:text="@string/description_select_note"
android:textSize="12sp"
android:textStyle="italic"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detail_upload_date_view"
tools:visibility="visible" />

<TextView
android:id="@+id/detail_description_view"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="true"
android:textSize="@dimen/video_item_detail_description_text_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detail_upload_date_view"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintTop_toBottomOf="@+id/detail_description_note_view"
tools:text="Description Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a ultricies ex. Integer sit amet sodales risus. Duis non mi et urna pretium bibendum." />

<LinearLayout
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<attr name="ic_help" format="reference" />
<attr name="ic_arrow_back" format="reference" />
<attr name="ic_live_tv" format="reference" />
<attr name="ic_select_all" format="reference" />

<attr name="progress_horizontal_drawable" format="reference" />
<!-- Can't refer to colors directly in drawable's xml-->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@
<string name="night_theme_summary">Select your favorite night theme — %s</string>
<string name="select_night_theme_toast">You can select your favorite night theme below</string>
<string name="download_has_started">Download has started</string>
<string name="description_select_note">Select text inside the description. Note that the page may flicker.</string>
<string name="metadata_category">Category</string>
<string name="metadata_tags">Tags</string>
<string name="metadata_licence">Licence</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<item name="ic_sort">@drawable/ic_sort_black_24dp</item>
<item name="ic_help">@drawable/ic_help_black_24dp</item>
<item name="ic_arrow_back">@drawable/ic_arrow_back_black_24dp</item>
<item name="ic_select_all">@drawable/ic_select_all_black_24dp</item>

<item name="separator_color">@color/light_separator_color</item>
<item name="contrast_background_color">@color/light_contrast_background_color</item>
Expand Down Expand Up @@ -209,6 +210,7 @@
<item name="ic_sort">@drawable/ic_sort_white_24dp</item>
<item name="ic_help">@drawable/ic_help_white_24dp</item>
<item name="ic_arrow_back">@drawable/ic_arrow_back_white_24dp</item>
<item name="ic_select_all">@drawable/ic_select_all_white_24dp</item>

<item name="separator_color">@color/dark_separator_color</item>
<item name="contrast_background_color">@color/dark_contrast_background_color</item>
Expand Down

0 comments on commit b440afe

Please sign in to comment.