Skip to content

refactor: avoid tight coupling by using dependency injection#13

Open
alexgerardojacinto wants to merge 1 commit intofeat/RMET-4099/camera-unificationfrom
feat/RMET-5076/refactor-bridge-android
Open

refactor: avoid tight coupling by using dependency injection#13
alexgerardojacinto wants to merge 1 commit intofeat/RMET-4099/camera-unificationfrom
feat/RMET-5076/refactor-bridge-android

Conversation

@alexgerardojacinto
Copy link
Collaborator

Problem

  • Cyclic dependency between CameraPlugin and flow classes (IonCameraFlow and LegacyCameraFlow)
  • Flows were receiving CameraPlugin instance via constructor and calling back to plugin methods
  • This created tight coupling: CameraPlugin → Flows → CameraPlugin
  • Made testing difficult and violated dependency inversion principle

Solution: Constructor Dependency Injection

  1. Created PermissionHelper class
  • Encapsulates permission-related operations without holding plugin reference
  • Uses lambda/function references instead of plugin instance
  • Accepts 4 function parameters: isPermissionDeclared, getPermissionState, requestPermissionForAlias, requestPermissionForAliases
  • Completely eliminates type dependency on CameraPlugin
  1. Refactored IonCameraFlow (Kotlin)
  • Changed constructor to accept explicit dependencies: Context, Activity, Bridge, appId, PermissionHelper
  • Replaced all plugin.* references with injected dependencies
  • No longer holds reference to CameraPlugin
  1. Refactored LegacyCameraFlow (Java)
  • Changed constructor to accept explicit dependencies: Context, AppCompatActivity, Bridge, appId, PermissionHelper, ActivityStarter
  • Created ActivityStarter functional interface for activity result launching (avoids naming conflict with AndroidX's ActivityResultLauncher)
  • Replaced all plugin.* references with injected dependencies
  • No longer holds reference to CameraPlugin
  1. Updated CameraPlugin
  • Modified load() method to instantiate flows with explicit dependencies
  • Creates PermissionHelper with lambda function references
  • Passes concrete dependencies (context, activity, bridge, appId) to flow constructors
  • Removed unnecessary wrapper methods (requestLegacyPermissionForAlias, requestLegacyPermissionForAliases)

Benefits

  • ✅ Eliminates cyclic dependency - flows only depend on interfaces/primitives, not plugin
  • ✅ Improves testability - flows can be unit tested with mock dependencies
  • ✅ Better separation of concerns - explicit, clear dependencies via constructor
  • ✅ Follows SOLID principles - dependency inversion, interface segregation
  • ✅ No behavioral changes - purely structural refactoring, maintains backward compatibility

Files Changed

  • New: PermissionHelper.kt - Permission operations wrapper
  • Modified: IonCameraFlow.kt - Constructor injection refactor
  • Modified: LegacyCameraFlow.java - Constructor injection refactor, added ActivityStarter interface
  • Modified: CameraPlugin.kt - Updated flow instantiation with dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant