package com.bawei.wangkejian0413;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.bawei.adapter.XRecyAdapter;
import com.bawei.api.App;
import com.bawei.bean.DaoBean;
import com.bawei.bean.ShowBean;
import com.bawei.ivew.IView;
import com.bawei.presenter.IPresenter;
import com.bawei.presenter.PresenterImpl;
import com.bawei.util.OkHttpUtil;
import com.bawei.wangkejian0413.greenDao.DaoBeanDao;
import com.bawei.wangkejian0413.greenDao.DaoSession;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView {
IPresenter iPresenter;
@SuppressLint(“HandlerLeak”)
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
XRecyAdapter xRecyAdapter = (XRecyAdapter) msg.obj;
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setAdapter(xRecyAdapter);
}
};
private XRecyclerView xRecyclerView;
private ShowBean bean;
private DaoBean daoBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean networkConnected = OkHttpUtil.getInstance().isNetworkConnected(this);
initView();
if (networkConnected){
Toast.makeText(MainActivity.this, "有网",Toast.LENGTH_SHORT).show();
iPresenter=new PresenterImpl();
iPresenter.onAttachView(this);
iPresenter.onRequestData("http://172.17.8.100/small/commodity/v1/commodityList");
}else{
Toast.makeText(MainActivity.this, "无网",Toast.LENGTH_SHORT).show();
isDao();
}
}
private void isDao() {
DaoSession daoSession = App.daoSession;
DaoBeanDao daoBeanDao = daoSession.getDaoBeanDao();
List<DaoBean> daoBeans = daoBeanDao.loadAll();
for (int i = 0; i <daoBeans.size() ; i++) {
daoBean=daoBeans.get(i);
Log.e("Three", daoBean +" ONE");
final XRecyAdapter xRecyAdapter = new XRecyAdapter(this,null,daoBean);
Message message = new Message();
message.obj=xRecyAdapter;
handler.sendMessage(message);
}
/*LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
xRecyclerView.setLayoutManager(linearLayoutManager);*/
}
private void initView() {
xRecyclerView = findViewById(R.id.xreview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
xRecyclerView.setLayoutManager(linearLayoutManager);
}
@Override
public void onShowData(Object object) {
bean = (ShowBean) object;
XRecyAdapter xRecyAdapter = new XRecyAdapter(this, bean,null);
xRecyclerView.setAdapter(xRecyAdapter);
ShowBean showBean = xRecyAdapter.getShowBean();
List<ShowBean.ResultBean.PzshBean.CommodityListBeanX> commodityList = showBean.getResult().getPzsh().getCommodityList();
for (int i = 0; i <commodityList.size() ; i++) {
DaoBean daoBean = new DaoBean(commodityList.get(i).getMasterPic(),commodityList.get(i).getCommodityName());
Log.e("Tag",daoBean+" two");
App.daoSession.insert(daoBean);
}
}
@Override
public void onFail(Object object) {
Toast.makeText(MainActivity.this,object+"",Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
iPresenter.onDeatchView(this);
}
}