Skip to content

Instantly share code, notes, and snippets.

@zawadz88
zawadz88 / CompletableOperatorsTest.kt
Last active July 9, 2024 22:35
Tests for disposing Disposables
package test
import io.reactivex.Completable
import io.reactivex.observers.TestObserver
import io.reactivex.schedulers.Schedulers
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.Executors
package com.stepstone.test.rule
import android.app.Activity
import android.content.Intent
import androidx.test.core.app.ActivityScenario
import org.junit.rules.ExternalResource
class LazyActivityScenarioRule<A : Activity> : ExternalResource {
constructor(launchActivity: Boolean, startActivityIntentSupplier: () -> Intent) {
class OfferFragment : DaggerFragment() {
@Inject lateinit var sharedPreferences: SharedPreferences,
@Inject lateinit var textProducer: TextProducer,
@Inject lateinit var someDep: SomeDep
// Fragment body
}
class OfferFragment
@Inject constructor(
private val sharedPreferences: SharedPreferences,
private val textProducer: TextProducer,
private val someDep: SomeDep
) : Fragment() {
// Fragment body
}
@zawadz88
zawadz88 / activity_main.xml
Created April 12, 2019 16:02
Before changes
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
class OfferFragment
@Inject constructor(
private val sharedPreferences: SharedPreferences,
private val textProducer: TextProducer
) : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("Constructor sharedPreferences: $sharedPreferences")
}
class InjectingNavHostFragment : NavHostFragment() {
@Inject
protected lateinit var daggerFragmentInjectionFactory: InjectingFragmentFactory
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
class InjectingFragmentFactory
@Inject constructor(
private val creators: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>>
) : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
val fragmentClass = loadFragmentClass(classLoader, className)
val creator = creators[fragmentClass]
?: return createFragmentAsFallback(classLoader, className)
@Module
abstract class FragmentBindingModule {
@Binds
@IntoMap
@FragmentKey(OfferFragment::class)
abstract fun bindMainFragment(mainFragment: OfferFragment): Fragment
@Binds
abstract fun bindFragmentFactory(factory: InjectingFragmentFactory): FragmentFactory