Handle forward as well

This commit is contained in:
Zander Brown 2018-11-05 21:39:07 +00:00
parent 85387a0a9b
commit 95108048be
No known key found for this signature in database
GPG Key ID: 25EE3C36E31E5F84

View File

@ -553,21 +553,32 @@ impl PlayerExt for PlayerWidget {
// Adapted from https://github.com/philn/glide/blob/b52a65d99daeab0b487f79a0e1ccfad0cd433e22/src/player_context.rs#L219-L245 // Adapted from https://github.com/philn/glide/blob/b52a65d99daeab0b487f79a0e1ccfad0cd433e22/src/player_context.rs#L219-L245
fn seek(&self, offset: ClockTime, direction: SeekDirection) { fn seek(&self, offset: ClockTime, direction: SeekDirection) {
// How far into the podcast we are
let position = self.player.get_position(); let position = self.player.get_position();
if position.is_none() || offset.is_none() { if position.is_none() || offset.is_none() {
return; return;
} }
// How much podcast we have
let duration = self.player.get_duration(); let duration = self.player.get_duration();
let destination = match direction { let destination = match direction {
// If we are more than `offset` into the podcast, jump back that far
SeekDirection::Backwards if position >= offset => Some(position - offset), SeekDirection::Backwards if position >= offset => Some(position - offset),
// If we haven't played `offset` yet just restart the podcast
SeekDirection::Backwards if position < offset => Some(ClockTime::from_seconds(0)), SeekDirection::Backwards if position < offset => Some(ClockTime::from_seconds(0)),
// If we have more than `offset` remaining jump forward they amount
SeekDirection::Forward if !duration.is_none() && position + offset <= duration => { SeekDirection::Forward if !duration.is_none() && position + offset <= duration => {
Some(position + offset) Some(position + offset)
} }
// We don't have `offset` remaining just move to the end (ending playback)
SeekDirection::Forward if !duration.is_none() && position + offset > duration => {
Some(duration)
}
// Who knows what's going on ¯\_(ツ)_/¯
_ => None, _ => None,
}; };
// If we calucated a new position, jump to it
destination.map(|d| self.player.seek(d)); destination.map(|d| self.player.seek(d));
} }