Having the SharedPreferences provided from one dagger2 module, in another dagger2 module would like to use it,
how to do it?
the code below seems not working.
/** the component */
@Singleton
@Component(modules = arrayOf(DataManagerModule::class,
AnotherModule::class))
interface DataManagerComponent {
fun getDataManager() : DataManager
fun getSharedPreferences() : SharedPreferences
}
/** the module 1 */
@Module
class DataManagerModule(@ApplicationContext private val appContext: Context) {
@Singleton
@Provides
@ApplicationContext
fun provideApplicationContext(): Context = appContext
@Singleton
@Provides
fun provideSharedPreferences(): SharedPreferences {
return appContext.getSharedPreferences(appContext.packageName,
Context.MODE_PRIVATE)
}
}
/** the module 2 */
@Module
class AnotherModule(private val config1: String?, private val config2: Int?) {
@Provides
@Singleton
internal fun provideClass2(context: Context): Class2 {
if (config2 == null) {
// how to get the preferences???
// getSharedPreferences().edit().getInt(Constants.Settings, -1)
}
return class2(config1, config2, context)
}
}
since all these artifacts share the same scope, and the component is built using both modules, you should be able to simply add SharedPreferences
as a parameter to provideClass2()
in order to use it in the construction of Class2
, like so:
@Provides
@Singleton
internal fun provideClass2(context: Context, prefs: SharedPreferences): Class2 {
...
}
Since you have already defined how to retrieve SharedPreferences from DataManagerModule class you can simply change
internal fun provideClass2(context: Context): Class2 {
...
}
to
internal fun provideClass2(sharedPreferences: SharedPreferences): Class2 {
...
}
Have another class extend the Application class and declare it to the AndroidManifest like so:
class App: Application() {
lateinit var dataManagerComponent: DataManagerComponent
override fun onCreate() {
super.onCreate()
dataManagerComponent = DaggerDataManagerComponent.builder()
.dataManagerModule(DataManagerModule(this))
.anotherModule(AnotherModule("config1", 123))
.build()
}
}
Modify your component to include an inject function
@Singleton
@Component(modules = arrayOf(DataManagerModule::class,
AnotherModule::class))
interface DataManagerComponent {
fun getDataManager() : DataManager
fun getSharedPreferences() : SharedPreferences
// The activity/fragment you will need the values
fun inject(target: MainActivity)
}
Then inject it to the activity
class MainActivity : AppCompatActivity() {
@Inject lateinit var class2: Class2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(application as App).dataManagerComponent.inject(this)
Log.i("CLASS2", class2.toString())
}
}