react-native-acoustic-connect-beta
Version:
BETA: React native plugin for Acoustic Connect
203 lines (186 loc) • 5.61 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
/********************************************************************************************
* Copyright (C) 2025 Acoustic, L.P. All rights reserved.
*
* NOTICE: This file contains material that is confidential and proprietary to
* Acoustic, L.P. and/or other developers. No license is granted under any intellectual or
* industrial property rights of Acoustic, L.P. except as may be provided in an agreement with
* Acoustic, L.P. Any unauthorized copying or distribution of content from this file is
* prohibited.
********************************************************************************************/
// Simple ID generator
function generateId() {
return Math.random().toString(36).substr(2, 9) + Date.now().toString(36);
}
class DialogListener {
activeDialogs = new Map();
isIntercepting = false;
eventCallbacks = [];
constructor() {
this.originalAlert = _reactNative.Alert.alert;
this.interceptAlertAPI();
}
static getInstance() {
if (!DialogListener.instance) {
DialogListener.instance = new DialogListener();
}
return DialogListener.instance;
}
/**
* Start intercepting dialog events
*/
startIntercepting() {
if (this.isIntercepting) return;
this.isIntercepting = true;
console.log('DialogListener: Started intercepting dialog events');
}
/**
* Stop intercepting dialog events
*/
stopIntercepting() {
if (!this.isIntercepting) return;
this.isIntercepting = false;
console.log('DialogListener: Stopped intercepting dialog events');
}
/**
* Add event callback for dialog events
*/
addEventListener(callback) {
this.eventCallbacks.push(callback);
return () => {
const index = this.eventCallbacks.indexOf(callback);
if (index > -1) {
this.eventCallbacks.splice(index, 1);
}
};
}
/**
* Intercept React Native's Alert.alert API
*/
interceptAlertAPI() {
const self = this;
_reactNative.Alert.alert = function (title, message, buttons, options) {
if (self.isIntercepting) {
const dialogId = generateId();
const dialogEvent = {
dialogId,
dialogTitle: title,
dialogType: 'alert',
timestamp: Date.now(),
buttons: buttons || []
};
self.activeDialogs.set(dialogId, dialogEvent);
self.emitEvent(dialogEvent);
// Create wrapped buttons that log events
const wrappedButtons = buttons?.map((button, index) => ({
...button,
onPress: () => {
const buttonClickEvent = {
dialogId,
buttonText: button.text || '',
buttonIndex: index,
timestamp: Date.now()
};
self.emitEvent(buttonClickEvent);
self.activeDialogs.delete(dialogId);
// Call original onPress if it exists
if (button.onPress) {
button.onPress();
}
}
}));
// Call original Alert.alert with wrapped buttons
self.originalAlert.call(_reactNative.Alert, title, message, wrappedButtons, options);
} else {
// Call original Alert.alert without interception
self.originalAlert.call(_reactNative.Alert, title, message, buttons, options);
}
};
}
/**
* Manually track custom dialog show event
*/
trackCustomDialogShow(dialogId, title, buttons) {
if (!this.isIntercepting) return;
const dialogEvent = {
dialogId,
dialogTitle: title,
dialogType: 'custom',
timestamp: Date.now(),
buttons: buttons || []
};
this.activeDialogs.set(dialogId, dialogEvent);
this.emitEvent(dialogEvent);
}
/**
* Manually track custom dialog dismiss event
*/
trackCustomDialogDismiss(dialogId, reason = 'manual') {
if (!this.isIntercepting) return;
const dialogEvent = this.activeDialogs.get(dialogId);
if (dialogEvent) {
this.activeDialogs.delete(dialogId);
// Emit proper dismiss event
const dismissEvent = {
dialogId,
dialogTitle: dialogEvent.dialogTitle,
dialogType: dialogEvent.dialogType,
dismissReason: reason,
timestamp: Date.now()
};
this.emitEvent(dismissEvent);
// Log the dismiss reason for debugging
console.log(`Dialog ${dialogId} dismissed with reason: ${reason}`);
}
}
/**
* Manually track custom dialog button click event
*/
trackCustomDialogButtonClick(dialogId, buttonText, buttonIndex) {
if (!this.isIntercepting) return;
const buttonClickEvent = {
dialogId,
buttonText,
buttonIndex,
timestamp: Date.now()
};
this.emitEvent(buttonClickEvent);
}
/**
* Get currently active dialogs
*/
getActiveDialogs() {
return Array.from(this.activeDialogs.values());
}
/**
* Clear all active dialogs
*/
clearActiveDialogs() {
this.activeDialogs.clear();
}
/**
* Emit event to all registered callbacks
*/
emitEvent(event) {
this.eventCallbacks.forEach(callback => {
try {
callback(event);
} catch (error) {
console.error('DialogListener: Error in event callback:', error);
}
});
}
/**
* Restore original Alert.alert API
*/
restoreOriginalAlert() {
_reactNative.Alert.alert = this.originalAlert;
}
}
var _default = exports.default = DialogListener;
//# sourceMappingURL=DialogListener.js.map
;