mirror of
https://github.com/balzack/databag.git
synced 2025-05-05 07:55:15 +00:00
supporting ignore and decline calls
This commit is contained in:
parent
75c129bd17
commit
f6bbe0fd24
@ -11,6 +11,7 @@ import LinearGradient from 'react-native-linear-gradient';
|
|||||||
import { Colors } from '../constants/Colors';
|
import { Colors } from '../constants/Colors';
|
||||||
import { RTCView } from 'react-native-webrtc';
|
import { RTCView } from 'react-native-webrtc';
|
||||||
import { Card } from '../card/Card';
|
import { Card } from '../card/Card';
|
||||||
|
import { activateKeepAwake, deactivateKeepAwake} from "@sayem314/react-native-keep-awake";
|
||||||
|
|
||||||
export function Calling({ callCard }: { callCard: string }) {
|
export function Calling({ callCard }: { callCard: string }) {
|
||||||
const { state, actions } = useCalling();
|
const { state, actions } = useCalling();
|
||||||
@ -20,6 +21,9 @@ export function Calling({ callCard }: { callCard: string }) {
|
|||||||
const {height, width} = useWindowDimensions();
|
const {height, width} = useWindowDimensions();
|
||||||
const [applyingVideo, setApplyingVideo] = useState(false);
|
const [applyingVideo, setApplyingVideo] = useState(false);
|
||||||
const [applyingAudio, setApplyingAudio] = 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 () => {
|
const toggleVideo = async () => {
|
||||||
if (!applyingVideo) {
|
if (!applyingVideo) {
|
||||||
@ -82,15 +86,41 @@ export function Calling({ callCard }: { callCard: string }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const accept = async (callId, card) => {
|
const accept = async (callId, card) => {
|
||||||
if (!connecting) {
|
if (!accepting) {
|
||||||
setConnecting(true);
|
setAccepting(callId);
|
||||||
try {
|
try {
|
||||||
await actions.accept(callId, card);
|
await actions.accept(callId, card);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
setAlert(true);
|
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]);
|
}, [callCard]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.calling) {
|
||||||
|
activateKeepAwake();
|
||||||
|
} else {
|
||||||
|
deactivateKeepAwake();
|
||||||
|
}
|
||||||
|
}, [state.calling]);
|
||||||
|
|
||||||
const calls = state.calls.map((contact, index) => {
|
const calls = state.calls.map((contact, index) => {
|
||||||
const { callId, card } = contact;
|
const { callId, card } = contact;
|
||||||
const { name, handle, node, imageUrl } = card;
|
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 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 decline = <IconButton key="decline" style={styles.flipIcon} iconColor="white" containerColor={Colors.offsync} icon="phone-outline" compact="true" mode="contained" size={24} onPress={()=>{}} />
|
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 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 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 (
|
return (
|
||||||
<Surface mode="flat" key={index}>
|
<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>
|
</Surface>
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
@ -96,16 +96,7 @@ export function useCalling() {
|
|||||||
updateState({ failed: true });
|
updateState({ failed: true });
|
||||||
}
|
}
|
||||||
} else if (status === 'closed') {
|
} else if (status === 'closed') {
|
||||||
call.current = null;
|
updatePeer('close');
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,6 +187,19 @@ export function useCalling() {
|
|||||||
}
|
}
|
||||||
} else if (type === 'local_track') {
|
} else if (type === 'local_track') {
|
||||||
await peerTrack(data);
|
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;
|
updatingPeer.current = false;
|
||||||
@ -248,6 +252,14 @@ export function useCalling() {
|
|||||||
}, [app.state.session]);
|
}, [app.state.session]);
|
||||||
|
|
||||||
const actions = {
|
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 () => {
|
end: async () => {
|
||||||
if (!call.current) {
|
if (!call.current) {
|
||||||
throw new Error('no active call');
|
throw new Error('no active call');
|
||||||
|
@ -83,7 +83,7 @@ export class LinkModule implements Link {
|
|||||||
}, RING_INTERVAL);
|
}, RING_INTERVAL);
|
||||||
|
|
||||||
this.ice = ice;
|
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>) {
|
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.log.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.websocket = this.setWebSocket(token, node, secure);
|
this.websocket = this.setWebSocket(token, server, secure);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async close() {
|
public async close() {
|
||||||
|
@ -71,7 +71,8 @@ export class RingModule implements Ring {
|
|||||||
return link;
|
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 id = `${cardId}:${callId}`;
|
||||||
const entry = this.calls.get(id);
|
const entry = this.calls.get(id);
|
||||||
if (!entry || entry.expires < now || entry.status !== 'ringing') {
|
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> {
|
public async decline(cardId: string, callId: string): Promise<void> {
|
||||||
|
const now = (new Date()).getTime();
|
||||||
const id = `${cardId}:${callId}`;
|
const id = `${cardId}:${callId}`;
|
||||||
const entry = this.calls.get(id);
|
const entry = this.calls.get(id);
|
||||||
if (!entry || entry.expires < now || entry.status !== 'ringing') {
|
if (!entry || entry.expires < now || entry.status !== 'ringing') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user