Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PlayerRepository and fix MediaItemPosition #175

Merged
merged 1 commit into from
May 17, 2022
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
1 change: 1 addition & 0 deletions media/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ package com.google.android.horologist.media.repository {
method public boolean hasPreviousMediaItem();
method public void pause();
method public void play();
method public void play(int mediaItemIndex);
method public void prepare();
method public void release();
method public void removeMediaItem(int index);
Expand Down
5 changes: 3 additions & 2 deletions media/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ plugins {
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {

implementation libs.kotlinx.coroutines.core

testImplementation libs.junit
testImplementation libs.truth
}

apply plugin: "com.vanniktech.maven.publish"
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public sealed class MediaItemPosition(
current: Duration,
duration: Duration
): KnownDuration {
check(current.isNegative()) {
check(!current.isNegative()) {
yschimke marked this conversation as resolved.
Show resolved Hide resolved
"Current position can't be a negative value [current: $current] [duration: $duration]."
}
check(!duration.isPositive()) {
check(duration.isPositive()) {
"Duration has to be greater than zero [current: $current] [duration: $duration]."
}
check(current <= duration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.android.horologist.media.model.MediaItem
import com.google.android.horologist.media.model.MediaItemPosition
import com.google.android.horologist.media.model.PlayerState
import kotlinx.coroutines.flow.StateFlow
import kotlin.time.Duration

@ExperimentalHorologistMediaApi
public interface PlayerRepository {
Expand Down Expand Up @@ -61,6 +62,11 @@ public interface PlayerRepository {
*/
public fun play()

/**
* Play [media item][MediaItem] at given index as soon as player is ready.
*/
public fun play(mediaItemIndex: Int)

/**
* Pauses playback.
*/
Expand Down Expand Up @@ -88,10 +94,8 @@ public interface PlayerRepository {

/**
* Returns the [seekBack] increment.
*
* @return The seek back increment, in milliseconds.
*/
public fun getSeekBackIncrement(): Long
public fun getSeekBackIncrement(): Duration

/**
* Seeks back in the [current media item][currentMediaItem] by [seek back increment][getSeekBackIncrement].
Expand All @@ -100,10 +104,8 @@ public interface PlayerRepository {

/**
* Returns the [seekForward] increment.
*
* @return The seek forward increment, in milliseconds.
*/
public fun getSeekForwardIncrement(): Long
public fun getSeekForwardIncrement(): Duration

/**
* Seek forward in the [current media item][currentMediaItem] by [seek forward increment][getSeekForwardIncrement].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@

package com.google.android.horologist.media.model

import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertThrows
import org.junit.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

class MediaItemPositionTest {

@Test
fun givenValidValues_whenCreateKnownPosition_thenCreateCorrectly() {
// given
val current = 1.seconds
val duration = 2.seconds

// when
val result = MediaItemPosition.create(current = current, duration = duration)

// then
assertThat(result.current).isEqualTo(current)
assertThat(result.duration).isEqualTo(duration)
assertThat(result.percent).isEqualTo(0.5f)
}

@Test
fun givenCurrentPositionIsNegative_whenCreateKnownPosition_thenExceptionIsThrown() {
// given
Expand Down