Skip to content

Commit

Permalink
Merge pull request #6045 from bg172/showOverallDurationInPlaylist
Browse files Browse the repository at this point in the history
show overall duration of videos in playlist
  • Loading branch information
Stypox authored Mar 29, 2024
2 parents f0beb66 + bef5907 commit 3c0a200
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public class PlaylistFragment extends BaseListInfoFragment<StreamInfoItem, Playl

private MenuItem playlistBookmarkButton;

private long streamCount;
private long playlistOverallDurationSeconds;

public static PlaylistFragment getInstance(final int serviceId, final String url,
final String name) {
final PlaylistFragment instance = new PlaylistFragment();
Expand Down Expand Up @@ -277,6 +280,12 @@ public void showLoading() {
animate(headerBinding.uploaderLayout, false, 200);
}

@Override
public void handleNextItems(final ListExtractor.InfoItemsPage result) {
super.handleNextItems(result);
setStreamCountAndOverallDuration(result.getItems(), !result.hasNextPage());
}

@Override
public void handleResult(@NonNull final PlaylistInfo result) {
super.handleResult(result);
Expand Down Expand Up @@ -322,8 +331,8 @@ public void handleResult(@NonNull final PlaylistInfo result) {
.into(headerBinding.uploaderAvatarView);
}

headerBinding.playlistStreamCount.setText(Localization
.localizeStreamCount(getContext(), result.getStreamCount()));
streamCount = result.getStreamCount();
setStreamCountAndOverallDuration(result.getRelatedItems(), !result.hasNextPage());

final Description description = result.getDescription();
if (description != null && description != Description.EMPTY_DESCRIPTION
Expand Down Expand Up @@ -486,4 +495,20 @@ private void updateBookmarkButtons() {
playlistBookmarkButton.setIcon(drawable);
playlistBookmarkButton.setTitle(titleRes);
}

private void setStreamCountAndOverallDuration(final List<StreamInfoItem> list,
final boolean isDurationComplete) {
if (activity != null && headerBinding != null) {
playlistOverallDurationSeconds += list.stream()
.mapToLong(x -> x.getDuration())
.sum();
headerBinding.playlistStreamCount.setText(
Localization.concatenateStrings(
Localization.localizeStreamCount(activity, streamCount),
Localization.getDurationString(playlistOverallDurationSeconds,
isDurationComplete))
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public void removeWatchedStreams(final boolean removePartiallyWatched) {
}

final long videoCount = itemListAdapter.getItemsList().size();
setVideoCount(videoCount);
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
if (videoCount == 0) {
showEmptyState();
}
Expand Down Expand Up @@ -532,7 +532,7 @@ public void handleResult(@NonNull final List<PlaylistStreamEntry> result) {
itemsList.getLayoutManager().onRestoreInstanceState(itemsListState);
itemsListState = null;
}
setVideoCount(itemListAdapter.getItemsList().size());
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());

PlayButtonHelper.initPlaylistControlClickListener(activity, playlistControlBinding, this);

Expand Down Expand Up @@ -665,7 +665,7 @@ private void removeDuplicatesInPlaylist() {
.subscribe(itemsToKeep -> {
itemListAdapter.clearStreamItemList();
itemListAdapter.addItems(itemsToKeep);
setVideoCount(itemListAdapter.getItemsList().size());
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
saveChanges();

hideLoading();
Expand All @@ -684,7 +684,7 @@ private void deleteItem(final PlaylistStreamEntry item) {
updateThumbnailUrl();
}

setVideoCount(itemListAdapter.getItemsList().size());
setStreamCountAndOverallDuration(itemListAdapter.getItemsList());
saveChanges();
}

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

private void setVideoCount(final long count) {
private void setStreamCountAndOverallDuration(final ArrayList<LocalItem> itemsList) {
if (activity != null && headerBinding != null) {
headerBinding.playlistStreamCount.setText(Localization
.localizeStreamCount(activity, count));
final long streamCount = itemsList.size();
final long playlistOverallDurationSeconds = itemsList.stream()
.filter(PlaylistStreamEntry.class::isInstance)
.map(PlaylistStreamEntry.class::cast)
.map(PlaylistStreamEntry::getStreamEntity)
.mapToLong(StreamEntity::getDuration)
.sum();
headerBinding.playlistStreamCount.setText(
Localization.concatenateStrings(
Localization.localizeStreamCount(activity, streamCount),
Localization.getDurationString(playlistOverallDurationSeconds))
);
}
}

Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,25 @@ public static String likeCount(@NonNull final Context context, final int likeCou
}
}

/**
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds}.
* Prepended zeros are removed.
* @param duration the duration in seconds
* @return a formatted duration String or {@code 0:00} if the duration is zero.
*/
public static String getDurationString(final long duration) {
return getDurationString(duration, true);
}

/**
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds+}.
* Prepended zeros are removed. If the given duration is incomplete, a plus is appended to the
* duration string.
* @param duration the duration in seconds
* @param isDurationComplete whether the given duration is complete or whether info is missing
* @return a formatted duration String or {@code 0:00} if the duration is zero.
*/
public static String getDurationString(final long duration, final boolean isDurationComplete) {
final String output;

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

/**
Expand Down

0 comments on commit 3c0a200

Please sign in to comment.