210 lines
7.8 KiB
Java
210 lines
7.8 KiB
Java
package com.gh.gamecenter.fragment;
|
|
|
|
import android.os.Bundle;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.app.Fragment;
|
|
import android.view.View;
|
|
|
|
import com.gh.base.fragment.BaseFragment_ViewPager_Checkable;
|
|
import com.gh.common.util.CheckLoginUtils;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.db.GameTrendsDao;
|
|
import com.gh.gamecenter.db.info.GameTrendsInfo;
|
|
import com.gh.gamecenter.discover.DiscoverFragment;
|
|
import com.gh.gamecenter.entity.ConcernEntity;
|
|
import com.gh.gamecenter.eventbus.EBConcernChanged;
|
|
import com.gh.gamecenter.eventbus.EBReuse;
|
|
import com.gh.gamecenter.eventbus.EBSkip;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
import com.gh.gamecenter.manager.UserManager;
|
|
import com.gh.gamecenter.personal.PersonalFragment;
|
|
import com.gh.gamecenter.qa.AskFragment;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.lightgame.view.NoScrollableViewPager;
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.util.List;
|
|
|
|
import butterknife.BindView;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
import static com.gh.gamecenter.MainActivity.EB_MAINACTIVITY_TAG;
|
|
import static com.gh.gamecenter.MainActivity.EB_SKIP_GAMEFRAGMENT;
|
|
|
|
/**
|
|
* @author CsHeng
|
|
* @Date 05/05/2017
|
|
* @Time 3:26 PM
|
|
*/
|
|
|
|
public class MainWrapperFragment extends BaseFragment_ViewPager_Checkable {
|
|
|
|
@BindView(R.id.main_iv_message_hint)
|
|
View mMessageHintIv;
|
|
|
|
@BindView(R.id.iv_discovery_hint_dot)
|
|
protected View mDiscoveryHintIv;
|
|
|
|
public static final int INDEX_GAME = 0;
|
|
public static final int INDEX_ASK = 1;
|
|
public static final int INDEX_DISCOVER = 2;
|
|
public static final int INDEX_PERSONAL = 3;
|
|
|
|
private static final int INTERVAL_REQUEST_DISCOVERY_DATA = 5 * 60 * 1000; // 5分钟的发现游戏动态请求间隔
|
|
|
|
private GameTrendsDao mGameTrendsDao;
|
|
|
|
private long mLastRequestDiscoveryTime;
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_main;
|
|
}
|
|
|
|
@Override
|
|
protected int getCheckableGroupId() {
|
|
return R.id.lightgame_tab_container;
|
|
}
|
|
|
|
@Override
|
|
protected int getViewPagerId() {
|
|
return R.id.lightgame_tab_viewpager;
|
|
}
|
|
|
|
@Override
|
|
protected void initFragmentList(List<Fragment> fragments) {
|
|
fragments.add(new GameWrapperFragment());
|
|
fragments.add(new AskFragment());
|
|
fragments.add(new DiscoverFragment());
|
|
fragments.add(new PersonalFragment());
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
mGameTrendsDao = new GameTrendsDao(getContext());
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
getDiscoveryData(false);
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
if (mViewPager instanceof NoScrollableViewPager) {
|
|
((NoScrollableViewPager) mViewPager).setScrollable(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected boolean handleOnClick(View view) {
|
|
boolean res = super.handleOnClick(view);
|
|
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, mCheckedIndex));
|
|
return res;
|
|
}
|
|
|
|
@Override
|
|
protected void onPageChanged(int index) {
|
|
super.onPageChanged(index);
|
|
|
|
switch (index) {
|
|
case INDEX_GAME:
|
|
DataUtils.onMtaEvent(getContext(), "顶级页面", "BottomBar", "游戏");
|
|
break;
|
|
case INDEX_ASK:
|
|
DataUtils.onMtaEvent(getContext(), "顶级页面", "BottomBar", "问答");
|
|
break;
|
|
case INDEX_DISCOVER:
|
|
DataUtils.onMtaEvent(getContext(), "顶级页面", "BottomBar", "发现");
|
|
break;
|
|
case INDEX_PERSONAL:
|
|
DataUtils.onMtaEvent(getContext(), "顶级页面", "BottomBar", "我的光环");
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBSkip skip) {
|
|
if (EB_SKIP_GAMEFRAGMENT.equals(skip.getType())) {
|
|
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_GAME));
|
|
checkIndex(INDEX_GAME);
|
|
EventBus.getDefault().post(new EBSkip(EB_MAINACTIVITY_TAG, skip.getCurrentItem()));
|
|
mViewPager.setCurrentItem(skip.getCurrentItem(), false);
|
|
}
|
|
}
|
|
|
|
private void getDiscoveryData(boolean immediately) {
|
|
if (CheckLoginUtils.isLogin()) {
|
|
long currentTimeMills = System.currentTimeMillis();
|
|
if (currentTimeMills - mLastRequestDiscoveryTime > INTERVAL_REQUEST_DISCOVERY_DATA || immediately) {
|
|
mLastRequestDiscoveryTime = currentTimeMills;
|
|
RetrofitManager.getInstance(getContext()).getApi()
|
|
.getZiXunConcern(UserManager.getInstance().getUserId(), 1)
|
|
.subscribeOn(Schedulers.io())
|
|
.subscribe(new Response<List<ConcernEntity>>() {
|
|
@Override
|
|
public void onResponse(List<ConcernEntity> response) {
|
|
super.onResponse(response);
|
|
if (response.size() > 0) {
|
|
ConcernEntity entity = response.get(0);
|
|
long internetPostTime = entity.getTime();
|
|
long readPostTime = mGameTrendsDao.findReadPostTime(UserManager.getInstance().getUserId());
|
|
// 当上一个条产生小红点的条目不存在或者有新的条目时更新数据库
|
|
if (readPostTime == 0 || readPostTime < internetPostTime) {
|
|
mGameTrendsDao.add(new GameTrendsInfo(UserManager.getInstance().getUserId(), internetPostTime, readPostTime, entity.getGameIcon()));
|
|
EventBus.getDefault().post(new EBReuse(DiscoverFragment.SHOW_DISCOVERY_DOT));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// 控制 我的光环消息未读红点
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBReuse reuse) {
|
|
if (PersonalFragment.MESSAGE_READ_OVER.equals(reuse.getType())) { // 消息阅读完成
|
|
if (mMessageHintIv != null) {
|
|
mMessageHintIv.setVisibility(View.GONE);
|
|
}
|
|
} else if (PersonalFragment.MESSAGE_UNREAD_TAG.equals(reuse.getType())) {
|
|
if (mMessageHintIv != null) {
|
|
mMessageHintIv.setVisibility(View.VISIBLE);
|
|
}
|
|
} else if (DiscoverFragment.SHOW_DISCOVERY_DOT.equals(reuse.getType())) {
|
|
if (mDiscoveryHintIv != null) {
|
|
mDiscoveryHintIv.setVisibility(View.VISIBLE);
|
|
}
|
|
} else if (DiscoverFragment.HIDE_DISCOVERY_DOT.equals(reuse.getType())) {
|
|
if (mDiscoveryHintIv != null) {
|
|
mDiscoveryHintIv.setVisibility(View.GONE);
|
|
}
|
|
} else if (PersonalFragment.LOGOUT_TAG.equals(reuse.getType())) {
|
|
if (mDiscoveryHintIv != null) {
|
|
mDiscoveryHintIv.setVisibility(View.GONE);
|
|
}
|
|
} else if (PersonalFragment.LOGIN_TAG.equals(reuse.getType())) {
|
|
if (mDiscoveryHintIv != null) {
|
|
getDiscoveryData(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 关注事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBConcernChanged changed) {
|
|
if (mDiscoveryHintIv != null) {
|
|
getDiscoveryData(true);
|
|
}
|
|
}
|
|
}
|