package beer.brew.vendingmachine.util;

import android.support.test.espresso.IdlingResource;

import java.util.concurrent.atomic.AtomicInteger;

import timber.log.Timber;

/**
 * Espresso Idling resource that handles waiting for RxJava Observables executions.
 * This class must be used with RxIdlingExecutionHook.
 * Before registering this idling resource you must:
 * 1. Create an instance of RxIdlingExecutionHook by passing an instance of this class.
 * 2. Register RxIdlingExecutionHook with the RxJavaPlugins using registerObservableExecutionHook()
 * 3. Register this idle resource with Espresso using Espresso.registerIdlingResources()
 */
public class RxIdlingResource implements IdlingResource {

    private final AtomicInteger mActiveSubscriptionsCount = new AtomicInteger(0);
    private ResourceCallback mResourceCallback;

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    @Override
    public boolean isIdleNow() {
        return mActiveSubscriptionsCount.get() == 0;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback callback) {
        mResourceCallback = callback;
    }

    public void incrementActiveSubscriptionsCount() {
        int count = mActiveSubscriptionsCount.incrementAndGet();
        Timber.i("Active subscriptions count increased to %d", count);
    }

    public void decrementActiveSubscriptionsCount() {
        int count = mActiveSubscriptionsCount.decrementAndGet();
        Timber.i("Active subscriptions count decreased to %d", count);
        if (isIdleNow()) {
            Timber.i("There is no active subscriptions, transitioning to Idle");
            mResourceCallback.onTransitionToIdle();
        }
    }

}

@@ -85,16 +85,41 @@
 esource;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -167,35 +167,8 @@
 r;%0A%0A
-import timber.log.Timber;%0A%0A
 /**%0A
@@ -699,16 +699,93 @@
 urce %7B%0A%0A
+    public static final String TAG = RxIdlingResource.class.getSimpleName();%0A
     priv
@@ -1366,33 +1366,35 @@
 ();%0A        
-Timber.i(
+Log.i(TAG, 
 %22Active subs
@@ -1414,36 +1414,35 @@
 nt increased to 
-%25d%22,
+%22 +
  count);%0A    %7D%0A%0A
@@ -1568,25 +1568,27 @@
         
-Timber.i(
+Log.i(TAG, 
 %22Active 
@@ -1624,12 +1624,11 @@
  to 
-%25d%22,
+%22 +
  cou
@@ -1675,17 +1675,19 @@
     
-Timber.i(
+Log.i(TAG, 
 %22The

remove SDK -- Timber's reference