Add support to non temporary portals from deep link

This commit is contained in:
Tiago Daniel Jacobs 2023-01-08 20:33:51 -03:00
parent 5e4c9810fa
commit 6f9b779248
2 changed files with 72 additions and 41 deletions

View File

@ -42,7 +42,23 @@ async function addPortalToStorage({
url, url,
temporary temporary
}: IPortalToAdd): Promise<IPortal[]> { }: IPortalToAdd): Promise<IPortal[]> {
// Copy current elements
const copyOfPortals = [...portals];
// Remove elements without changing reference
portals.length=0;
// Add all but those with same name of the new
copyOfPortals.forEach((portal: IPortal)=>{
if(portal.name == name) return;
portals.push(portal);
})
// Add the new portal
portals.push({name, url, temporary}); portals.push({name, url, temporary});
// Store new list
await AsyncStorage.setItem('portal', JSON.stringify(portals)); await AsyncStorage.setItem('portal', JSON.stringify(portals));
return portals; return portals;
} }

View File

@ -18,91 +18,106 @@ const DeepLink = ()=>{
const SCHEME = 'bigbluebutton-tablet://'; const SCHEME = 'bigbluebutton-tablet://';
const SCHEME_DEFAULT = 'https://' const SCHEME_DEFAULT = 'https://'
var NAME_PORTALS_DEEP_LINK = i18next.t('mobileApp.portals.namePortal.deepLink'); var lastDeepLinkPortalName = null;
const navigate = useNavigation(); const navigation = useNavigation();
const {portals, setPortals} = usePortal(); const {portals, setPortals} = usePortal();
async function createTemporaryPortalFromDeepLink(link: string){ async function createPortalFromDeepLink(link: string){
const linkWithoutScheme = link.replace(SCHEME, ''); const linkWithoutScheme = link.replace(SCHEME, '').replace(/\+/g, ' ');
if(linkWithoutScheme === ''){ if(linkWithoutScheme === ''){
navigate.navigate(i18next.t('mobileApp.portals.drawerNavigation.button.label')) navigation.navigate(i18next.t('mobileApp.portals.drawerNavigation.button.label'))
return Alert.alert(i18next.t('mobileApp.portals.handleWithoutURL')) return Alert.alert(i18next.t('mobileApp.portals.handleWithoutURL'))
} }
let roomNameWBar = linkWithoutScheme.match(/^[A-z-.\w]+\//) let roomNameWBar = linkWithoutScheme.match(/^[-.\w +]+\//)
if(!roomNameWBar) { if(!roomNameWBar) {
navigate.navigate(i18next.t('mobileApp.portals.drawerNavigation.button.label')) navigation.navigate(i18next.t('mobileApp.portals.drawerNavigation.button.label'))
return Alert.alert(i18next.t('mobileApp.portals.handleWithoutURL')) return Alert.alert(i18next.t('mobileApp.portals.handleWithoutURL'))
} }
let roomName = roomNameWBar[0].replace(/\//, '') let roomName = roomNameWBar[0].replace(/\//, '')
if(roomName != 'bigbluebutton'){ if(roomName === 'bigbluebutton'){
NAME_PORTALS_DEEP_LINK = roomName roomName = i18next.t('mobileApp.portals.namePortal.deepLink');
} }
let linkWhitoutSchemeAndName = linkWithoutScheme.replace(/^[A-z-.\w]+\//, '') lastDeepLinkPortalName = roomName;
if (!linkWhitoutSchemeAndName.includes('://')) { let linkWithoutSchemeAndName = linkWithoutScheme.replace(/^[-.\w +]+\//, '')
linkWhitoutSchemeAndName = SCHEME_DEFAULT + linkWhitoutSchemeAndName
if (!linkWithoutSchemeAndName.includes('://')) {
linkWithoutSchemeAndName = SCHEME_DEFAULT + linkWithoutSchemeAndName
} }
// Join links are temporary (discarded on next app launch)
const isTemporary = linkWithoutScheme.includes('/bigbluebutton/api/join?');
const portalToAdd:IPortal = { const portalToAdd:IPortal = {
name: NAME_PORTALS_DEEP_LINK, name: lastDeepLinkPortalName,
url: linkWhitoutSchemeAndName, url: linkWithoutSchemeAndName,
temporary: true temporary: isTemporary
} }
// Adding LinkedPortal to AsyncStorage // Adding LinkedPortal to AsyncStorage
const newPortals = await createNewPortal(portalToAdd) const newPortals = await createNewPortal(portalToAdd)
// Adding to context // Adding to context
setPortals(newPortals) setPortals(newPortals)
// Navigation to portal
navigate.navigate(NAME_PORTALS_DEEP_LINK) // Navigate to it
navigation.navigate(lastDeepLinkPortalName);
} }
async function checkIfHaveTemporaryPortal(){ async function checkIfHaveTemporaryPortal(){
let portalsParsed: IPortal[]|null = null; let portalsParsed: IPortal[]|null = null;
try { try {
let items = await AsyncStorage.getAllKeys(); let items = await AsyncStorage.getAllKeys();
if (items.includes('portal')) {
// If portal storage is empty, initialize it
if(!items.includes('portal')) {
await AsyncStorage.setItem('portal', JSON.stringify([]));
}
const portalsFromStorage = await AsyncStorage.getItem('portal') const portalsFromStorage = await AsyncStorage.getItem('portal')
portalsParsed = portalsFromStorage ? JSON.parse(portalsFromStorage) : null portalsParsed = portalsFromStorage ? JSON.parse(portalsFromStorage) : null
} else {
console.log('Error: Dont Have Portals Storage');
}
} catch (e) { } catch (e) {
console.log('error', e); console.log('error', e);
} }
if(!portalsParsed){ if(!portalsParsed){
return console.error("Invalid portal storage: ", portalsParsed);
return;
} }
// Clear temporary portals from previous executions
const portalsWithoutTemporary = portalsParsed.filter((portal: IPortal)=>{ const portalsWithoutTemporary = portalsParsed.filter((portal: IPortal)=>{
if(portal.temporary == true) return false if(portal.temporary == true) return false;
if(portal.name == NAME_PORTALS_DEEP_LINK) return false if(portal.name == lastDeepLinkPortalName) return false;
return portal return portal;
}) });
await AsyncStorage.setItem('portal', JSON.stringify(portalsWithoutTemporary)) await AsyncStorage.setItem('portal', JSON.stringify(portalsWithoutTemporary));
setPortals(portalsWithoutTemporary) setPortals(portalsWithoutTemporary);
getLinkFromAppClosed() //To app running in backgrond then open throug deep link // Handle app launch with link
getLinkFromAppLaunch();
// Handle link when app is running
Linking.addEventListener('url', (url)=>{ Linking.addEventListener('url', (url)=>{
// if app is open when it's minimized (not running in background) createPortalFromDeepLink(url.url);
createTemporaryPortalFromDeepLink(url.url) });
})
} }
async function getLinkFromAppClosed(){ async function getLinkFromAppLaunch(){
const linkFromAppClosed = await Linking.getInitialURL() const linkFromAppClosed = await Linking.getInitialURL();
if(linkFromAppClosed === null) return if(linkFromAppClosed === null) return;
createTemporaryPortalFromDeepLink(linkFromAppClosed) createPortalFromDeepLink(linkFromAppClosed);
} }
React.useEffect(() => { React.useEffect(() => {
checkIfHaveTemporaryPortal() checkIfHaveTemporaryPortal();
}, []); }, []);
return null
return null;
} }
const Drawer = createDrawerNavigator(); const Drawer = createDrawerNavigator();