Skip to content

Commit bef5907

Browse files
bg1722Stypox
authored andcommitted
show OverallDuration in Playlist
earlier only overall amount of videos was shown. Now overall duration is shown there too - as formatted by existing Localization.concatenateStrings() and Localization.getDurationString(). show all videos OverallDuration in local Playlist too refactor to make implementation in LocalPlaylistFragment and PlaylistFragment more obviously similar unfortunately could not refactor upto BaseLocalListFragment revert the changes for online Playlists because they are paginated and may be infinite i.e. correct count may come only from the service->extractor chain which unfortunately does not give overall duration yet next try to improve user-experience with online Playlist just show that duration is longer (">") than the calculated value in case there is more page(s) even more improve user-experience for online Playlist by adding the duration of next items as soon as they are made visible make showing of playlists duration configurable, disabled by default adjusted duration to be handled as long because it comes as long from extractor no idea why I handled it as int earlier Revert "make showing of playlists duration configurable, disabled by default", refactor This reverts commit bc1ba17. Fix rebase Apply review Rename video -> stream Remove unused settings keys
1 parent f0beb66 commit bef5907

3 files changed

Lines changed: 64 additions & 10 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public class PlaylistFragment extends BaseListInfoFragment<StreamInfoItem, Playl
8989

9090
private MenuItem playlistBookmarkButton;
9191

92+
private long streamCount;
93+
private long playlistOverallDurationSeconds;
94+
9295
public static PlaylistFragment getInstance(final int serviceId, final String url,
9396
final String name) {
9497
final PlaylistFragment instance = new PlaylistFragment();
@@ -277,6 +280,12 @@ public void showLoading() {
277280
animate(headerBinding.uploaderLayout, false, 200);
278281
}
279282

283+
@Override
284+
public void handleNextItems(final ListExtractor.InfoItemsPage result) {
285+
super.handleNextItems(result);
286+
setStreamCountAndOverallDuration(result.getItems(), !result.hasNextPage());
287+
}
288+
280289
@Override
281290
public void handleResult(@NonNull final PlaylistInfo result) {
282291
super.handleResult(result);
@@ -322,8 +331,8 @@ public void handleResult(@NonNull final PlaylistInfo result) {
322331
.into(headerBinding.uploaderAvatarView);
323332
}
324333

325-
headerBinding.playlistStreamCount.setText(Localization
326-
.localizeStreamCount(getContext(), result.getStreamCount()));
334+
streamCount = result.getStreamCount();
335+
setStreamCountAndOverallDuration(result.getRelatedItems(), !result.hasNextPage());
327336

328337
final Description description = result.getDescription();
329338
if (description != null && description != Description.EMPTY_DESCRIPTION
@@ -486,4 +495,20 @@ private void updateBookmarkButtons() {
486495
playlistBookmarkButton.setIcon(drawable);
487496
playlistBookmarkButton.setTitle(titleRes);
488497
}
498+
499+
private void setStreamCountAndOverallDuration(final List<StreamInfoItem> list,
500+
final boolean isDurationComplete) {
501+
if (activity != null && headerBinding != null) {
502+
playlistOverallDurationSeconds += list.stream()
503+
.mapToLong(x -> x.getDuration())
504+
.sum();
505+
headerBinding.playlistStreamCount.setText(
506+
Localization.concatenateStrings(
507+
Localization.localizeStreamCount(activity, streamCount),
508+
Localization.getDurationString(playlistOverallDurationSeconds,
509+
isDurationComplete))
510+
);
511+
}
512+
}
513+
489514
}

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public void removeWatchedStreams(final boolean removePartiallyWatched) {
502502
}
503503

504504
final long videoCount = itemListAdapter.getItemsList().size();
505-
setVideoCount(videoCount);
505+
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
506506
if (videoCount == 0) {
507507
showEmptyState();
508508
}
@@ -532,7 +532,7 @@ public void handleResult(@NonNull final List<PlaylistStreamEntry> result) {
532532
itemsList.getLayoutManager().onRestoreInstanceState(itemsListState);
533533
itemsListState = null;
534534
}
535-
setVideoCount(itemListAdapter.getItemsList().size());
535+
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
536536

537537
PlayButtonHelper.initPlaylistControlClickListener(activity, playlistControlBinding, this);
538538

@@ -665,7 +665,7 @@ private void removeDuplicatesInPlaylist() {
665665
.subscribe(itemsToKeep -> {
666666
itemListAdapter.clearStreamItemList();
667667
itemListAdapter.addItems(itemsToKeep);
668-
setVideoCount(itemListAdapter.getItemsList().size());
668+
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
669669
saveChanges();
670670

671671
hideLoading();
@@ -684,7 +684,7 @@ private void deleteItem(final PlaylistStreamEntry item) {
684684
updateThumbnailUrl();
685685
}
686686

687-
setVideoCount(itemListAdapter.getItemsList().size());
687+
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
688688
saveChanges();
689689
}
690690

@@ -855,10 +855,20 @@ private void setInitialData(final long pid, final String title) {
855855
this.name = !TextUtils.isEmpty(title) ? title : "";
856856
}
857857

858-
private void setVideoCount(final long count) {
858+
private void setStreamCountAndOverallDuration(final ArrayList<LocalItem> itemsList) {
859859
if (activity != null && headerBinding != null) {
860-
headerBinding.playlistStreamCount.setText(Localization
861-
.localizeStreamCount(activity, count));
860+
final long streamCount = itemsList.size();
861+
final long playlistOverallDurationSeconds = itemsList.stream()
862+
.filter(PlaylistStreamEntry.class::isInstance)
863+
.map(PlaylistStreamEntry.class::cast)
864+
.map(PlaylistStreamEntry::getStreamEntity)
865+
.mapToLong(StreamEntity::getDuration)
866+
.sum();
867+
headerBinding.playlistStreamCount.setText(
868+
Localization.concatenateStrings(
869+
Localization.localizeStreamCount(activity, streamCount),
870+
Localization.getDurationString(playlistOverallDurationSeconds))
871+
);
862872
}
863873
}
864874

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,25 @@ public static String likeCount(@NonNull final Context context, final int likeCou
238238
}
239239
}
240240

241+
/**
242+
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds}.
243+
* Prepended zeros are removed.
244+
* @param duration the duration in seconds
245+
* @return a formatted duration String or {@code 0:00} if the duration is zero.
246+
*/
241247
public static String getDurationString(final long duration) {
248+
return getDurationString(duration, true);
249+
}
250+
251+
/**
252+
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds+}.
253+
* Prepended zeros are removed. If the given duration is incomplete, a plus is appended to the
254+
* duration string.
255+
* @param duration the duration in seconds
256+
* @param isDurationComplete whether the given duration is complete or whether info is missing
257+
* @return a formatted duration String or {@code 0:00} if the duration is zero.
258+
*/
259+
public static String getDurationString(final long duration, final boolean isDurationComplete) {
242260
final String output;
243261

244262
final long days = duration / (24 * 60 * 60L); /* greater than a day */
@@ -256,7 +274,8 @@ public static String getDurationString(final long duration) {
256274
} else {
257275
output = String.format(Locale.US, "%d:%02d", minutes, seconds);
258276
}
259-
return output;
277+
final String durationPostfix = isDurationComplete ? "" : "+";
278+
return output + durationPostfix;
260279
}
261280

262281
/**

0 commit comments

Comments
 (0)