Skip to content

Commit 085d1e0

Browse files
committed
Actually fix wrong view count
1 parent 4ee1cd5 commit 085d1e0

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import androidx.room.ColumnInfo;
55
import androidx.room.Entity;
66
import androidx.room.ForeignKey;
7-
import androidx.room.Ignore;
87
import androidx.room.Index;
98

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

45-
public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate,
44+
/**
45+
* @param streamUid the stream id this history item will refer to
46+
* @param accessDate the last time the stream was accessed
47+
* @param repeatCount the total number of views this stream received
48+
*/
49+
public StreamHistoryEntity(final long streamUid,
50+
@NonNull final OffsetDateTime accessDate,
4651
final long repeatCount) {
4752
this.streamUid = streamUid;
4853
this.accessDate = accessDate;
4954
this.repeatCount = repeatCount;
5055
}
5156

52-
@Ignore
53-
public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate) {
54-
this(streamUid, accessDate, 0); // start with 0 views (adding views will be done elsewhere)
55-
}
56-
5757
public long getStreamUid() {
5858
return streamUid;
5959
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,11 @@ public Maybe<Long> markAsWatched(final StreamInfoItem info) {
128128

129129
// Add a history entry
130130
final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId);
131-
if (latestEntry != null) {
132-
streamHistoryTable.delete(latestEntry);
133-
latestEntry.setAccessDate(currentTime);
134-
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
135-
return streamHistoryTable.insert(latestEntry);
131+
if (latestEntry == null) {
132+
// never actually viewed: add history entry but with 0 views
133+
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime, 0));
136134
} else {
137-
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime));
135+
return 0L;
138136
}
139137
})).subscribeOn(Schedulers.io());
140138
}
@@ -155,7 +153,8 @@ public Maybe<Long> onViewed(final StreamInfo info) {
155153
latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1);
156154
return streamHistoryTable.insert(latestEntry);
157155
} else {
158-
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime));
156+
// just viewed for the first time: set 1 view
157+
return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime, 1));
159158
}
160159
})).subscribeOn(Schedulers.io());
161160
}

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,22 +2485,31 @@ public void onEvents(@NonNull final com.google.android.exoplayer2.Player player,
24852485
Listener.super.onEvents(player, events);
24862486
MediaItemTag.from(player.getCurrentMediaItem()).ifPresent(tag -> {
24872487
if (tag == currentMetadata) {
2488-
return;
2488+
return; // we still have the same metadata, no need to do anything
24892489
}
2490+
final StreamInfo previousInfo = Optional.ofNullable(currentMetadata)
2491+
.flatMap(MediaItemTag::getMaybeStreamInfo).orElse(null);
24902492
currentMetadata = tag;
2491-
if (!tag.getErrors().isEmpty()) {
2493+
2494+
if (!currentMetadata.getErrors().isEmpty()) {
2495+
// new errors might have been added even if previousInfo == tag.getMaybeStreamInfo()
24922496
final ErrorInfo errorInfo = new ErrorInfo(
2493-
tag.getErrors().get(0),
2497+
currentMetadata.getErrors(),
24942498
UserAction.PLAY_STREAM,
2495-
"Loading failed for [" + tag.getTitle() + "]: " + tag.getStreamUrl(),
2496-
tag.getServiceId());
2499+
"Loading failed for [" + currentMetadata.getTitle()
2500+
+ "]: " + currentMetadata.getStreamUrl(),
2501+
currentMetadata.getServiceId());
24972502
ErrorUtil.createNotification(context, errorInfo);
24982503
}
2499-
tag.getMaybeStreamInfo().ifPresent(info -> {
2504+
2505+
currentMetadata.getMaybeStreamInfo().ifPresent(info -> {
25002506
if (DEBUG) {
25012507
Log.d(TAG, "ExoPlayer - onEvents() update stream info: " + info.getName());
25022508
}
2503-
updateMetadataWith(info);
2509+
if (previousInfo == null || !previousInfo.getUrl().equals(info.getUrl())) {
2510+
// only update with the new stream info if it has actually changed
2511+
updateMetadataWith(info);
2512+
}
25042513
});
25052514
});
25062515
}

0 commit comments

Comments
 (0)