Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;

import org.schabi.newpipe.database.stream.model.StreamEntity;
Expand Down Expand Up @@ -42,18 +41,19 @@ public class StreamHistoryEntity {
@ColumnInfo(name = STREAM_REPEAT_COUNT)
private long repeatCount;

public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate,
/**
* @param streamUid the stream id this history item will refer to
* @param accessDate the last time the stream was accessed
* @param repeatCount the total number of views this stream received
*/
public StreamHistoryEntity(final long streamUid,
@NonNull final OffsetDateTime accessDate,
final long repeatCount) {
this.streamUid = streamUid;
this.accessDate = accessDate;
this.repeatCount = repeatCount;
}

@Ignore
public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate) {
this(streamUid, accessDate, 0); // start with 0 views (adding views will be done elsewhere)
}

public long getStreamUid() {
return streamUid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ public Maybe<Long> markAsWatched(final StreamInfoItem info) {

// Add a history entry
final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId);
if (latestEntry != null) {
streamHistoryTable.delete(latestEntry);
latestEntry.setAccessDate(currentTime);
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
return streamHistoryTable.insert(latestEntry);
if (latestEntry == null) {
// never actually viewed: add history entry but with 0 views
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime, 0));
} else {
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime));
return 0L;
}
})).subscribeOn(Schedulers.io());
}
Expand All @@ -155,7 +153,8 @@ public Maybe<Long> onViewed(final StreamInfo info) {
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
return streamHistoryTable.insert(latestEntry);
} else {
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime));
// just viewed for the first time: set 1 view
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime, 1));
}
})).subscribeOn(Schedulers.io());
}
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2485,22 +2485,31 @@ public void onEvents(@NonNull final com.google.android.exoplayer2.Player player,
Listener.super.onEvents(player, events);
MediaItemTag.from(player.getCurrentMediaItem()).ifPresent(tag -> {
if (tag == currentMetadata) {
return;
return; // we still have the same metadata, no need to do anything
}
final StreamInfo previousInfo = Optional.ofNullable(currentMetadata)
.flatMap(MediaItemTag::getMaybeStreamInfo).orElse(null);
Comment on lines +2490 to +2491
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use getCurrentStreamInfo() method to do so, unless you want to avoid confusion between the previous info and the current one :)

Suggested change
final StreamInfo previousInfo = Optional.ofNullable(currentMetadata)
.flatMap(MediaItemTag::getMaybeStreamInfo).orElse(null);
final StreamInfo previousInfo = getCurrentStreamInfo().orElse(null);

Reminder of getCurrentStreamInfo() method
    private Optional<StreamInfo> getCurrentStreamInfo() {
        return Optional.ofNullable(currentMetadata).flatMap(MediaItemTag::getMaybeStreamInfo);
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even tough to my eye your suggestion looks better, I'd prefer to keep the current code as it shows where the stream info comes from

currentMetadata = tag;
if (!tag.getErrors().isEmpty()) {

if (!currentMetadata.getErrors().isEmpty()) {
// new errors might have been added even if previousInfo == tag.getMaybeStreamInfo()
final ErrorInfo errorInfo = new ErrorInfo(
tag.getErrors().get(0),
currentMetadata.getErrors(),
UserAction.PLAY_STREAM,
"Loading failed for [" + tag.getTitle() + "]: " + tag.getStreamUrl(),
tag.getServiceId());
"Loading failed for [" + currentMetadata.getTitle()
+ "]: " + currentMetadata.getStreamUrl(),
currentMetadata.getServiceId());
ErrorUtil.createNotification(context, errorInfo);
}
tag.getMaybeStreamInfo().ifPresent(info -> {

currentMetadata.getMaybeStreamInfo().ifPresent(info -> {
if (DEBUG) {
Log.d(TAG, "ExoPlayer - onEvents() update stream info: " + info.getName());
}
updateMetadataWith(info);
if (previousInfo == null || !previousInfo.getUrl().equals(info.getUrl())) {
// only update with the new stream info if it has actually changed
updateMetadataWith(info);
}
});
});
}
Expand Down