package com.suixingpay.serialsdk.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Toast统一管理类
 */
public class ToastUtil {

    private static Context appContext;

    /**
     * 初始化Toast工具类
     * 在Application中调用一次即可
     *
     * @param context
     */

    public static void init(Context context) {
        appContext = context;
    }

    public static void showToast(String msg) {
        // 模拟 CoroutineScope(Dispatchers.Default)
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        executorService.execute(() -> {
            // 模拟 withContext(Dispatchers.Main)
            new Handler(Looper.getMainLooper()).post(() -> {
                // 在主线程中显示Toast
                Toast.makeText(appContext, msg, Toast.LENGTH_LONG).show();
            });
        });

        // 关闭ExecutorService
        executorService.shutdown();
    }
}