From fa4bcb2d964a5cf3dfc2f158609b453095890b9c Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 18 Jan 2020 12:16:44 +0530 Subject: [PATCH] WIP Signed-off-by: Harsh Shandilya --- content/posts/dagger-the-easy-way--part-1.md | 37 +++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/content/posts/dagger-the-easy-way--part-1.md b/content/posts/dagger-the-easy-way--part-1.md index c7f004e..6341c82 100644 --- a/content/posts/dagger-the-easy-way--part-1.md +++ b/content/posts/dagger-the-easy-way--part-1.md @@ -20,7 +20,7 @@ I know what I said, but this is just necessary. Bear with me. ### `Component` -A Component defines an interface that Dagger constructs to know the entry points where dependencies can be injected. It can also hold the component factory that instructs Dagger how to construct said component. A Component *also* holds the list of modules. +A Component defines an interface that Dagger constructs to know the entry points where dependencies can be injected. It can also hold the component factory that instructs Dagger how to construct said component. A Component _also_ holds the list of modules. ### `Module` @@ -36,3 +36,38 @@ Switch to the `part-1` branch, which has a bit more in terms of commit history a ## Setting up the things +Remember `Component` and `Module`? It's gonna come in handy here. + +Start off with [adding the Dagger dependencies](https://github.com/msfjarvis/dagger-the-easy-way/commit/f86208b89cee2c05becd4341e1b209dc2479aa2f), then add an **empty** Component and Module, which we did [here](https://github.com/msfjarvis/dagger-the-easy-way/commit/f1604adb4e99f342b213cefa9fada21efb6f49a2). + +```kotlin +@Singleton +@Component(modules = [AppModule::class]) +interface AppComponent { + +} + +@Module +object AppModule { + +} +``` + +What we're doing here, is marking our `AppComponent` as a 'singleton', to indicate that it needs to only be constructed _once_ for the lifecycle of the application. We're also annotating it with `@Component` for obvious reasons, and adding our module to it to indicate that they're going together. This is empty right now but that's going to change soon. + +If you check `MainActivity`, you'll notice that we're using [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences.html). To demonstrate the use of Dagger, I'm going to replace that usage with one provided through Dagger. For that to happen though, Dagger needs to know how to create a `SharedPreferences`. Let's get that going! + +```kotlin +@Module +object AppModule { + + @Provides + @Reusable + fun provideSharedPrefs(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) +} +``` + +Breaking this down: `Provides` tells Dagger to bind the return value of the method to the object graph, and `Reusable` tells Dagger that you want to use one copy of this as many times as you can, but it's _okay_ to create a new instance if that's not possible. + +If you pay attention to the [commit](https://github.com/msfjarvis/dagger-the-easy-way/commit/f1a60ffaf6f07f8654bde27fbd65bef08c248f4e) for this step, you'll see that we're also adding preferences to the `AppComponent`. This is just one of the many different patterns one can use with Dagger, and I'm using it just for the simplicity. We'll look into another pattern for the next part! +