package com.example.prettywallpapers.room;

import android.os.Handler;
import android.os.Looper;

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

public class MyTool {
    private static ExecutorService executorService;
    private static Handler mainHandler;//处理线程通信和消息分发的重要工具

    public static void runIO(Runnable task) {
        getExecutorService().execute(task);
    }

    private static ExecutorService getExecutorService() {
        if (executorService == null) {
            executorService = Executors.newSingleThreadExecutor();
        }
        return executorService;
    }
    private static Handler getMainHandler() {
        if (mainHandler == null) {
            mainHandler = new Handler(Looper.getMainLooper());
        }
        return mainHandler;
    }

    public static void insertRoom(List<PrettyPaperEntity> list) {
        getExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                for (PrettyPaperEntity entityApp : list) {
                    PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().insertLikeData(entityApp);
                }

            }
        });
    }
/*   public static void queryRoom(boolean islike, QueryPaperListener listener) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PrettyPaperEntity> list = PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().queryData(islike);
                *//*post() 方法将一个 Runnable 对象添加到主线程的消息队列中，以便在主线程上执行。
                （）->{}是匿名发送一个任务到主线程中*//*
                getMainHandler().post(() -> {   //getMainHandler()是获取主线程的handler方法，handler是处理线程间通信的机制，允许将任务发送到其他线程的消息队列中
                    listener.onQueryResult(list);//在任务执行时，会调用listener.onQueryResult(appList)方法
                });
            }
        });
    }*/


}
