Skip to content

Instantly share code, notes, and snippets.

View rachitmishra's full-sized avatar
🏴‍☠️

Rachit Mishra rachitmishra

🏴‍☠️
View GitHub Profile
@rachitmishra
rachitmishra / LiveData.md
Last active December 27, 2018 09:26
Extensions

Event Observer

inline fun <T> LiveData<Event<T>>.observeEvent(owner: LifecycleOwner, crossinline onEventUnhandledContent: (T) -> Unit) {
    observe(owner, Observer { it?.getContentIfNotHandled()?.let(onEventUnhandledContent) })
}

Keybase proof

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:

@rachitmishra
rachitmishra / dagger-reference.md
Last active February 21, 2020 14:54
dagger-reference
@rachitmishra
rachitmishra / ComputerBinding.kt
Last active November 7, 2017 19:12
dagger-binds
/**
* 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
}
/**
* Activity handling it's injection
*/
class MyActivity {
@Inject
lateinit var computer: Computer // Requesting the dependency
override fun onCreate(savedInstanceState: Bundle?) {
@rachitmishra
rachitmishra / MyApplication.kt
Last active November 7, 2017 19:13
dagger-android-activity-basic
/**
* Declaring application with `ComputerComponent` instance
*/
class MyApplication: Application() {
lateinit var computerComponent: ComputerComponent // Declaring our component instance
@Override
public void onCreate() {
@rachitmishra
rachitmishra / ComputerApp.kt
Last active November 7, 2017 19:14
dagger-app
/**
* Building `ComputerComponent` and getting a dependency
*/
class ComputerApp {
@Inject lateinit var computer: Computer
}
fun main(args: Array<String>) {
val computerComponent = DaggerComputerComponent
@rachitmishra
rachitmishra / HomeActivity.kt
Last active November 7, 2017 19:15
dagger-android-activity-fragment
/**
* An activity implementing `HasSupportFragmentInjector` and with an instance of `DispatchingAndroidInjector<Fragment>`
* for fragment injection.
*/
class HomeActivity : HasSupportFragmentInjector {
@Inject
lateinit var mAndroidInjector: DispatchingAndroidInjector<Fragment>
@rachitmishra
rachitmishra / HomeFragment.kt
Last active November 7, 2017 19:17
dagger-android-fragment
/**
* Fragment with dependency `ConnectionUtils`
*/
class HomeFragment: Fragment() {
@Inject
lateinit var connectionUtils: ConnectionUtils // Requesting our dependency
override fun onAttach(context: Context?) {
@rachitmishra
rachitmishra / HomeViewModel.kt
Last active November 7, 2017 19:17
dagger-module
/**
* HomeViewModel with `@Inject`able constructor
*/
class HomeViewModel @Inject constructor(private val resources: Resources,
private val preferenceUtils: PreferenceUtils,
private val defineRepository: DefineRepository) {}