Skip to content

Commit

Permalink
Update package.json, README.md, and refactor VolumeManager module
Browse files Browse the repository at this point in the history
- Added new keywords to package.json for better discoverability: "expo", "volume", "mute", "silent".
- Enhanced README.md to clarify that the library is not functional on the web, while maintaining existing functionality descriptions.
- Refactored imports in hooks.ts and index.ts to use the new module.ts file for improved organization.
- Introduced module.ts and module.web.ts to separate platform-specific implementations and provide no-op methods for web compatibility.
  • Loading branch information
hirbod committed Dec 15, 2024
1 parent 0f07aa6 commit 0f1c285
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# react-native-volume-manager

Take control of system volume on iOS and Android with this powerful native package. Seamlessly adjust volume levels, track changes, and design custom sliders for a tailored user experience. With an intuitive API, you can access the current volume, detect the silent switch on iOS, and monitor ringer mode changes on Android.
Take control of system volume on **iOS** and **Android** with this powerful native package. Seamlessly adjust volume levels, track changes, and design custom sliders for a tailored user experience. With an intuitive API, you can access the current volume, detect the silent switch on iOS, and monitor ringer mode changes on Android.

| ![React Native Volume Manager](ios-preview.gif) | ![React Native Volume Manager](android-preview.gif) |
| ----------------------------------------------- | --------------------------------------------------- |
Expand Down Expand Up @@ -58,6 +58,10 @@ If you are using Expo, make sure to use expo-build-properties to set the minimum

- Android: It runs on both a real device (API level 21+) and the emulator (API level 33+).

## Web

This library is not functional on the web. While the package exports no-op methods for web usage, allowing you to include it without any issues, these methods have no actual effect.

## Usage

All methods are available under the `VolumeManager` namespace or can be imported directly. Here are some examples:
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"keywords": [
"react-native",
"ios",
"expo",
"volume",
"mute",
"silent",
"android"
],
"repository": "https://github.com/hirbod/react-native-volume-manager",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getRingerMode,
setNativeSilenceCheckInterval,
setRingerMode,
} from './native';
} from './module';
import type { RingerModeType } from './types';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './native';
export * from './module';
export * from './hooks';
export * from './types';
File renamed without changes.
173 changes: 173 additions & 0 deletions src/module.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import type {
AVAudioSessionCategory,
AVAudioSessionMode,
EmitterSubscriptionNoop,
RingMuteSwitchEventCallback,
RingerEventCallback,
RingerModeType,
VolumeManagerSetVolumeConfig,
VolumeResult,
} from './types';

// Track if warning has been shown
let hasWarned = false;

// Helper function to show warning only in development
const warnOnWeb = () => {
if (__DEV__ && !hasWarned) {
console.warn(
'react-native-volume-manager is not functional on the web. While the package exports no-op methods for web usage, allowing you to include it without any issues, these methods have no actual effect. This warning is only visible in development mode.'
);
hasWarned = true;
}
};

const noopEmitterSubscription: EmitterSubscriptionNoop = {
remove: () => {
// noop
},
};

// Base volume result for web platform
const defaultVolumeResult: VolumeResult = {
volume: 1,
};

export async function getRingerMode(): Promise<RingerModeType | undefined> {
warnOnWeb();
return undefined;
}

export async function setRingerMode(
_mode: RingerModeType
): Promise<RingerModeType | undefined> {
warnOnWeb();
return undefined;
}

export async function enable(
_enabled: boolean = true,
_async: boolean = true
): Promise<void> {
warnOnWeb();
return undefined;
}

export async function setActive(
_value: boolean = true,
_async: boolean = true
): Promise<void> {
warnOnWeb();
return undefined;
}

export async function setCategory(
_value: AVAudioSessionCategory,
_mixWithOthers: boolean = false
): Promise<void> {
warnOnWeb();
return undefined;
}

export async function setMode(_value: AVAudioSessionMode): Promise<void> {
warnOnWeb();
return undefined;
}

export async function enableInSilenceMode(
_enabled: boolean = true
): Promise<void> {
warnOnWeb();
return undefined;
}

export async function checkDndAccess(): Promise<boolean | undefined> {
warnOnWeb();
return undefined;
}

export async function requestDndAccess(): Promise<boolean | undefined> {
warnOnWeb();
return undefined;
}

export async function getVolume(): Promise<VolumeResult> {
warnOnWeb();
return defaultVolumeResult;
}

export async function setVolume(
_value: number,
_config: VolumeManagerSetVolumeConfig = {}
): Promise<void> {
warnOnWeb();
return undefined;
}

export async function showNativeVolumeUI(_config: {
enabled: boolean;
}): Promise<void> {
warnOnWeb();
return undefined;
}

export function addVolumeListener(
_callback: (result: VolumeResult) => void
): EmitterSubscriptionNoop {
warnOnWeb();
return noopEmitterSubscription;
}

export const addSilentListener = (
_callback: RingMuteSwitchEventCallback
): EmitterSubscriptionNoop => {
warnOnWeb();
return noopEmitterSubscription;
};

export const setNativeSilenceCheckInterval = (_value: number): void => {
warnOnWeb();
// noop
};

export const isAndroidDeviceSilent = (): Promise<boolean | null> => {
warnOnWeb();
return Promise.resolve(null);
};

export const addRingerListener = (
_callback: RingerEventCallback
): EmitterSubscriptionNoop => {
warnOnWeb();
return noopEmitterSubscription;
};

export const removeRingerListener = (
_listener: EmitterSubscriptionNoop
): void => {
warnOnWeb();
// noop
};

export const VolumeManager = {
addVolumeListener,
getVolume,
setVolume,
showNativeVolumeUI,
isAndroidDeviceSilent,
addSilentListener,
addRingerListener,
removeRingerListener,
setNativeSilenceCheckInterval,
getRingerMode,
setRingerMode,
checkDndAccess,
requestDndAccess,
enable,
setActive,
setCategory,
setMode,
enableInSilenceMode,
};

export default VolumeManager;

0 comments on commit 0f1c285

Please sign in to comment.