Improve meta info layout and merge duplicate code

This commit is contained in:
Stypox 2020-12-20 15:05:37 +01:00
parent a88d3a8a68
commit 15bf6b82e2
7 changed files with 120 additions and 133 deletions

View file

@ -16,7 +16,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.DisplayMetrics;
import android.util.Log;
@ -63,7 +62,6 @@ import org.schabi.newpipe.R;
import org.schabi.newpipe.ReCaptchaActivity;
import org.schabi.newpipe.download.DownloadDialog;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.MetaInfo;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@ -126,11 +124,11 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
import static android.text.TextUtils.isEmpty;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.COMMENTS;
import static org.schabi.newpipe.extractor.stream.StreamExtractor.NO_AGE_LIMIT;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.player.helper.PlayerHelper.globalScreenOrientationLocked;
import static org.schabi.newpipe.player.helper.PlayerHelper.isClearingQueueConfirmationRequired;
import static org.schabi.newpipe.player.playqueue.PlayQueueItem.RECOVERY_UNSET;
import static org.schabi.newpipe.util.AnimationUtils.animateView;
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
public final class VideoDetailFragment
extends BaseStateFragment<StreamInfo>
@ -221,9 +219,8 @@ public final class VideoDetailFragment
private TextView detailDurationView;
private TextView detailPositionView;
private LinearLayout detailMetadataInfo;
private View detailMetadataInfoSeparator;
private TextView detailMetadataInfoText;
private View detailMetaInfoSeparator;
private TextView detailMetaInfoTextView;
private LinearLayout videoDescriptionRootLayout;
private TextView videoUploadDateView;
@ -651,9 +648,8 @@ public final class VideoDetailFragment
detailDurationView = rootView.findViewById(R.id.detail_duration_view);
detailPositionView = rootView.findViewById(R.id.detail_position_view);
detailMetadataInfo = rootView.findViewById(R.id.detail_metadata_info);
detailMetadataInfoSeparator = rootView.findViewById(R.id.detail_metadata_info_separator);
detailMetadataInfoText = rootView.findViewById(R.id.detail_metadata_info_text);
detailMetaInfoSeparator = rootView.findViewById(R.id.detail_meta_info_separator);
detailMetaInfoTextView = rootView.findViewById(R.id.detail_meta_info_text_view);
videoDescriptionRootLayout = rootView.findViewById(R.id.detail_description_root_layout);
videoUploadDateView = rootView.findViewById(R.id.detail_upload_date_view);
@ -1258,42 +1254,6 @@ public final class VideoDetailFragment
}
}
private void setMetaInfo(final StreamInfo info) {
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(
requireContext());
final boolean showMetaInfo = sp.getBoolean(
requireContext().getString(R.string.show_meta_info_key), true);
if (info.getMetaInfo().isEmpty() || !showMetaInfo) {
detailMetadataInfo.setVisibility(View.GONE);
detailMetadataInfoSeparator.setVisibility(View.GONE);
} else {
final List<MetaInfo> metaIfs = info.getMetaInfo();
final StringBuilder stringBuilder = new StringBuilder();
for (final MetaInfo mi: metaIfs) {
if (!isNullOrEmpty(mi.getTitle())) {
stringBuilder.append("<h2>").append(mi.getTitle()).append("</h2>");
}
stringBuilder.append(mi.getContent().getContent());
for (int i = 0; i < mi.getUrls().size(); i++) {
stringBuilder
.append(" <a href=\"").append(mi.getUrls().get(i)).append("\">")
.append(mi.getUrlTexts().get(i))
.append("</a>");
if (i < mi.getUrls().size() - 1 && mi.getUrls().size() > 1) {
// append line break to all but the last URL if there are multiple URLs
stringBuilder.append("<br>");
}
}
}
detailMetadataInfoSeparator.setVisibility(View.VISIBLE);
detailMetadataInfoText.setText(HtmlCompat.fromHtml(
stringBuilder.toString(), HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_HEADING));
detailMetadataInfoText.setMovementMethod(LinkMovementMethod.getInstance());
detailMetadataInfo.setVisibility(View.VISIBLE);
}
}
private final ViewTreeObserver.OnPreDrawListener preDrawListener =
new ViewTreeObserver.OnPreDrawListener() {
@Override
@ -1606,7 +1566,7 @@ public final class VideoDetailFragment
prepareDescription(info.getDescription());
updateProgressInfo(info);
initThumbnailViews(info);
setMetaInfo(info);
showMetaInfoInTextView(info.getMetaInfo(), detailMetaInfoTextView, detailMetaInfoSeparator);
if (player == null || player.isPlayerStopped()) {

View file

@ -9,7 +9,6 @@ import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@ -80,8 +79,8 @@ import io.reactivex.rxjava3.subjects.PublishSubject;
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
import static java.util.Arrays.asList;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.util.AnimationUtils.animateView;
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.InfoItemsPage<?>>
implements BackPressable {
@ -160,6 +159,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
private TextView correctSuggestion;
private TextView metaInfoTextView;
private View metaInfoSeparator;
private View suggestionsPanel;
private boolean suggestionsPanelVisible = false;
@ -276,7 +276,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
handleSearchSuggestion();
handleMetaInfo();
showMetaInfoInTextView(metaInfo == null ? null : Arrays.asList(metaInfo),
metaInfoTextView, metaInfoSeparator);
if (suggestionDisposable == null || suggestionDisposable.isDisposed()) {
initSuggestionObserver();
@ -362,7 +363,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
searchClear = searchToolbarContainer.findViewById(R.id.toolbar_search_clear);
correctSuggestion = rootView.findViewById(R.id.correct_suggestion);
metaInfoTextView = rootView.findViewById(R.id.search_meta_info);
metaInfoTextView = rootView.findViewById(R.id.search_meta_info_text_view);
metaInfoSeparator = rootView.findViewById(R.id.search_meta_info_separator);
}
/*//////////////////////////////////////////////////////////////////////////
@ -988,7 +990,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
metaInfo = result.getMetaInfo().toArray(metaInfo);
handleSearchSuggestion();
handleMetaInfo();
showMetaInfoInTextView(result.getMetaInfo(), metaInfoTextView, metaInfoSeparator);
lastSearchedString = searchString;
nextPage = result.getNextPage();
@ -1036,39 +1039,6 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
}
}
private void handleMetaInfo() {
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(
requireContext());
final boolean showMetaInfo = sp.getBoolean(
requireContext().getString(R.string.show_meta_info_key), true);
if (metaInfo == null || metaInfo.length == 0 || !showMetaInfo) {
metaInfoTextView.setVisibility(View.GONE);
} else {
final StringBuilder stringBuilder = new StringBuilder();
for (final MetaInfo mi: metaInfo) {
if (!isNullOrEmpty(mi.getTitle())) {
stringBuilder.append("<h2>").append(mi.getTitle()).append("</h2>");
}
stringBuilder.append(mi.getContent().getContent());
for (int i = 0; i < mi.getUrls().size(); i++) {
stringBuilder
.append(" <a href=\"").append(mi.getUrls().get(i)).append("\">")
.append(mi.getUrlTexts().get(i))
.append("</a>");
if (i < mi.getUrls().size() - 1 && mi.getUrls().size() > 1) {
// append line break to all but the last URL if there are multiple URLs
stringBuilder.append("<br>");
}
}
}
metaInfoTextView.setText(HtmlCompat.fromHtml(
stringBuilder.toString(), HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_HEADING));
metaInfoTextView.setMovementMethod(LinkMovementMethod.getInstance());
metaInfoTextView.setVisibility(View.VISIBLE);
}
}
@Override
public void handleNextItems(final ListExtractor.InfoItemsPage<?> result) {
showListFooter(false);