-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DaggerAndroidWorker (demo inject to worker using dagger-android)
- Loading branch information
1 parent
ca68feb
commit 0468632
Showing
7 changed files
with
83 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/sample/daggerworkmanagersample/DaggerAndroidWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.sample.daggerworkmanagersample | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import dagger.android.ContributesAndroidInjector | ||
import dagger.android.HasAndroidInjector | ||
import javax.inject.Inject | ||
|
||
|
||
object ContextInjection { | ||
@JvmStatic | ||
fun inject(to: Any, with: Context) { | ||
(with.applicationContext as HasAndroidInjector).androidInjector().inject(to) | ||
} | ||
} | ||
|
||
class DaggerAndroidWorker( | ||
context: Context, | ||
params: WorkerParameters | ||
) : Worker(context, params) { | ||
|
||
@Inject | ||
lateinit var foo: Foo | ||
|
||
init { | ||
ContextInjection.inject(to = this, with = context) | ||
} | ||
|
||
override fun doWork(): Result { | ||
Log.d("DaggerAndroidWorker", "Injected foo: $foo") | ||
return Result.success() | ||
} | ||
|
||
|
||
@dagger.Module | ||
interface Module { | ||
@ContributesAndroidInjector | ||
fun worker(): DaggerAndroidWorker | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters