import { AdaptyIOSPresentationStyle, AdaptyUiDialogActionType, AdaptyUiDialogConfig, AdaptyUiView, CreateFlowViewParamsInput, DEFAULT_FLOW_EVENT_HANDLERS, FlowEventHandlers, } from './types'; import { FlowViewEmitter } from './flow-view-emitter'; import { AdaptyFlow } from '@/types'; import { LogContext, LogScope } from '@/logger'; import { coderFactory } from '@/coders/factory'; import { MethodName } from '@/types/bridge'; import { $bridge } from '@/bridge'; import { AdaptyError } from '@/adapty-error'; import { AdaptyType } from '@/coders/parse'; import { Req } from '@/types/schema'; export const DEFAULT_PARAMS: CreateFlowViewParamsInput = { prefetchProducts: true, loadTimeoutMs: 5000, android: { enableSafeArea: true, }, }; /** * Provides methods to control created flow view * @public */ export class FlowViewController { /** * Intended way to create a FlowViewController instance. * It prepares a native controller to be presented * and creates reference between native controller and JS instance * @internal */ static async create( flow: AdaptyFlow, params: CreateFlowViewParamsInput, ): Promise { const ctx = new LogContext(); const log = ctx.call({ methodName: 'createFlowView' }); log.start(() => ({ flow, params })); const view = new FlowViewController(); const flowCoder = coderFactory.createFlowCoder(); const paramsCoder = coderFactory.createUiCreateFlowViewParamsCoder(); const methodKey = 'adapty_ui_create_flow_view'; // Set default values for required parameters const paramsWithDefaults: CreateFlowViewParamsInput = { ...DEFAULT_PARAMS, ...params, }; const data: Req['AdaptyUICreateFlowView.Request'] = { method: methodKey, flow: flowCoder.encode(flow), ...paramsCoder.encode(paramsWithDefaults), }; const body = JSON.stringify(data); const result = await view.handle( methodKey, body, 'AdaptyUiView', ctx, log, ); view.id = result.id; view.locale = result.locale; view.viewEmitter = new FlowViewEmitter(result.id); view.setEventHandlers(DEFAULT_FLOW_EVENT_HANDLERS); return view; } private id: string | null; // reference to a native view. UUID private viewEmitter: FlowViewEmitter | null = null; /** * The localization the view was actually built with. * * @remarks * May differ from the requested localization if it is not available in the flow. */ public locale?: string; /** * Since constructors in JS cannot be async, it is not * preferred to create ViewControllers in direct way. * Consider using @link{FlowViewController.create} instead * * @remarks * Creating FlowViewController this way does not let you * to make native create request and set _id. * It is intended to avoid usage * * @internal */ private constructor() { this.id = null; } private async handle( method: MethodName, params: string, resultType: AdaptyType, ctx: LogContext, log: LogScope, ): Promise { try { const result = await $bridge.request(method, params, resultType, ctx); log.success(() => result as Record); return result as T; } catch (error) { /* * Success because error was handled validly * It is a developer task to define which errors must be logged */ log.success(() => ({ error })); throw error; } } /** * Presents a flow view as a modal * * @param {Object} options - Presentation options * @param {AdaptyIOSPresentationStyle} [options.iosPresentationStyle] - iOS presentation style. * Available options: 'full_screen' (default) or 'page_sheet'. * Only affects iOS platform. * * @remarks * Calling `present` upon already visible flow view * would result in an error * * @throws {AdaptyError} */ public async present( options: { iosPresentationStyle?: AdaptyIOSPresentationStyle } = {}, ): Promise { const ctx = new LogContext(); const log = ctx.call({ methodName: 'present' }); log.start(() => ({ _id: this.id, iosPresentationStyle: options.iosPresentationStyle, })); if (this.id === null) { log.failed(() => ({ error: 'no _id' })); throw this.errNoViewReference(); } const methodKey = 'adapty_ui_present_flow_view'; const body = JSON.stringify({ method: methodKey, id: this.id, ios_presentation_style: options.iosPresentationStyle ?? 'full_screen', } satisfies Req['AdaptyUIPresentFlowView.Request']); const result = await this.handle(methodKey, body, 'Void', ctx, log); return result; } /** * Dismisses a flow view * * @remarks * By default the native view is destroyed along with the flow: the controller cannot be * presented afterwards, its event handlers are cleared. * * Pass `destroy: false` to keep the view alive instead. It can be presented again with * {@link FlowViewController.present}, resuming on the screen the user left and with the * state the flow had built up, and its event handlers stay active. Such a view is held * until it is released with {@link FlowViewController.destroy}. * * @param {Object} options - Dismiss options * @param {boolean} [options.destroy] - Destroy the native view as well. Defaults to `true`. * * @throws {AdaptyError} * * @example * ```ts * // close the flow and destroy its native view * await view.dismiss(); * * // close the flow, keeping it ready to be shown again * await view.dismiss({ destroy: false }); * await view.present(); // resumes where the user left off * await view.dismiss({ destroy: true }); // destroys the view for good * ``` */ public async dismiss(options: { destroy?: boolean } = {}): Promise { const ctx = new LogContext(); const log = ctx.call({ methodName: 'dismiss' }); const destroy = options.destroy ?? true; log.start(() => ({ _id: this.id, destroy })); if (this.id === null) { log.failed(() => ({ error: 'no id' })); throw this.errNoViewReference(); } const methodKey = 'adapty_ui_dismiss_flow_view'; const body = JSON.stringify({ method: methodKey, id: this.id, destroy, } satisfies Req['AdaptyUIDismissFlowView.Request']); await this.handle(methodKey, body, 'Void', ctx, log); if (destroy && this.viewEmitter) { this.viewEmitter.removeAllListeners(); } } /** * Releases a native flow view that was kept alive by `dismiss({ destroy: false })`. * * @remarks * By default {@link FlowViewController.dismiss} releases the view on its own, so there * is no need to call `destroy()` after it. * * The controller cannot be presented afterwards. Event handlers are cleared. * * @throws {AdaptyError} if the view reference is invalid, or if the native view was * already released. * * @example * ```ts * const view = await createFlowView(flow); * await view.present(); * await view.dismiss({ destroy: false }); * // ... later * await view.destroy(); * ``` */ public async destroy(): Promise { const ctx = new LogContext(); const log = ctx.call({ methodName: 'destroy' }); log.start(() => ({ _id: this.id })); if (this.id === null) { log.failed(() => ({ error: 'no id' })); throw this.errNoViewReference(); } const methodKey = 'adapty_ui_destroy_flow_view'; const body = JSON.stringify({ method: methodKey, id: this.id, } satisfies Req['AdaptyUIDestroyFlowView.Request']); await this.handle(methodKey, body, 'Void', ctx, log); if (this.viewEmitter) { this.viewEmitter.removeAllListeners(); } } /** * Presents an alert dialog * * @param {AdaptyUiDialogConfig} config - A config for showing the dialog. * * @remarks * Use this method instead of RN alert dialogs when flow view is presented. * On Android, built-in RN alerts appear behind the flow view, making them invisible to users. * This method ensures proper dialog presentation above the flow view on all platforms. * * If you provide two actions in the config, be sure `primaryAction` cancels the operation * and leaves things unchanged. * * @returns {Promise} A Promise that resolves to the {@link AdaptyUiDialogActionType} object * * @throws {AdaptyError} */ public async showDialog( config: AdaptyUiDialogConfig, ): Promise { const ctx = new LogContext(); const log = ctx.call({ methodName: 'showDialog' }); log.start(() => ({ _id: this.id })); if (this.id === null) { log.failed(() => ({ error: 'no id' })); throw this.errNoViewReference(); } const coder = coderFactory.createUiDialogConfigCoder(); const methodKey = 'adapty_ui_show_dialog'; const body = JSON.stringify({ method: methodKey, id: this.id, configuration: coder.encode(config), } satisfies Req['AdaptyUIShowDialog.Request']); return await this.handle( methodKey, body, 'Void', ctx, log, ); } private onRequestClose = async (): Promise => { try { await this.dismiss(); } catch (error) { // Log error but don't re-throw to avoid breaking event handling const ctx = new LogContext(); const log = ctx.call({ methodName: 'onRequestClose' }); log.failed(() => ({ error, message: 'Failed to dismiss flow view' })); } }; /** * Sets event handlers for flow view events * * @see {@link https://adapty.io/docs/react-native-handling-events-1 | [DOC] Handling View Events} * * @remarks * Each event type can have only one handler — new handlers replace existing ones. * Default handlers are set during view creation: {@link DEFAULT_FLOW_EVENT_HANDLERS} * - `onCloseButtonPress` - closes the view (returns `true`) * - `onAndroidSystemBack` - keeps the view open (returns `false`) * - `onRestoreCompleted` - keeps the view open (returns `false`) * - `onError` - closes the view (returns `true`) * - `onPurchaseCompleted` - keeps the view open (returns `false`) * - `onUrlPress` - opens URL and keeps the view open (returns `false`) * * To retain default behavior in a custom listener, return the same value as the default implementation. * * **Important**: Calling this method multiple times will override only the handlers you provide, * keeping previously set handlers intact. * * @param {Partial} [eventHandlers] - set of event handling callbacks * @returns {() => void} unsubscribe - function to unsubscribe all listeners */ public setEventHandlers( eventHandlers: Partial = {}, ): () => void { const ctx = new LogContext(); const log = ctx.call({ methodName: 'setEventHandlers' }); log.start(() => ({ _id: this.id })); if (this.id === null) { throw this.errNoViewReference(); } // Create viewEmitter on first call if (!this.viewEmitter) { this.viewEmitter = new FlowViewEmitter(this.id); } // Register only provided handlers (they will replace existing ones for same events) Object.keys(eventHandlers).forEach(eventStr => { const event = eventStr as keyof FlowEventHandlers; if (!eventHandlers.hasOwnProperty(event)) { return; } const handler = eventHandlers[ event ] as FlowEventHandlers[keyof FlowEventHandlers]; this.viewEmitter!.addListener(event, handler, this.onRequestClose); }); return () => this.viewEmitter?.removeAllListeners(); } private errNoViewReference(): AdaptyError { throw new Error('View reference not found'); } }