package com.brickmodule import com.facebook.react.bridge.ReactContext /** * Minimal interface that all Brick modules must implement Contains only essential properties for * module identification */ interface BrickModuleBase { /** The name of the module (required for registration) */ val moduleName: String /** * Returns the constants exposed by this module * Override this method to provide module-specific constants * @return Map of constant name to value */ fun getConstants(): Map = emptyMap() } /** * Abstract base class for Brick modules that provides internal implementation details Module * implementations should extend this class instead of implementing BrickModuleBase directly */ abstract class BrickModuleSpec(private val reactContext: ReactContext) : BrickModuleBase { /** * Get the ReactContext instance * @return ReactContext for this module */ protected fun getReactContext(): ReactContext = reactContext /** * Send an event to JavaScript This method handles event validation and routing through the * BrickHost * * @param eventName The name of the event (without module prefix) * @param data The event data to send */ protected fun sendEvent(eventName: String, data: Any?) { val context = getReactContext() // Format event name with module prefix val fullEventName = "${moduleName}_$eventName" // Get the BrickHost from ReactContext val activity = context.currentActivity if (activity !is BrickModuleRegistrar) { println( "❌ $moduleName: Current activity does not implement BrickHost, cannot send event" ) return } // Send event via BrickHost's moduleRegistry try { activity.getModuleRegistry().sendEvent(context, fullEventName, data) } catch (e: Exception) { println("❌ $moduleName: Failed to send event $fullEventName: ${e.message}") } } } /** Error types for Brick modules */ open class BrickModuleError(message: String, val errorCode: String) : Exception(message) { class TypeMismatch(message: String) : BrickModuleError("Type mismatch: $message", "TYPE_ERROR") class ExecutionError(message: String) : BrickModuleError("Execution error: $message", "EXECUTION_ERROR") class InvalidDefinition(message: String) : BrickModuleError("Invalid definition: $message", "DEFINITION_ERROR") class MethodNotFound(message: String) : BrickModuleError("Method not found: $message", "METHOD_NOT_FOUND") class ModuleNotFound(message: String) : BrickModuleError("Module not found: $message", "MODULE_NOT_FOUND") }