package com.motse.fengshui_compass.ui.home;

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

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.motse.fengshui_compass.common.RotateThread;

public class HomeViewModel extends ViewModel {

    private final MutableLiveData<Float> rotation;
    RotateThread rotateThread;
    private Handler handler;

    public HomeViewModel() {
        handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                Float _rotation = msg.getData().getFloat("rotation");
                rotation.setValue(_rotation);
            }
        };
        rotation = new MutableLiveData<>();
        rotation.setValue((float) 0);
//        rotateThread = new RotateThread(handler);
//        rotateThread.start();
    }

    public void initThread() {
        rotateThread = new RotateThread(handler);
        rotateThread.start();
    }

    public void destroyThread() {
        rotateThread.interrupt();
        rotateThread = null;
    }

    public LiveData<Float> getRotation() {
        return rotation;
    }
}