Skip to content

Commit

Permalink
Merge pull request #24 from stefanmedack/feature/delete_playback_posi…
Browse files Browse the repository at this point in the history
…tion_for_finished_videos

adds logic to delete saved playback positions when the video is almos…
  • Loading branch information
stefanmedack authored Apr 14, 2018
2 parents 8a58dce + 674aa14 commit 92113df
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 47 deletions.
35 changes: 0 additions & 35 deletions CHANGELOG.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.reactivex.Single

internal interface Inputs {
fun toggleBookmark()
fun savePlaybackPosition(seconds: Int)
fun savePlaybackPosition(playedSeconds: Int, totalDurationSeconds: Int)
}

internal interface Outputs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ class DetailFragment : DetailsSupportFragment() {

override fun onPause() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || activity?.isInPictureInPictureMode == false) {
playerAdapter?.currentPosition?.let { currentMillis ->
viewModel.inputs.savePlaybackPosition((currentMillis / 1000).toInt())
val currentMillis = playerAdapter?.currentPosition
val durationMillis = playerAdapter?.duration
if(currentMillis != null && durationMillis != null) {
viewModel.inputs.savePlaybackPosition(
playedSeconds = (currentMillis / 1000).toInt(),
totalDurationSeconds = (durationMillis /1000).toInt()
)
}
detailsBackground.playbackGlue?.pause()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DetailViewModel @Inject constructor(
internal val inputs: Inputs = this
internal val outputs: Outputs = this

private val SAVE_PLAYBACK_SECONDS_THRESHOLD = 60

private var eventId: Int = -1

fun init(eventId: Int) {
Expand All @@ -44,8 +42,8 @@ class DetailViewModel @Inject constructor(
bookmarkClickStream.onNext(0)
}

override fun savePlaybackPosition(seconds: Int) {
savePlayPositionStream.onNext(seconds)
override fun savePlaybackPosition(playedSeconds: Int, totalDurationSeconds: Int) {
savePlayPositionStream.onNext(PlaybackData(playedSeconds, totalDurationSeconds))
}

//</editor-fold>
Expand All @@ -70,7 +68,7 @@ class DetailViewModel @Inject constructor(
//</editor-fold>

private val bookmarkClickStream = PublishSubject.create<Int>()
private val savePlayPositionStream = PublishSubject.create<Int>()
private val savePlayPositionStream = PublishSubject.create<PlaybackData>()

private val doToggleBookmark
get() = bookmarkClickStream
Expand All @@ -80,8 +78,8 @@ class DetailViewModel @Inject constructor(
private val doSavePlayedSeconds
get() = savePlayPositionStream
.flatMapCompletable {
if (it > SAVE_PLAYBACK_SECONDS_THRESHOLD) {
repository.savePlayedSeconds(eventId, it)
if (it.hasPlayedMinimumToSave() && !it.hasAlmostFinished()) {
repository.savePlayedSeconds(eventId, it.playedSeconds)
} else {
repository.deletePlayedSeconds(eventId)
}
Expand All @@ -100,4 +98,20 @@ class DetailViewModel @Inject constructor(

private fun updateBookmarkState(isBookmarked: Boolean): Completable = repository.changeBookmarkState(eventId, !isBookmarked)

private data class PlaybackData(
val playedSeconds: Int,
val totalDurationSeconds: Int
) {

private val MINIMUM_PLAYBACK_SECONDS_TO_SAVE = 60
private val MAXIMUM_PLAYBACK_PERCENT_TO_SAVE = .9f

fun hasPlayedMinimumToSave(): Boolean = this.playedSeconds > MINIMUM_PLAYBACK_SECONDS_TO_SAVE

fun hasAlmostFinished(): Boolean = when {
totalDurationSeconds > 0 -> (playedSeconds.toFloat() / totalDurationSeconds.toFloat()) > MAXIMUM_PLAYBACK_PERCENT_TO_SAVE
else -> true
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DetailViewModelTest {
val testEventId = 3
detailViewModel.init(testEventId)

detailViewModel.inputs.savePlaybackPosition(seconds = 180)
detailViewModel.inputs.savePlaybackPosition(playedSeconds = 180, totalDurationSeconds = 2300)

verify(repository).savePlayedSeconds(eventId = testEventId, seconds = 180)
}
Expand All @@ -90,7 +90,17 @@ class DetailViewModelTest {
val testEventId = 3
detailViewModel.init(testEventId)

detailViewModel.inputs.savePlaybackPosition(seconds = 30)
detailViewModel.inputs.savePlaybackPosition(playedSeconds = 30, totalDurationSeconds = 2300)

verify(repository).deletePlayedSeconds(3)
}

@Test
fun `saving playback position when video is almost finished should delete saved playback position`() {
val testEventId = 3
detailViewModel.init(testEventId)

detailViewModel.inputs.savePlaybackPosition(playedSeconds = 2250, totalDurationSeconds = 2300)

verify(repository).deletePlayedSeconds(3)
}
Expand Down

0 comments on commit 92113df

Please sign in to comment.