supporting ignore and decline calls

This commit is contained in:
balzack 2025-01-29 13:36:16 -08:00
parent 75c129bd17
commit f6bbe0fd24
4 changed files with 72 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import LinearGradient from 'react-native-linear-gradient';
import { Colors } from '../constants/Colors';
import { RTCView } from 'react-native-webrtc';
import { Card } from '../card/Card';
import { activateKeepAwake, deactivateKeepAwake} from "@sayem314/react-native-keep-awake";
export function Calling({ callCard }: { callCard: string }) {
const { state, actions } = useCalling();
@ -20,6 +21,9 @@ export function Calling({ callCard }: { callCard: string }) {
const {height, width} = useWindowDimensions();
const [applyingVideo, setApplyingVideo] = useState(false);
const [applyingAudio, setApplyingAudio] = useState(false);
const [accepting, setAccepting] = useState(null as null|string);
const [ignoring, setIgnoring] = useState(null as null|string);
const [declining, setDeclining] = useState(null as null|string);
const toggleVideo = async () => {
if (!applyingVideo) {
@ -82,15 +86,41 @@ export function Calling({ callCard }: { callCard: string }) {
}
const accept = async (callId, card) => {
if (!connecting) {
setConnecting(true);
if (!accepting) {
setAccepting(callId);
try {
await actions.accept(callId, card);
} catch (err) {
console.log(err);
setAlert(true);
}
setConnecting(false);
setAccepting(null);
}
}
const ignore = async (callId, card) => {
if (!ignoring) {
setIgnoring(callId);
try {
await actions.ignore(callId, card);
} catch (err) {
console.log(err);
setAlert(true);
}
setIgnoring(null);
}
}
const decline = async (callId, card) => {
if (!declining) {
setDeclining(callId);
try {
await actions.decline(callId, card);
} catch (err) {
console.log(err);
setAlert(true);
}
setDeclining(null);
}
}
@ -112,15 +142,23 @@ export function Calling({ callCard }: { callCard: string }) {
}
}, [callCard]);
useEffect(() => {
if (state.calling) {
activateKeepAwake();
} else {
deactivateKeepAwake();
}
}, [state.calling]);
const calls = state.calls.map((contact, index) => {
const { callId, card } = contact;
const { name, handle, node, imageUrl } = card;
const ignore = <IconButton key="ignore" style={styles.circleIcon} iconColor="white" containerColor={Colors.pending} icon="eye-off-outline" compact="true" mode="contained" size={24} onPress={()=>{}} />
const decline = <IconButton key="decline" style={styles.flipIcon} iconColor="white" containerColor={Colors.offsync} icon="phone-outline" compact="true" mode="contained" size={24} onPress={()=>{}} />
const accept = <IconButton key="accept" style={styles.circleIcon} iconColor="white" containerColor={Colors.primary} icon="phone-outline" compact="true" mode="contained" size={24} onPress={()=>actions.accept(callId, card)} />
const ignoreButton = <IconButton key="ignore" style={styles.circleIcon} iconColor="white" containerColor={Colors.pending} icon="eye-off-outline" compact="true" mode="contained" size={24} loading={ignoring===callId} onPress={()=>ignore(callId, card)} />
const declineButton = <IconButton key="decline" style={styles.flipIcon} iconColor="white" containerColor={Colors.offsync} icon="phone-outline" compact="true" mode="contained" size={24} loading={declining===callId} onPress={()=>decline(callId, card)} />
const acceptButton = <IconButton key="accept" style={styles.circleIcon} iconColor="white" containerColor={Colors.primary} icon="phone-outline" compact="true" mode="contained" size={24} loading={accepting===callId} onPress={()=>accept(callId, card)} />
return (
<Surface mode="flat" key={index}>
<Card containerStyle={styles.card} placeholder={''} imageUrl={imageUrl} name={name} node={node} handle={handle} actions={[ignore, decline, accept]} />
<Card containerStyle={styles.card} placeholder={''} imageUrl={imageUrl} name={name} node={node} handle={handle} actions={[ignoreButton, declineButton, acceptButton]} />
</Surface>
)
});

View File

@ -96,16 +96,7 @@ export function useCalling() {
updateState({ failed: true });
}
} else if (status === 'closed') {
call.current = null;
try {
peer.close();
link.close();
} catch (err) {
console.log(err);
}
localStream.current = null;
remoteStream.current = null,
updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null });
updatePeer('close');
}
}
}
@ -196,6 +187,19 @@ export function useCalling() {
}
} else if (type === 'local_track') {
await peerTrack(data);
} else if (type === 'close' && call.current) {
peerUpdate.current = [];
const { peer, link } = call.current;
call.current = null;
try {
peer.close();
link.close();
} catch (err) {
console.log(err);
}
localStream.current = null;
remoteStream.current = null,
updateState({ calling: null, failed: false, audio: null, video: null, local: null, remote: null });
}
}
updatingPeer.current = false;
@ -248,6 +252,14 @@ export function useCalling() {
}, [app.state.session]);
const actions = {
ignore: async (callId: string, card: Card) => {
const ring = app.state.session.getRing();
await ring.ignore(card.cardId, callId);
},
decline: async (callId: string, card: Card) => {
const ring = app.state.session.getRing();
await ring.decline(card.cardId, callId);
},
end: async () => {
if (!call.current) {
throw new Error('no active call');

View File

@ -83,7 +83,7 @@ export class LinkModule implements Link {
}, RING_INTERVAL);
this.ice = ice;
this.connect(callerToken, node, secure);
this.websocket = this.setWebSocket(callerToken, node, secure);
}
public async join(server: string, secure: boolean, token: string, ice: { urls: string; username: string; credential: string }[], endCall: ()=>Promise<void>) {
@ -96,7 +96,7 @@ export class LinkModule implements Link {
this.log.error(err);
}
}
this.websocket = this.setWebSocket(token, node, secure);
this.websocket = this.setWebSocket(token, server, secure);
}
public async close() {

View File

@ -71,7 +71,8 @@ export class RingModule implements Ring {
return link;
}
public async ignore(cardId: stirng, callId: string): Promise<void> {
public async ignore(cardId: string, callId: string): Promise<void> {
const now = (new Date()).getTime();
const id = `${cardId}:${callId}`;
const entry = this.calls.get(id);
if (!entry || entry.expires < now || entry.status !== 'ringing') {
@ -82,6 +83,7 @@ export class RingModule implements Ring {
}
public async decline(cardId: string, callId: string): Promise<void> {
const now = (new Date()).getTime();
const id = `${cardId}:${callId}`;
const entry = this.calls.get(id);
if (!entry || entry.expires < now || entry.status !== 'ringing') {