package com.azerion.bluestack.react.utils;

// import android.os.Handler;
import android.os.Looper;
import com.facebook.react.bridge.UiThreadUtil;

/**
 * Utility class to run code on the main thread.
 */
public class MainThreadDispatcher {
    private static final String TAG = "MainThreadDispatcher";
    // private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());

    public static void runOnUiThread(Runnable runnable) {
        boolean isUiThread = Looper.myLooper() == Looper.getMainLooper();
        BlueStackLogger.debug(TAG, "isUiThread: " + isUiThread);
        if (isUiThread) {
            BlueStackLogger.debug(TAG, "Already on main thread");
            runnable.run(); // Already on main thread
        } else {
            // MAIN_HANDLER.post(runnable); // Post to main thread
            BlueStackLogger.debug(TAG, "Posting to main thread");
            UiThreadUtil.runOnUiThread(runnable);
        }
    }
}

