How do you implement DaggerService

I've covered the basics as well as the class, but being new to dagger (or even dagger 2) I have no idea how I can use this

Here is the dagger intent service: https://google.github.io/dagger/api/latest/dagger/android/DaggerIntentService.html

I understand android intent service and the basics to implement, but I cannot find information on DaggerIntentService (and I am also trying to find information for DaggerService)

My goal is to create it using TDD, but I just need to understand the workflow to implement a dagger based service

Thanks, Kelly

+3


source to share


1 answer


This does not answer the problem DaggerIntentService

. dagger.android

packages do more or less the same thing that you can do manually by installing the appropriate component and module for the intent service. Therefore, you can try the following approach:

ServiceComponent

@Subcomponent(modules = ServiceModule.class)
public interface ServiceComponent{

    @Subcomponent.Builder
    public interface Builder {
        Builder withServiceModule(ServiceModule serviceModule);
        ServiceComponent build();
    }

    void inject(MyService myService);
}

      

ServiceModule

@Module
public class ServiceModule{

    private MyService myService;

    public ServiceModule(MyService myService){
        this.myService = myService;
    }

    @Provides public MyService provideServiceContext(){
        return myService;
    }

    @Provides public SomeRepository provideSomeRepository(){
        return new SomeRepository();
    }
}

      

Bearing in mind that you have a root dagger component like ApplicationComponent

that you create in your application onCreate()

, you will need an additional public method in your application class.

ApplicationComponent

@Component(modules = ApplicationModule.class)
public interface ApplicationComponent{
    ServiceComponent.Builder serviceBuilder();
}

      

ApplicationModule



@Module(subcomponents = ServiceComponent.class)
public class ApplicationModule{

    public ApplicationModule(MyApplication myApplication){
        this.myApplication = myApplication;
    }

    @Provides
    public MyApplication providesMyApplication(){
        return myApplication;
    }
}

      

MyApplication

public class MyApplication extends Application{

    ApplicationComponent applicationComponent;

    @Override
    public void onCreate(){
        super.onCreate();
        applicationComponent = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(this))
            .build();
    }

    public ServiceComponent getServiceInjector(MyService myService){
        return applicationComponent.serviceBuilder().withServiceModule(new ServiceModule(myService)).build();
}

      

Finally yours MyService

:)

MyService

public class MyService extends IntentService{

    @Inject MyApplication application;
    @Inject SomeRepository someRepository;

    public onCreate(){
        ((MyApplication)getApplicationContext()).getServiceInjector(this).inject();
    }

    public void onHandleIntent(Intent intent){
        //todo extract your data here
    }

      

It may look more complicated at first, but if you already have a dagger structure customization, then these are just two or three additional classes.

I hope you find it helpful. Greetings.

+5


source







All Articles