Android Development

Dagger Android Tutorial 4

How to do dagger-android

Guowei Lv

2 minute read

Convert RxJava stream into LiveData stream When we use Rxjava for handling api requests, it will return a RxJava Stream. But in the presentation layer we are using LiveData, this article shows you how to make them work together. The tool we are using here is LiveDataReactiveStreams. It is an adapter from LiveData to ReactiveStream and vice versa. This is how we do it in AuthViewModel. public class AuthViewModel extends ViewModel { private static final String TAG = "AuthViewModel"; private final AuthApi authApi; // 1.

Dagger Android Tutorial 3

How to do dagger-android

Guowei Lv

1 minute read

Inject Retrofit @Singleton The Retrofit instance should be a singleton in the scope of AppComponent. Provide the Retrofit instance in AppModule. @Singleton @Provides static Retrofit provideRetrofitInstance() { return new Retrofit.Builder().baseUrl(Constants.BASE_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()).build(); } Under AuthActivity subcomponent, create a module called AuthModule, in it, provides the AuthApi instance. @Module public class AuthModule { @Provides static AuthApi provideAuthApi(Retrofit retrofit) { return retrofit.create(AuthApi.class); } } public interface AuthApi { @GET("/users/{id}") Flowable<User> getUser(@Path("id") int id); } @ContributesAndroidInjector(modules = {AuthModule.

Dagger Android Tutorial 2

How to do dagger-android

Guowei Lv

3 minute read

In this article we show how to inject into ViewModels. Note that we will not dive too deep into the inner workings of things, since they are extremely complex. If you want to get some level of insight into the topic read this article. But I try to give the clean solution and how you can put it in your head. First a few words about the ViewModel in android MVVM architecture.

Dagger Android Tutorial 1

How to do dagger-android

Guowei Lv

2 minute read

Warning: Google thinks you should avoid dagger-android for new projects. Watch Here But, if you are using it now, here are some notes on how to approach it. This is basically a text version of these videos Application Component Pretty much every app will have an application component, whose scope will be the lifetime of the application. First, create the AppComponent //1. Include this 'AndroidSupportInjectionModule', this is required @Component(modules = {AndroidSupportInjectionModule.

Guowei Lv

5 minute read

In this post we talk about the drawing order in custom view. After super.onDraw() What we have done in the previous episodes is to override the onDraw() method of the View class. A lot of times we do not need to create our own custom view from scratch. We can simply extends an existing view, e.g. ImageView. If we want to draw something on top of the image, we can write that after the super.