Dagger Components
Purpose
Dagger Components define the bridge between dependency providers (modules) and the consumers (activities, classes) in this media player project. They specifically scope and inject instances such as the FSM player, state factories, controllers, and monitors into activities that handle media playback. This approach ensures modularity, testability, and clear lifecycle management of dependencies.
Within the broader Dependency Injection Setup, Dagger Components uniquely focus on *declaring injection targets* and *exposing scoped dependencies* like the `StateFactory` for FSM state creation, which is not covered by modules alone.
Functionality
Dagger Components operate as the configured injection containers that:
Bind modules providing dependencies (
PlayerModuleDefault,FSMModuleReal) to injection targets.Scope the lifetime of injected dependencies using a custom
@ActicityScopeannotation, ensuring singletons per activity lifecycle.Expose specific dependencies to consumers when needed (e.g., providing the
StateFactoryfor tests or internal FSM management).Inject dependencies into concrete activity classes, enabling activities to receive fully constructed objects without manual instantiation or wiring.
Two primary components reflect different configurations for injection targets and modules:
FsmComonent (for DoubleViewTubiPlayerActivity)
Uses
PlayerModuleDefaultfor providing default FSM player and controller instances.Exposes the
StateFactorypublicly, supporting testing or internal FSM state creation.Injects dependencies into
DoubleViewTubiPlayerActivity, enabling this activity to receive the FSM player and related objects.
Key snippet:
@ActicityScope
@Component(modules = PlayerModuleDefault.class)
public interface FsmComonent {
StateFactory getStateFactory();
void inject(DoubleViewTubiPlayerActivity activity);
}
FsmComonentReal (for RealActivity)
Uses
FSMModuleRealmodule for a potentially different or more production-oriented dependency configuration.Injects dependencies into
RealActivity.Does not expose any dependencies publicly, encapsulating internal objects strictly for injection.
Key snippet:
@ActicityScope
@Component(modules = FSMModuleReal.class)
public interface FsmComonentReal {
void inject(RealActivity activity);
}
Integration with Dependency Injection Setup
Dagger Components work hand-in-hand with Dagger Modules and custom scopes:
Modules create and provide dependencies (e.g., FSM player, controllers, monitors).
Components declare where and how those dependencies are injected.
The custom scope annotation (
@ActicityScope) ensures dependencies live as long as the activity.
Components complement modules by:
Defining injection targets (activities).
Enforcing scoping rules at compile time.
Providing a clean API for injection and some dependency exposure (like
StateFactory).
This separation enhances modularity by decoupling *how* dependencies are created (modules) from *where* they are injected (components).
Relationship to Other Subtopics
Dagger Modules: Provide the actual dependency instances; components depend on them.
Custom Scope Annotation: Used by components to manage lifecycle scope.
FSM Playback and UI Controls: Activities injected by components consume FSM players and controllers.
Ad and Cue Point Management: Components inject monitors and retrievers as part of the FSM player dependencies.
By clearly defining injection boundaries, components enable seamless FSM player lifecycle management and UI integration.
Diagram: Dagger Component Injection Flow
flowchart TD
ModuleDefault[PlayerModuleDefault\n(provides FSM Player,\nStateFactory, Controllers)]
ModuleReal[FSMModuleReal\n(provides FSM Player,\nad retrievers, monitors)]
FsmComponent[FsmComonent\n@ActivityScope]
FsmComponentReal[FsmComonentReal\n@ActivityScope]
DoubleViewActivity[DoubleViewTubiPlayerActivity]
RealActivity[RealActivity]
ModuleDefault --> FsmComponent
ModuleReal --> FsmComponentReal
FsmComponent -->|inject| DoubleViewActivity
FsmComponentReal -->|inject| RealActivity
FsmComponent -->|exposes| StateFactory[StateFactory]
style ModuleDefault fill:#d9f0ff,stroke:#blue,stroke-width:1px
style ModuleReal fill:#ffe0e0,stroke:#d00,stroke-width:1px
style FsmComponent fill:#e0ffe0,stroke:#080,stroke-width:1px
style FsmComponentReal fill:#fff0b3,stroke:#aa0,stroke-width:1px
This design ensures activities receive properly scoped FSM player instances and supporting objects, promoting clean separation of concerns in dependency management.