package cn.edu.bzu.lims;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.umeng.update.UmengUpdateAgent;
import java.util.List;
import java.util.concurrent.TimeUnit;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.edu.bzu.lims.activity.DetailsActivity;
import cn.edu.bzu.lims.activity.DutyActivity;
import cn.edu.bzu.lims.activity.LoginActivity;
import cn.edu.bzu.lims.activity.MyNewsActivity;
import cn.edu.bzu.lims.activity.NewActivity;
import cn.edu.bzu.lims.activity.SettingActivity;
import cn.edu.bzu.lims.activity.UserInformActivity;
import cn.edu.bzu.lims.adapter.InformRecyclerAdapter;
import cn.edu.bzu.lims.bean.News;
import cn.edu.bzu.lims.bean.User;
import cn.edu.bzu.lims.receiver.MyPushMessageReceiver;
import cn.edu.bzu.lims.util.ImageLoader;
import cn.edu.bzu.lims.util.TestNetWork;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends BaseActivity {
private Toolbar mToolBar;
private RecyclerView mRecyclerView;
private InformRecyclerAdapter mAdapter;
private FloatingActionButton mFab;
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView; //菜单的所在布局
private ActionBarDrawerToggle mActionBarDrawerToggle; //ActionBarDrawerToggle 点击菜单出现与关闭
private SwipeRefreshLayout swipeRefreshLayout;
public BmobUser userCurrent;
private ProgressWheel progressWheel;
private CircleImageView iv_UserPhoto;
private TextView tv_name;
private TextView tv_major;
private String objectId;
private Boolean isNet; //检测网络状态
private BmobQuery<News> query;
private MyPushMessageReceiver receiver ; //广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkNet();
}
/**
* 进入程序的操作
*/
private void firstEnter() {
userCurrent=BmobUser.getCurrentUser(this, User.class);
if(userCurrent==null){
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
finish();
}else{
//TODO 推送消息的 Notification 设置
registerReceiver(receiver,new IntentFilter("PushConstants.ACTION_MESSAGE"));
initView();
initToolBar();
initSwipe();
getData(); //初始化数据源
initAction();
}
}
/**
* 检测网络状态
*/
private void checkNet() {
isNet=new TestNetWork().isNetworkAvailable(this);
if(isNet){
UmengUpdateAgent.update(this);
firstEnter();
}else {
//TODO 这个地方存在bug -- 点击否的处理问题
new TestNetWork().showNetDialog(this);
firstEnter();
}
}
/**
* 得到网络数据
*/
private void getData() {
query = new BmobQuery<>();
query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ELSE_NETWORK); // 先从缓存获取数据,如果没有,再从网络获取。
query.include("user"); //包含作者
query.setLimit(30); //限制接收的数据数量
query.order("-updatedAt");
query.setMaxCacheAge(TimeUnit.DAYS.toMillis(7));//此表示缓存七天天
boolean isCache = query.hasCachedResult(this,News.class);
if(isCache){
query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ELSE_NETWORK); // 如果有缓存的话,则设置策略为CACHE_ELSE_NETWORK
}else{
query.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE); // 如果没有缓存的话,则设置策略为NETWORK_ELSE_CACHE
}
query.findObjects(MainActivity.this, new FindListener<News>() {
@Override
public void onSuccess(List<News> list) {
progressWheel.setVisibility(View.INVISIBLE);
if (list != null) {
initData(list);
} else {
Toast.makeText(MainActivity.this, "暂时没有公告", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(int i, String s) {
// Toast.makeText(MainActivity.this, "查询数据失败", Toast.LENGTH_SHORT).show();
}
});
//初始化左侧菜单栏
objectId = userCurrent.getObjectId();
BmobQuery<User> userBmobQuery = new BmobQuery<>();
userBmobQuery.getObject(MainActivity.this, objectId, new GetListener<User>() {
@Override
public void onSuccess(User user) {
String url =user.getUserPhoto().getFileUrl(MainActivity.this);
iv_UserPhoto.setTag(url);
new ImageLoader().showImageByThread(iv_UserPhoto,url);
tv_name.setText(user.getName());
tv_major.setText(user.getMajor());
}
@Override
public void onFailure(int i, String s) {
Log.e("UserQuery","查询左侧菜单栏用户信息出现错误:"+s);
}
});
}
/**
* 对数据进行赋值
* @param list
*/
private void initData(List<News> list) {
mAdapter= new InformRecyclerAdapter(list,this);
mRecyclerView.setAdapter(mAdapter);
//设置RecyclerView的布局管理
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
//设置RecyclerView的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter.setOnItemClickListener(new InformRecyclerAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position, View view) {
View v = mRecyclerView.getChildAt(position);
TextView tv_objectId = (TextView) v.findViewById(R.id.tv_objectId);
String objectId = tv_objectId.getText().toString();
//公告详情页面的跳转
Intent i =new Intent(MainActivity.this, DetailsActivity.class);
i.putExtra("objectId",objectId);
startActivity(i);
}
@Override
public void OnItemLongClick(int position, View view) {
// Toast.makeText(MainActivity.this, "Click: " + position, Toast.LENGTH_SHORT).show();
}
});
}
/**
* 借助SwipeRefreshLayout实现下拉刷新的操作
*/
private void initSwipe() {
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
//设置刷新时动画的颜色
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override