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

Skipping 15 sec progress #237

Merged
merged 3 commits into from
Jan 17, 2023
Merged
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
46 changes: 31 additions & 15 deletions src/renderer/components/Lofi/Cover/Controls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import * as settings from 'electron-settings';
import './style.scss';
import { SpotifyApiInstance } from '../../../../../api/spotify-api';

const moveSongProgress = async (isForward: boolean, distInSec: number) => {
const distinMs = distInSec * 1000;
const currentlyPlaying = await SpotifyApiInstance.fetch(`/me/player`, {
method: 'GET',
});
const currentProgress = Number(currentlyPlaying.progress_ms);
const newProgress = isForward ? currentProgress + distinMs : currentProgress - distinMs;
await SpotifyApiInstance.fetch(`/me/player/seek?position_ms=${newProgress}`, {
method: 'PUT',
});
};

class Controls extends React.Component<any, any> {
private accountType: string;
constructor(props: any) {
Expand Down Expand Up @@ -39,31 +51,35 @@ class Controls extends React.Component<any, any> {
this.props.parent.togglePlayPause();
}

async forward() {
async forward({ ctrlKey }: React.MouseEvent) {
if (this.props.parent.state.spotifyError) {
return;
}

SpotifyApiInstance.fetch('/me/player/next', {
method: 'POST',
}).then(() => {
// Spotify API doesn't update fast enough sometimes, so give it some leeway
setTimeout(this.props.parent.listeningTo.bind(this), 2000);
});
if (ctrlKey) {
moveSongProgress(true, 15);
} else {
await SpotifyApiInstance.fetch('/me/player/next', {
method: 'POST',
});
}

setTimeout(this.props.parent.listeningTo.bind(this), 2000);
this.props.parent.setPlaying(true);
}

async backward() {
async backward({ ctrlKey }: React.MouseEvent) {
if (this.props.parent.state.spotifyError) {
return;
}

SpotifyApiInstance.fetch('/me/player/previous', {
method: 'POST',
}).then(() => {
// Spotify API doesn't update fast enough sometimes, so give it some leeway
setTimeout(this.props.parent.listeningTo.bind(this), 2000);
});
if (ctrlKey) {
moveSongProgress(false, 15);
} else {
await SpotifyApiInstance.fetch('/me/player/previous', {
method: 'POST',
});
}
setTimeout(this.props.parent.listeningTo.bind(this), 2000);
this.props.parent.setPlaying(true);
}

Expand Down