package com.maniu.mn_vip_jetpack_all.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.maniu.lib_cache.model.User;
import com.maniu.lib_cache.view_model.LoginActivityViewModel;
import com.maniu.lib_common.base.BaseActivity;
import com.maniu.mn_vip_jetpack_all.R;
import com.maniu.mn_vip_jetpack_all.databinding.ActivityLoginBinding;
import com.maniu.mn_vip_jetpack_all.main.MainActivity;

/**
 *
 *
 * MVVM    Model  View  ViewModel
 * 双向   指的是   xml文件 和ViewModel对象
 * 单向  指的是  ViewModel被xml文件绑定
 */
public class LoginActivity extends BaseActivity {
    LoginActivityViewModel loginActivityViewModel;
    ActivityLoginBinding activityLoginBinding;
    MutableLiveData<User> user;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //将xml进行绑定
        activityLoginBinding  =
                DataBindingUtil.setContentView(this, R.layout.activity_login);
        activityLoginBinding.setLifecycleOwner(this);
        //创建Activity所对应的VIewmodel
        loginActivityViewModel = new LoginActivityViewModel();
        activityLoginBinding.setViewModel(loginActivityViewModel);
        user  = activityLoginBinding.getViewModel().getUser();
        user.observe(LoginActivity.this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                //做验证
                if(user!= null && user.getU_id()!=0){
                    //跳转到首页
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        });
    }

    public void updateData(View view) {
        //登录的请求
        loginActivityViewModel.loginRequest();

    }
}
