Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add voice broadcast seek 30s forward/backward buttons (#9592)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Nov 21, 2022
1 parent caac059 commit d699f56
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 11 deletions.
6 changes: 6 additions & 0 deletions res/css/compound/_Icon.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ limitations under the License.
flex: 0 0 16px;
width: 16px;
}

.mx_Icon_24 {
height: 24px;
flex: 0 0 24px;
width: 24px;
}
4 changes: 4 additions & 0 deletions res/css/views/elements/_AccessibleButton.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ limitations under the License.
color: $accent;
}

&.mx_AccessibleButton_kind_secondary_content {
color: $secondary-content;
}

&.mx_AccessibleButton_kind_danger {
color: $button-danger-fg-color;
background-color: $alert;
Expand Down
1 change: 0 additions & 1 deletion res/css/voice-broadcast/atoms/_VoiceBroadcastControl.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ limitations under the License.
display: flex;
height: 32px;
justify-content: center;
margin-bottom: $spacing-8;
width: 32px;
}

Expand Down
5 changes: 4 additions & 1 deletion res/css/voice-broadcast/molecules/_VoiceBroadcastBody.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ limitations under the License.
}

.mx_VoiceBroadcastBody_controls {
align-items: center;
display: flex;
justify-content: space-around;
gap: $spacing-32;
justify-content: center;
margin-bottom: $spacing-8;
}

.mx_VoiceBroadcastBody_timerow {
Expand Down
1 change: 1 addition & 0 deletions res/img/element-icons/Back30s.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/img/element-icons/Forward30s.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/components/views/elements/AccessibleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type AccessibleButtonKind = | 'primary'
| 'primary_outline'
| 'primary_sm'
| 'secondary'
| 'secondary_content'
| 'content_inline'
| 'danger'
| 'danger_outline'
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@
"play voice broadcast": "play voice broadcast",
"resume voice broadcast": "resume voice broadcast",
"pause voice broadcast": "pause voice broadcast",
"30s backward": "30s backward",
"30s forward": "30s forward",
"Go live": "Go live",
"Live": "Live",
"Voice broadcast": "Voice broadcast",
Expand Down
39 changes: 39 additions & 0 deletions src/voice-broadcast/components/atoms/SeekButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";

import AccessibleButton from "../../../components/views/elements/AccessibleButton";

interface Props {
icon: React.FC<React.SVGProps<SVGSVGElement>>;
label: string;
onClick: () => void;
}

export const SeekButton: React.FC<Props> = ({
onClick,
icon: Icon,
label,
}) => {
return <AccessibleButton
kind="secondary_content"
onClick={onClick}
aria-label={label}
>
<Icon className="mx_Icon mx_Icon_24" />
</AccessibleButton>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";
import React, { ReactElement } from "react";

import {
VoiceBroadcastControl,
Expand All @@ -26,9 +26,14 @@ import Spinner from "../../../components/views/elements/Spinner";
import { useVoiceBroadcastPlayback } from "../../hooks/useVoiceBroadcastPlayback";
import { Icon as PlayIcon } from "../../../../res/img/element-icons/play.svg";
import { Icon as PauseIcon } from "../../../../res/img/element-icons/pause.svg";
import { Icon as Back30sIcon } from "../../../../res/img/element-icons/Back30s.svg";
import { Icon as Forward30sIcon } from "../../../../res/img/element-icons/Forward30s.svg";
import { _t } from "../../../languageHandler";
import Clock from "../../../components/views/audio_messages/Clock";
import SeekBar from "../../../components/views/audio_messages/SeekBar";
import { SeekButton } from "../atoms/SeekButton";

const SEEK_TIME = 30;

interface VoiceBroadcastPlaybackBodyProps {
playback: VoiceBroadcastPlayback;
Expand All @@ -40,10 +45,11 @@ export const VoiceBroadcastPlaybackBody: React.FC<VoiceBroadcastPlaybackBodyProp
const {
duration,
liveness,
playbackState,
position,
room,
sender,
toggle,
playbackState,
} = useVoiceBroadcastPlayback(playback);

let control: React.ReactNode;
Expand Down Expand Up @@ -76,6 +82,31 @@ export const VoiceBroadcastPlaybackBody: React.FC<VoiceBroadcastPlaybackBodyProp
/>;
}

let seekBackwardButton: ReactElement | null = null;
let seekForwardButton: ReactElement | null = null;

if (playbackState !== VoiceBroadcastPlaybackState.Stopped) {
const onSeekBackwardButtonClick = () => {
playback.skipTo(Math.max(0, position - SEEK_TIME));
};

seekBackwardButton = <SeekButton
icon={Back30sIcon}
label={_t("30s backward")}
onClick={onSeekBackwardButtonClick}
/>;

const onSeekForwardButtonClick = () => {
playback.skipTo(Math.min(duration, position + SEEK_TIME));
};

seekForwardButton = <SeekButton
icon={Forward30sIcon}
label={_t("30s forward")}
onClick={onSeekForwardButtonClick}
/>;
}

return (
<div className="mx_VoiceBroadcastBody">
<VoiceBroadcastHeader
Expand All @@ -85,7 +116,9 @@ export const VoiceBroadcastPlaybackBody: React.FC<VoiceBroadcastPlaybackBodyProp
showBroadcast={true}
/>
<div className="mx_VoiceBroadcastBody_controls">
{ seekBackwardButton }
{ control }
{ seekForwardButton }
</div>
<div className="mx_VoiceBroadcastBody_timerow">
<SeekBar playback={playback} />
Expand Down
10 changes: 9 additions & 1 deletion src/voice-broadcast/hooks/useVoiceBroadcastPlayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export const useVoiceBroadcastPlayback = (playback: VoiceBroadcastPlayback) => {
d => setDuration(d / 1000),
);

const [position, setPosition] = useState(playback.timeSeconds);
useTypedEventEmitter(
playback,
VoiceBroadcastPlaybackEvent.PositionChanged,
p => setPosition(p / 1000),
);

const [liveness, setLiveness] = useState(playback.getLiveness());
useTypedEventEmitter(
playback,
Expand All @@ -57,9 +64,10 @@ export const useVoiceBroadcastPlayback = (playback: VoiceBroadcastPlayback) => {
return {
duration,
liveness: liveness,
playbackState,
position,
room: room,
sender: playback.infoEvent.sender,
toggle: playbackToggle,
playbackState,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import React from "react";
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { act, render, RenderResult } from "@testing-library/react";
import { act, render, RenderResult, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock";

Expand Down Expand Up @@ -54,7 +54,7 @@ describe("VoiceBroadcastPlaybackBody", () => {

infoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
VoiceBroadcastInfoState.Stopped,
userId,
client.getDeviceId(),
);
Expand All @@ -65,6 +65,7 @@ describe("VoiceBroadcastPlaybackBody", () => {
jest.spyOn(playback, "toggle").mockImplementation(() => Promise.resolve());
jest.spyOn(playback, "getLiveness");
jest.spyOn(playback, "getState");
jest.spyOn(playback, "skipTo");
jest.spyOn(playback, "durationSeconds", "get").mockReturnValue(23 * 60 + 42); // 23:42
});

Expand All @@ -80,13 +81,61 @@ describe("VoiceBroadcastPlaybackBody", () => {
});
});

describe("when rendering a playing broadcast", () => {
beforeEach(() => {
mocked(playback.getState).mockReturnValue(VoiceBroadcastPlaybackState.Playing);
mocked(playback.getLiveness).mockReturnValue("not-live");
renderResult = render(<VoiceBroadcastPlaybackBody playback={playback} />);
});

it("should render as expected", () => {
expect(renderResult.container).toMatchSnapshot();
});

describe("and being in the middle of the playback", () => {
beforeEach(() => {
act(() => {
playback.emit(VoiceBroadcastPlaybackEvent.PositionChanged, 10 * 60 * 1000); // 10:00
});
});

describe("and clicking 30s backward", () => {
beforeEach(async () => {
await act(async () => {
await userEvent.click(screen.getByLabelText("30s backward"));
});
});

it("should seek 30s backward", () => {
expect(playback.skipTo).toHaveBeenCalledWith(9 * 60 + 30);
});
});

describe("and clicking 30s forward", () => {
beforeEach(async () => {
await act(async () => {
await userEvent.click(screen.getByLabelText("30s forward"));
});
});

it("should seek 30s forward", () => {
expect(playback.skipTo).toHaveBeenCalledWith(10 * 60 + 30);
});
});
});
});

describe(`when rendering a stopped broadcast`, () => {
beforeEach(() => {
mocked(playback.getState).mockReturnValue(VoiceBroadcastPlaybackState.Stopped);
mocked(playback.getLiveness).mockReturnValue("not-live");
renderResult = render(<VoiceBroadcastPlaybackBody playback={playback} />);
});

it("should render as expected", () => {
expect(renderResult.container).toMatchSnapshot();
});

describe("and clicking the play button", () => {
beforeEach(async () => {
await userEvent.click(renderResult.getByLabelText("play voice broadcast"));
Expand All @@ -104,8 +153,8 @@ describe("VoiceBroadcastPlaybackBody", () => {
});
});

it("should render as expected", () => {
expect(renderResult.container).toMatchSnapshot();
it("should render the new length", async () => {
expect(await screen.findByText("00:42")).toBeInTheDocument();
});
});
});
Expand Down
Loading

0 comments on commit d699f56

Please sign in to comment.