Skip to content

Commit

Permalink
add saveVolume method
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumewuip committed Apr 4, 2022
1 parent d6137dc commit 7c4f943
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions domain/player/src/entities/Tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type Empty = ReturnType<typeof TracksAPI.of.Empty>;
export type Loaded = ReturnType<typeof TracksAPI.of.Loaded>;

export const volume = TracksAPI.lensFromProp("volume").get;
export const setVolume = TracksAPI.lensFromProp("volume").set;
export const autoplayEnabled = TracksAPI.lensFromProp("autoplayEnabled").get;
export const setAutoplay = TracksAPI.lensFromProp("autoplayEnabled").set;

Expand Down
2 changes: 2 additions & 0 deletions domain/player/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
loadSoundcloud,
} from "./repositories/track";
import { saveAutoplayChoice } from "./repositories/autoplay";
import { updateVolume } from "./repositories/volume";

export {
Track,
Expand All @@ -29,4 +30,5 @@ export {
loadBandcamp,
loadSoundcloud,
saveAutoplayChoice,
updateVolume,
};
6 changes: 4 additions & 2 deletions domain/player/src/repositories/playPause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import * as Tracks from "../entities/Tracks";
import * as Store from "../store";

import * as TrackRepo from "./track";
import { setVolumeForCurrentTrack } from "./volume";

function selectAndPlayTrack(track: Track.Initialized) {
return (state: Tracks.Loaded): IO.IO<void> => {
return pipe(
Store.write(() => pipe(state, Tracks.selectTrack(track), Tracks.playing)),
IO.chain(() => TrackRepo.play(track))
IO.chain(() => TrackRepo.play(track)),
IO.chain(setVolumeForCurrentTrack)
);
};
}
Expand All @@ -25,7 +27,7 @@ export const playOrPause = pipe(
return IO.of(undefined);
}

const selectedTrack = pipe(state, Tracks.selectedTrack);
const selectedTrack = Tracks.selectedTrack(state);

// nothing to do here
if (!Track.isInteractive(selectedTrack)) {
Expand Down
33 changes: 33 additions & 0 deletions domain/player/src/repositories/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ function pauseYoutube(source: Source.Youtube): IO.IO<void> {
return () => (Source.player(source) as any).pauseVideo();
}

function setVolumeYoutube(volume: number) {
return (source: Source.Youtube) => (): IO.IO<void> =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Source.player(source) as any).setVolume(volume * 100);
}

function resetSoundcloud(source: Source.Soundcloud): IO.IO<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return () => (Source.widget(source) as any).seekTo(0);
Expand All @@ -88,6 +94,12 @@ function pauseSoundcloud(source: Source.Soundcloud): IO.IO<void> {
return () => (Source.widget(source) as any).pause();
}

function setVolumeSoundcloud(volume: number) {
return (source: Source.Soundcloud) => (): IO.IO<void> =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Source.widget(source) as any).setVolume(volume * 100);
}

function resetBandcamp(source: Source.Bandcamp): IO.IO<void> {
return () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -105,6 +117,14 @@ function playBandcamp(source: Source.Bandcamp): IO.IO<void> {
return () => (Source.audio(source) as any).play();
}

function setVolumeBandcamp(volume: number) {
return (source: Source.Bandcamp): IO.IO<void> =>
() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Source.audio(source) as any).volue = volume;
};
}

export function reset(track: Track.Initialized): IO.IO<void> {
return pipe(
track,
Expand Down Expand Up @@ -141,6 +161,19 @@ export function pause(track: Track.Initialized): IO.IO<void> {
);
}

export function setVolume(volume: number) {
return (track: Track.Initialized): IO.IO<void> =>
pipe(
track,
Track.source,
Source.fold({
Youtube: setVolumeYoutube(volume),
Soundcloud: setVolumeSoundcloud(volume),
Bandcamp: setVolumeBandcamp(volume),
})
);
}

const aborted = doIfSelectedTrack((track: Track.Initialized) =>
Store.write(
Tracks.modifyIfNotEmpty(
Expand Down
41 changes: 41 additions & 0 deletions domain/player/src/repositories/volume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as IO from "fp-ts/IO";
import { pipe } from "fp-ts/function";

import * as Volume from "../entities/Volume";
import * as Tracks from "../entities/Tracks";
import * as Track from "../entities/Track";

import * as Store from "../store";
import { volume } from "../localStorage";

import * as TrackRepo from "./track";

export function setVolumeForCurrentTrack(): IO.IO<void> {
return () => {
const state = Store.read();

// nothing to do here
if (Tracks.isEmpty(state)) {
return IO.of(undefined);
}

const selectedTrack = Tracks.selectedTrack(state);

// nothing to do here
if (!Track.isInteractive(selectedTrack)) {
return IO.of(undefined);
}

const value = pipe(state, Tracks.volume, Volume.value);

return TrackRepo.setVolume(value)(selectedTrack);
};
}

export function updateVolume(newVolume: Volume.Volume): IO.IO<void> {
return pipe(
volume.silentWrite(Volume.value(newVolume)),
IO.chainFirst(() => Store.write(Tracks.setVolume(newVolume))),
IO.chain(setVolumeForCurrentTrack)
);
}

0 comments on commit 7c4f943

Please sign in to comment.