Skip to content

Commit

Permalink
Fix broken tests/refs with refactor of mediastore
Browse files Browse the repository at this point in the history
  • Loading branch information
enigma0Z committed Dec 10, 2024
1 parent 8f2ff18 commit 17e18fb
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 207 deletions.
32 changes: 16 additions & 16 deletions components/AudioPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useStores } from '../hooks/useStores';
import { msToTicks } from '../utils/Time';

const AudioPlayer = observer(() => {
const { rootStore } = useStores();
const { mediaStore } = useStores();

const [ player, setPlayer ] = useState();

Expand Down Expand Up @@ -57,46 +57,46 @@ const AudioPlayer = observer(() => {
didJustFinish === undefined ||
isPlaying === undefined ||
positionMs === undefined ||
rootStore.mediaStore.isFinished
mediaStore.isFinished
) {
return;
}
rootStore.mediaStore.isFinished = didJustFinish;
rootStore.mediaStore.isPlaying = isPlaying;
rootStore.mediaStore.positionTicks = msToTicks(positionMs);
mediaStore.isFinished = didJustFinish;
mediaStore.isPlaying = isPlaying;
mediaStore.positionTicks = msToTicks(positionMs);
});
setPlayer(sound);
}
};

if (rootStore.mediaStore.type === MediaTypes.Audio) {
if (mediaStore.type === MediaTypes.Audio) {
createPlayer({
uri: rootStore.mediaStore.uri,
positionMillis: rootStore.mediaStore.positionMillis
uri: mediaStore.uri,
positionMillis: mediaStore.positionMillis
});
}
}, [ rootStore.mediaStore.type, rootStore.mediaStore.uri ]);
}, [ mediaStore.type, mediaStore.uri ]);

// Update the play/pause state when the store indicates it should
useEffect(() => {
if (rootStore.mediaStore.type === MediaTypes.Audio && rootStore.mediaStore.shouldPlayPause) {
if (rootStore.mediaStore.isPlaying) {
if (mediaStore.type === MediaTypes.Audio && mediaStore.shouldPlayPause) {
if (mediaStore.isPlaying) {
player?.pauseAsync();
} else {
player?.playAsync();
}
rootStore.mediaStore.shouldPlayPause = false;
mediaStore.shouldPlayPause = false;
}
}, [ rootStore.mediaStore.shouldPlayPause ]);
}, [ mediaStore.shouldPlayPause ]);

// Stop the player when the store indicates it should stop playback
useEffect(() => {
if (rootStore.mediaStore.type === MediaTypes.Audio && rootStore.mediaStore.shouldStop) {
if (mediaStore.type === MediaTypes.Audio && mediaStore.shouldStop) {
player?.stopAsync();
player?.unloadAsync();
rootStore.mediaStore.shouldStop = false;
mediaStore.shouldStop = false;
}
}, [ rootStore.mediaStore.shouldStop ]);
}, [ mediaStore.shouldStop ]);

return <></>;
});
Expand Down
18 changes: 9 additions & 9 deletions components/NativeShellWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import RefreshWebView from './RefreshWebView';

const NativeShellWebView = observer(
function NativeShellWebView(props, ref) {
const { rootStore, downloadStore, serverStore } = useStores();
const { rootStore, downloadStore, serverStore, mediaStore } = useStores();
const [ isRefreshing, setIsRefreshing ] = useState(false);

const server = serverStore.servers[rootStore.settingStore.activeServer];
Expand Down Expand Up @@ -70,7 +70,7 @@ true;
if (rootStore.isFullscreen) return;

// Stop media playback in native players
rootStore.mediaStore.shouldStop = true;
mediaStore.shouldStop = true;

setIsRefreshing(true);
ref.current?.reload();
Expand Down Expand Up @@ -124,19 +124,19 @@ true;
break;
case 'ExpoAudioPlayer.play':
case 'ExpoVideoPlayer.play':
rootStore.mediaStore.type = event === 'ExpoAudioPlayer.play' ? MediaTypes.Audio : MediaTypes.Video;
rootStore.mediaStore.uri = data.url;
rootStore.mediaStore.backdropUri = data.backdropUrl;
rootStore.mediaStore.isFinished = false;
rootStore.mediaStore.positionTicks = data.playerStartPositionTicks;
mediaStore.type = event === 'ExpoAudioPlayer.play' ? MediaTypes.Audio : MediaTypes.Video;
mediaStore.uri = data.url;
mediaStore.backdropUri = data.backdropUrl;
mediaStore.isFinished = false;
mediaStore.positionTicks = data.playerStartPositionTicks;
break;
case 'ExpoAudioPlayer.playPause':
case 'ExpoVideoPlayer.playPause':
rootStore.mediaStore.shouldPlayPause = true;
mediaStore.shouldPlayPause = true;
break;
case 'ExpoAudioPlayer.stop':
case 'ExpoVideoPlayer.stop':
rootStore.mediaStore.shouldStop = true;
mediaStore.shouldStop = true;
break;
case 'console.debug':
// console.debug('[Browser Console]', data);
Expand Down
32 changes: 16 additions & 16 deletions components/VideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useStores } from '../hooks/useStores';
import { msToTicks } from '../utils/Time';

const VideoPlayer = observer(() => {
const { rootStore } = useStores();
const { rootStore, mediaStore } = useStores();

const player = useRef(null);
// Local player fullscreen state
Expand All @@ -31,37 +31,37 @@ const VideoPlayer = observer(() => {

// Update the player when media type or uri changes
useEffect(() => {
if (rootStore.mediaStore.type === MediaTypes.Video) {
if (mediaStore.type === MediaTypes.Video) {
rootStore.didPlayerCloseManually = true;
player.current?.loadAsync({
uri: rootStore.mediaStore.uri
uri: mediaStore.uri
}, {
positionMillis: rootStore.mediaStore.positionMillis,
positionMillis: mediaStore.positionMillis,
shouldPlay: true
});
}
}, [ rootStore.mediaStore.type, rootStore.mediaStore.uri ]);
}, [ mediaStore.type, mediaStore.uri ]);

// Update the play/pause state when the store indicates it should
useEffect(() => {
if (rootStore.mediaStore.type === MediaTypes.Video && rootStore.mediaStore.shouldPlayPause) {
if (rootStore.mediaStore.isPlaying) {
if (mediaStore.type === MediaTypes.Video && mediaStore.shouldPlayPause) {
if (mediaStore.isPlaying) {
player.current?.pauseAsync();
} else {
player.current?.playAsync();
}
rootStore.mediaStore.shouldPlayPause = false;
mediaStore.shouldPlayPause = false;
}
}, [ rootStore.mediaStore.shouldPlayPause ]);
}, [ mediaStore.shouldPlayPause ]);

// Close the player when the store indicates it should stop playback
useEffect(() => {
if (rootStore.mediaStore.type === MediaTypes.Video && rootStore.mediaStore.shouldStop) {
if (mediaStore.type === MediaTypes.Video && mediaStore.shouldStop) {
rootStore.didPlayerCloseManually = false;
closeFullscreen();
rootStore.mediaStore.shouldStop = false;
mediaStore.shouldStop = false;
}
}, [ rootStore.mediaStore.shouldStop ]);
}, [ mediaStore.shouldStop ]);

const openFullscreen = () => {
if (!isPresenting) {
Expand All @@ -86,7 +86,7 @@ const VideoPlayer = observer(() => {
<Video
ref={player}
usePoster
posterSource={{ uri: rootStore.mediaStore.backdropUri }}
posterSource={{ uri: mediaStore.backdropUri }}
resizeMode='contain'
useNativeControls
onReadyForDisplay={openFullscreen}
Expand All @@ -96,8 +96,8 @@ const VideoPlayer = observer(() => {
closeFullscreen();
return;
}
rootStore.mediaStore.isPlaying = isPlaying;
rootStore.mediaStore.positionTicks = msToTicks(positionMillis);
mediaStore.isPlaying = isPlaying;
mediaStore.positionTicks = msToTicks(positionMillis);
}}
onFullscreenUpdate={({ fullscreenUpdate }) => {
switch (fullscreenUpdate) {
Expand All @@ -114,7 +114,7 @@ const VideoPlayer = observer(() => {
case VideoFullscreenUpdate.PLAYER_DID_DISMISS:
setIsDismissing(false);
rootStore.isFullscreen = false;
rootStore.mediaStore.reset();
mediaStore.reset();
player.current?.unloadAsync()
.catch(console.debug);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports[`VideoPlayer should render correctly 1`] = `
<Image
source={
Object {
"uri": undefined,
"uri": null,
}
}
style={
Expand Down
2 changes: 2 additions & 0 deletions hooks/useStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { useDownloadStore } from '../stores/DownloadStore';
import { useMediaStore } from '../stores/MediaStore';
import { useRootStore } from '../stores/RootStore';
import { useServerStore } from '../stores/ServerStore';

Expand All @@ -12,4 +13,5 @@ export const useStores = () => ({
rootStore: useRootStore(),
downloadStore: useDownloadStore(),
serverStore: useServerStore(),
mediaStore: useMediaStore()
})
8 changes: 4 additions & 4 deletions screens/DownloadScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useStores } from '../hooks/useStores';

const DownloadScreen = () => {
const navigation = useNavigation();
const { rootStore, downloadStore } = useStores();
const { downloadStore, mediaStore } = useStores();
const { t } = useTranslation();
const { theme } = useContext(ThemeContext);
const [ isEditMode, setIsEditMode ] = useState(false);
Expand Down Expand Up @@ -137,9 +137,9 @@ const DownloadScreen = () => {
}}
onPlay={async () => {
item.isNew = false;
rootStore.mediaStore.isLocalFile = true;
rootStore.mediaStore.type = MediaTypes.Video;
rootStore.mediaStore.uri = item.uri;
mediaStore.isLocalFile = true;
mediaStore.type = MediaTypes.Video;
mediaStore.uri = item.uri;
}}
/>
)}
Expand Down
30 changes: 15 additions & 15 deletions screens/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useStores } from '../hooks/useStores';
import { getIconName } from '../utils/Icons';

const HomeScreen = observer(() => {
const { rootStore, serverStore } = useStores();
const { rootStore, serverStore, mediaStore } = useStores();
const navigation = useNavigation();
const { t } = useTranslation();
const insets = useSafeAreaInsets();
Expand Down Expand Up @@ -60,29 +60,29 @@ const HomeScreen = observer(() => {

// Report media updates to the audio/video plugin
useEffect(() => {
if (!rootStore.mediaStore.isLocalFile) {
if (!mediaStore.isLocalFile) {
const status = {
didPlayerCloseManually: rootStore.didPlayerCloseManually,
uri: rootStore.mediaStore.uri,
isFinished: rootStore.mediaStore.isFinished,
isPlaying: rootStore.mediaStore.isPlaying,
positionTicks: rootStore.mediaStore.positionTicks,
positionMillis: rootStore.mediaStore.positionMillis
uri: mediaStore.uri,
isFinished: mediaStore.isFinished,
isPlaying: mediaStore.isPlaying,
positionTicks: mediaStore.positionTicks,
positionMillis: mediaStore.positionMillis
};

if (rootStore.mediaStore.type === MediaTypes.Audio) {
if (mediaStore.type === MediaTypes.Audio) {
webview.current?.injectJavaScript(`window.ExpoAudioPlayer && window.ExpoAudioPlayer._reportStatus(${JSON.stringify(status)});`);
} else if (rootStore.mediaStore.type === MediaTypes.Video) {
} else if (mediaStore.type === MediaTypes.Video) {
webview.current?.injectJavaScript(`window.ExpoVideoPlayer && window.ExpoVideoPlayer._reportStatus(${JSON.stringify(status)});`);
}
}
}, [
rootStore.mediaStore.type,
rootStore.mediaStore.uri,
rootStore.mediaStore.isFinished,
rootStore.mediaStore.isLocalFile,
rootStore.mediaStore.isPlaying,
rootStore.mediaStore.positionTicks
mediaStore.type,
mediaStore.uri,
mediaStore.isFinished,
mediaStore.isLocalFile,
mediaStore.isPlaying,
mediaStore.positionTicks
]);

// Clear the error state when the active server changes
Expand Down
6 changes: 3 additions & 3 deletions screens/__tests__/HomeScreen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jest.mock('../../components/NativeShellWebView', () => 'NativeShellWebView');
jest.mock('../../hooks/useStores');
useStores.mockImplementation(() => ({
rootStore: {
mediaStore: {},
settingStore: {
activeServer: 0
}
},
mediaStore: {},
serverStore: {
servers: [
{
Expand Down Expand Up @@ -59,9 +59,9 @@ describe('HomeScreen', () => {
it('should render null when no servers are present', () => {
useStores.mockImplementationOnce(() => ({
rootStore: {
mediaStore: {},
settingStore: {}
},
mediaStore: {},
serverStore: {},
}));

Expand All @@ -81,11 +81,11 @@ describe('HomeScreen', () => {
it('should render ErrorView when invalid server exists', () => {
useStores.mockImplementationOnce(() => ({
rootStore: {
mediaStore: {},
settingStore: {
activeServer: 0
}
},
mediaStore: {},
serverStore: {
servers: [{}]
}
Expand Down
Loading

0 comments on commit 17e18fb

Please sign in to comment.