title: RxJava show Loading when http request takes more than given amount of time
body: <p><strong>Problem:</strong></p>
  <p>Recently a design team on a project I'm working on came up with an idea to show loading states only when http request takes more than two seconds to complete.
  I wanted to go for a reusable piece of code and I created an extension.</p>
  <p><strong>My approach:</strong></p>
  <p>Let's say we have 2 seconds delay time after which loading should appear on the screen.</p>
  <ul>
  <li>In case I get the successful response quicker than 2 seconds, takeUntil completes when condition is satisfied. Loading is not emitted.</li>
  <li>In case successful response comes after 2 seconds, loading is emitted and after that takeUntil completes when condition is satisfied.</li>
  <li>In case we get an exception our Flowable just dies, so we either get loading and error or just an error.</li>
  </ul>
  <pre><code>fun &lt;T : Any&gt; Single&lt;T&gt;.mergeWithDelayedEvent(
      delayedSubject: T,
      delayTime: Long,
      timeUnit: TimeUnit,
      takeUntil: T,
      schedulersProvider: SchedulersProvider
  ): Flowable&lt;T&gt; =
      mergeWith(getSingleDelayed(delayedSubject, delayTime, timeUnit, schedulersProvider))
          .takeUntil {
              it == takeUntil
          }
  
  private fun &lt;T : Any&gt; getSingleDelayed(
      delayedSubject: T,
      delayTime: Long,
      timeUnit: TimeUnit,
      schedulersProvider: SchedulersProvider
  ): Single&lt;T&gt; =
      Single.create&lt;T&gt; {
          it.onSuccess(delayedSubject)
      }
          .subscribeOn(schedulersProvider.io())
          .delay(delayTime, timeUnit)
          .observeOn(schedulersProvider.main())
  </code></pre>
  <p><strong>Can I do it in a better/cleaner way?</strong></p>
  <p>I have a strong feeling that above code is too complicated and I'd really appreciate your insight.</p>