Home

Awesome

SecretSauce

Maven Central Maven Central master: Build Status develop: Build Status

Collection of utility classes that are common for multiple android projects.

Contains:

initialization

Some feature are more convenient to use if you declare some parameters up front.

SecretSauceSettings.set(debug = BuildConfig.DEBUG,
                containerViewId = R.id.container,
                bindingViewModelId = BR.viewModel,
                viewModelFactoryProvider = { viewModelFactory })

Settings any of those values is optional. If you don't use given feature you can skip given parameter. However if you attempt to use given feature later you will either have to pass optional param to function or the exception will be thrown. debug is used by logs and Toasts containerViewId used by showFragment methods bindingViewModelId and viewModelFactoryProvider use by viewModel extension functions.

Do you create new instances of objects inside your fragments/activities by hand, because they require scoped context? Dagger Android support can help you!

Dagger works best if it can create all the injected objects

Lets say that we have successfully configured Dagger and we inject our non-Android dependencies.
Our activity may look something like that:

class MyActivity : AppCompatActivity() {

    @Inject
    lateinit var dependencyA: DependencyA
    @Inject
    lateinit var dependencyB: DependencyB
    val colorMixer by lazy { ColorMixer(this, dependencyA, dependencyB) }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityContextDependencies2Binding = bindContentView(R.layout.myactivity)
        DaggerUtil.inject(this)
        binding.textView.setBackgroundColor(colorMixer.color)
    }
}

It is not that bad, but there are several problems:

You may ask - what if I use application context everywhere instead? In some cases that may work but I would discourage it. You will get exception if you try to get layout inflater, and you may get wrong answer if you ask for color (if you use themed activities).

So what can we do? We can use dagger.android

Do you create new instances of objects inside your fragments/activities by hand, because they require scoped context? Dagger Android support can help you!

Dagger works best if it can create all the injected objects

Lets say that we have successfully configured Dagger and we inject our non-Android dependencies.
Our activity may look something like that:

class MyActivity : AppCompatActivity() {

    @Inject
    lateinit var dependencyA: DependencyA
    @Inject
    lateinit var dependencyB: DependencyB
    val colorMixer by lazy { ColorMixer(this, dependencyA, dependencyB) }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityContextDependencies2Binding = bindContentView(R.layout.myactivity)
        DaggerUtil.inject(this)
        binding.textView.setBackgroundColor(colorMixer.color)
    }
}

It is not that bad, but there are several problems:

You may ask - what if I use application context everywhere instead? In some cases that may work but I would discourage it. You will get exception if you try to get layout inflater, and you may get wrong answer if you ask for color (if you use themed activities).

So what can we do? We can use dagger.android