Actually fix wrong view count

This commit is contained in:
Stypox 2022-07-01 15:44:27 +02:00
parent 4ee1cd5826
commit 085d1e0d38
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
3 changed files with 29 additions and 21 deletions

View file

@ -4,7 +4,6 @@ import androidx.annotation.NonNull;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index; import androidx.room.Index;
import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.database.stream.model.StreamEntity;
@ -42,18 +41,19 @@ public class StreamHistoryEntity {
@ColumnInfo(name = STREAM_REPEAT_COUNT) @ColumnInfo(name = STREAM_REPEAT_COUNT)
private long repeatCount; 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) { final long repeatCount) {
this.streamUid = streamUid; this.streamUid = streamUid;
this.accessDate = accessDate; this.accessDate = accessDate;
this.repeatCount = repeatCount; 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() { public long getStreamUid() {
return streamUid; return streamUid;
} }

View file

@ -128,13 +128,11 @@ public class HistoryRecordManager {
// Add a history entry // Add a history entry
final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId); final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId);
if (latestEntry != null) { if (latestEntry == null) {
streamHistoryTable.delete(latestEntry); // never actually viewed: add history entry but with 0 views
latestEntry.setAccessDate(currentTime); return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime, 0));
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
return streamHistoryTable.insert(latestEntry);
} else { } else {
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime)); return 0L;
} }
})).subscribeOn(Schedulers.io()); })).subscribeOn(Schedulers.io());
} }
@ -155,7 +153,8 @@ public class HistoryRecordManager {
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1); latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
return streamHistoryTable.insert(latestEntry); return streamHistoryTable.insert(latestEntry);
} else { } 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()); })).subscribeOn(Schedulers.io());
} }

View file

@ -2485,22 +2485,31 @@ public final class Player implements
Listener.super.onEvents(player, events); Listener.super.onEvents(player, events);
MediaItemTag.from(player.getCurrentMediaItem()).ifPresent(tag -> { MediaItemTag.from(player.getCurrentMediaItem()).ifPresent(tag -> {
if (tag == currentMetadata) { 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);
currentMetadata = tag; 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( final ErrorInfo errorInfo = new ErrorInfo(
tag.getErrors().get(0), currentMetadata.getErrors(),
UserAction.PLAY_STREAM, UserAction.PLAY_STREAM,
"Loading failed for [" + tag.getTitle() + "]: " + tag.getStreamUrl(), "Loading failed for [" + currentMetadata.getTitle()
tag.getServiceId()); + "]: " + currentMetadata.getStreamUrl(),
currentMetadata.getServiceId());
ErrorUtil.createNotification(context, errorInfo); ErrorUtil.createNotification(context, errorInfo);
} }
tag.getMaybeStreamInfo().ifPresent(info -> {
currentMetadata.getMaybeStreamInfo().ifPresent(info -> {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "ExoPlayer - onEvents() update stream info: " + info.getName()); 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);
}
}); });
}); });
} }