import { reportScreenChangeOnNative } from '../nativeCommunication'; import { ScreenChangePayload, ScreenHierarchyElement, type ScreenChangeType, type ScreenDetectionType, } from '../ScreenChangePayload'; import { validateChangeType, validateDetectionType, validateScreenHierarchy, validateScreenName, } from './manualNavigationTrackingValidation'; /** * Reports the appearance of a screen. * * @param {string} screenName - The name of the screen that is appearing. * @returns {void} * @throws Error if validation fails for the screen name. * * @example * reportScreenAppearance('Home'); */ export function reportScreenAppearance(screenName: string): void { try { validateScreenName(screenName); } catch (error) { console.error(error); return; } reportScreenChange( [new ScreenHierarchyElement(screenName)], 'appear', 'manual' ); } /** * Reports the disappearance of a screen. * * @param {string} screenName - The name of the screen that is disappearing. * @returns {void} * @throws Error if validation fails for the screen name. * * @example * reportScreenDisappearance('Home'); */ export function reportScreenDisappearance(screenName: string): void { try { validateScreenName(screenName); } catch (error) { console.error(error); return; } reportScreenChange( [new ScreenHierarchyElement(screenName)], 'disappear', 'manual' ); } /** * Reports the appearance or disappearance of a screen. This is a specialized function * for reporting screen changes, utilizing a hierarchy of screens instead of a single screen name. * * @param {ScreenHierarchyElement[]} screenHierarchy - Hierarchy of active screens that make up the UI. * @param {ScreenChangeType} changeType - Type of the screen change. * @param {ScreenDetectionType} [detectionType='manual'] - Method of detecting the screen change. * @returns {void} * @throws Error if any validation fails for the provided parameters. * * @example * reportScreenChange( * [ * ScreenHierarchyElement('Home', 'drawer-navigator'), * ScreenHierarchyElement('Search', 'bottom-tabs-navigator'), * ScreenHierarchyElement('ItemDetails', 'screen'), * ], * 'appear', * 'manual' * ); */ export function reportScreenChange( screenHierarchy: ScreenHierarchyElement[], changeType: ScreenChangeType, detectionType: ScreenDetectionType = 'manual' ): void { try { validateScreenHierarchy(screenHierarchy); validateChangeType(changeType); validateDetectionType(detectionType); } catch (error) { console.error(error); return; } const screenChangePayload = new ScreenChangePayload( screenHierarchy, changeType, detectionType ); reportScreenChangeOnNative(screenChangePayload); }