Skip to content

Commit

Permalink
Consider stream state valid also if >1/4 of video was watched
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Jun 7, 2021
1 parent afd6c03 commit 06be78b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ public void setProgressMillis(final long progressMillis) {

/**
* The state will be considered valid, and thus be saved, if the progress is more than {@link
* #PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}.
* #PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS} or at least 1/4 of the video length.
* @param durationInSeconds the duration of the stream connected with this state, in seconds
* @return whether this stream state entity should be saved or not
*/
public boolean isValid() {
return progressMillis > PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS;
public boolean isValid(final long durationInSeconds) {
return progressMillis > PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS
|| progressMillis > durationInSeconds * 1000 / 4;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ public Maybe<StreamHistoryEntity> getStreamHistory(final StreamInfo info) {

public Maybe<StreamStateEntity> loadStreamState(final PlayQueueItem queueItem) {
return queueItem.getStream()
.map((info) -> streamTable.upsert(new StreamEntity(info)))
.map(info -> streamTable.upsert(new StreamEntity(info)))
.flatMapPublisher(streamStateTable::getState)
.firstElement()
.flatMap(list -> list.isEmpty() ? Maybe.empty() : Maybe.just(list.get(0)))
.filter(StreamStateEntity::isValid)
.filter(state -> state.isValid(queueItem.getDuration()))
.subscribeOn(Schedulers.io());
}

Expand All @@ -224,15 +224,15 @@ public Maybe<StreamStateEntity> loadStreamState(final StreamInfo info) {
.flatMapPublisher(streamStateTable::getState)
.firstElement()
.flatMap(list -> list.isEmpty() ? Maybe.empty() : Maybe.just(list.get(0)))
.filter(StreamStateEntity::isValid)
.filter(state -> state.isValid(info.getDuration()))
.subscribeOn(Schedulers.io());
}

public Completable saveStreamState(@NonNull final StreamInfo info, final long progressMillis) {
return Completable.fromAction(() -> database.runInTransaction(() -> {
final long streamId = streamTable.upsert(new StreamEntity(info));
final StreamStateEntity state = new StreamStateEntity(streamId, progressMillis);
if (state.isValid()) {
if (state.isValid(info.getDuration())) {
streamStateTable.upsert(state);
}
})).subscribeOn(Schedulers.io());
Expand Down

0 comments on commit 06be78b

Please sign in to comment.