Audio Player: Fix pausing playback when closing chat

This commit is contained in:
Alexander Zinchuk 2021-04-24 13:36:00 +03:00
parent 17664e6a73
commit 35bf7a5d54
2 changed files with 31 additions and 12 deletions

View File

@ -2,11 +2,13 @@ import React, { FC, useEffect, memo } from '../../lib/teact/teact';
import { getGlobal, withGlobal } from '../../lib/teact/teactn'; import { getGlobal, withGlobal } from '../../lib/teact/teactn';
import { GlobalActions } from '../../global/types'; import { GlobalActions } from '../../global/types';
import { ApiMessage } from '../../api/types';
import '../../modules/actions/all'; import '../../modules/actions/all';
import { ANIMATION_END_DELAY, DEBUG } from '../../config'; import { ANIMATION_END_DELAY, DEBUG } from '../../config';
import { pick } from '../../util/iteratees'; import { pick } from '../../util/iteratees';
import { import {
selectChatMessage,
selectCountNotMutedUnread, selectCountNotMutedUnread,
selectIsForwardModalOpen, selectIsForwardModalOpen,
selectIsMediaViewerOpen, selectIsMediaViewerOpen,
@ -21,9 +23,10 @@ import LeftColumn from '../left/LeftColumn';
import MiddleColumn from '../middle/MiddleColumn'; import MiddleColumn from '../middle/MiddleColumn';
import RightColumn from '../right/RightColumn'; import RightColumn from '../right/RightColumn';
import MediaViewer from '../mediaViewer/MediaViewer.async'; import MediaViewer from '../mediaViewer/MediaViewer.async';
import ForwardPicker from './ForwardPicker.async'; import AudioPlayer from '../middle/AudioPlayer';
import Notifications from './Notifications.async'; import Notifications from './Notifications.async';
import Errors from './Errors.async'; import Errors from './Errors.async';
import ForwardPicker from './ForwardPicker.async';
import './Main.scss'; import './Main.scss';
@ -36,6 +39,7 @@ type StateProps = {
isForwardModalOpen: boolean; isForwardModalOpen: boolean;
hasNotifications: boolean; hasNotifications: boolean;
hasErrors: boolean; hasErrors: boolean;
audioMessage?: ApiMessage;
}; };
type DispatchProps = Pick<GlobalActions, 'loadAnimatedEmojis'>; type DispatchProps = Pick<GlobalActions, 'loadAnimatedEmojis'>;
@ -59,6 +63,7 @@ const Main: FC<StateProps & DispatchProps> = ({
animationLevel, animationLevel,
hasNotifications, hasNotifications,
hasErrors, hasErrors,
audioMessage,
}) => { }) => {
if (DEBUG && !DEBUG_isLogged) { if (DEBUG && !DEBUG_isLogged) {
DEBUG_isLogged = true; DEBUG_isLogged = true;
@ -151,6 +156,7 @@ const Main: FC<StateProps & DispatchProps> = ({
<ForwardPicker isOpen={isForwardModalOpen} /> <ForwardPicker isOpen={isForwardModalOpen} />
<Notifications isOpen={hasNotifications} /> <Notifications isOpen={hasNotifications} />
<Errors isOpen={hasErrors} /> <Errors isOpen={hasErrors} />
{audioMessage && <AudioPlayer key={audioMessage.id} message={audioMessage} noUi />}
</div> </div>
); );
}; };
@ -169,15 +175,23 @@ function updateIcon(asUnread: boolean) {
} }
export default memo(withGlobal( export default memo(withGlobal(
(global): StateProps => ({ (global): StateProps => {
animationLevel: global.settings.byKey.animationLevel, const { chatId: audioChatId, messageId: audioMessageId } = global.audioPlayer;
lastSyncTime: global.lastSyncTime, const audioMessage = audioChatId && audioMessageId
isLeftColumnShown: global.isLeftColumnShown, ? selectChatMessage(global, audioChatId, audioMessageId)
isRightColumnShown: selectIsRightColumnShown(global), : undefined;
isMediaViewerOpen: selectIsMediaViewerOpen(global),
isForwardModalOpen: selectIsForwardModalOpen(global), return {
hasNotifications: Boolean(global.notifications.length), animationLevel: global.settings.byKey.animationLevel,
hasErrors: Boolean(global.errors.length), lastSyncTime: global.lastSyncTime,
}), isLeftColumnShown: global.isLeftColumnShown,
isRightColumnShown: selectIsRightColumnShown(global),
isMediaViewerOpen: selectIsMediaViewerOpen(global),
isForwardModalOpen: selectIsForwardModalOpen(global),
hasNotifications: Boolean(global.notifications.length),
hasErrors: Boolean(global.errors.length),
audioMessage,
};
},
(setGlobal, actions): DispatchProps => pick(actions, ['loadAnimatedEmojis']), (setGlobal, actions): DispatchProps => pick(actions, ['loadAnimatedEmojis']),
)(Main)); )(Main));

View File

@ -24,6 +24,7 @@ import './AudioPlayer.scss';
type OwnProps = { type OwnProps = {
message: ApiMessage; message: ApiMessage;
className?: string; className?: string;
noUi?: boolean;
}; };
type StateProps = { type StateProps = {
@ -33,7 +34,7 @@ type StateProps = {
type DispatchProps = Pick<GlobalActions, 'focusMessage' | 'closeAudioPlayer'>; type DispatchProps = Pick<GlobalActions, 'focusMessage' | 'closeAudioPlayer'>;
const AudioPlayer: FC<OwnProps & StateProps & DispatchProps> = ({ const AudioPlayer: FC<OwnProps & StateProps & DispatchProps> = ({
message, className, senderName, focusMessage, closeAudioPlayer, message, className, noUi, senderName, focusMessage, closeAudioPlayer,
}) => { }) => {
const mediaData = mediaLoader.getFromMemory(getMessageMediaHash(message, 'inline')!) as (string | undefined); const mediaData = mediaLoader.getFromMemory(getMessageMediaHash(message, 'inline')!) as (string | undefined);
const { playPause, isPlaying } = useAudioPlayer( const { playPause, isPlaying } = useAudioPlayer(
@ -53,6 +54,10 @@ const AudioPlayer: FC<OwnProps & StateProps & DispatchProps> = ({
const lang = useLang(); const lang = useLang();
if (noUi) {
return undefined;
}
const audio = getMessageAudio(message); const audio = getMessageAudio(message);
return ( return (