inline fun <T> LiveData<Event<T>>.observeEvent(owner: LifecycleOwner, crossinline onEventUnhandledContent: (T) -> Unit) {
observe(owner, Observer { it?.getContentIfNotHandled()?.let(onEventUnhandledContent) })
}
🏴☠️
I hereby claim:
- I am rachitmishra on github.
- I am rachitmishra (https://keybase.io/rachitmishra) on keybase.
- I have a public key whose fingerprint is 1EED 1186 5734 CC4B 3615 E8BD 6DEA 2BC5 9B4B BA4F
To claim this, I am signing this object:
Inversion of Control Containers and the Dependency Injection pattern
https://www.martinfowler.com/articles/injection.html
A beginners guide to Dependency Injection
http://www.theserverside.com/news/1321158/A-beginners-guide-to-Dependency-Injection
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
/** | |
* Using `@Binds` to provide concrete implementations | |
*/ | |
@Module | |
abstract class ComputerBinding(private val memorySize: Int, | |
private val vMemorySize: Int) { | |
@Binds | |
abstract fun providesMotherboard(gigabyte: Gigabyte): Motherboard | |
} |
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
/** | |
* Activity handling it's injection | |
*/ | |
class MyActivity { | |
@Inject | |
lateinit var computer: Computer // Requesting the dependency | |
override fun onCreate(savedInstanceState: Bundle?) { |
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
/** | |
* Declaring application with `ComputerComponent` instance | |
*/ | |
class MyApplication: Application() { | |
lateinit var computerComponent: ComputerComponent // Declaring our component instance | |
@Override | |
public void onCreate() { |
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
/** | |
* Building `ComputerComponent` and getting a dependency | |
*/ | |
class ComputerApp { | |
@Inject lateinit var computer: Computer | |
} | |
fun main(args: Array<String>) { | |
val computerComponent = DaggerComputerComponent |
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
/** | |
* An activity implementing `HasSupportFragmentInjector` and with an instance of `DispatchingAndroidInjector<Fragment>` | |
* for fragment injection. | |
*/ | |
class HomeActivity : HasSupportFragmentInjector { | |
@Inject | |
lateinit var mAndroidInjector: DispatchingAndroidInjector<Fragment> | |
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
/** | |
* Fragment with dependency `ConnectionUtils` | |
*/ | |
class HomeFragment: Fragment() { | |
@Inject | |
lateinit var connectionUtils: ConnectionUtils // Requesting our dependency | |
override fun onAttach(context: Context?) { |
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
/** | |
* HomeViewModel with `@Inject`able constructor | |
*/ | |
class HomeViewModel @Inject constructor(private val resources: Resources, | |
private val preferenceUtils: PreferenceUtils, | |
private val defineRepository: DefineRepository) {} |
NewerOlder