|
|
@@ -0,0 +1,230 @@
|
|
|
+package com.sheep.gamegroup.module.home.fragment;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.v4.app.Fragment;
|
|
|
+import android.support.v4.app.FragmentTransaction;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.kfzs.duanduan.utils.NumberFormatUtils;
|
|
|
+import com.sheep.gamegroup.event.BigEvent;
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+import com.sheep.gamegroup.model.entity.MessageUnReadEntity;
|
|
|
+import com.sheep.gamegroup.model.entity.UserEntity;
|
|
|
+import com.sheep.gamegroup.model.util.SheepSubscriber;
|
|
|
+import com.sheep.gamegroup.util.DataUtil;
|
|
|
+import com.sheep.gamegroup.util.GlideImageLoader;
|
|
|
+import com.sheep.gamegroup.util.Jump2View;
|
|
|
+import com.sheep.gamegroup.util.ListUtil;
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
+import com.sheep.gamegroup.view.activity.ActMsg;
|
|
|
+import com.sheep.gamegroup.view.dialog.DialogNotificationOfArrival;
|
|
|
+import com.sheep.gamegroup.view.fragment.FgtFind;
|
|
|
+import com.sheep.gamegroup.view.fragment.FgtPersonalCenter;
|
|
|
+import com.sheep.gamegroup.view.fragment.FgtSmallSheep;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.base.BaseFragment;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.SpUtils;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import butterknife.ButterKnife;
|
|
|
+import butterknife.OnClick;
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+
|
|
|
+public class FgtHome extends BaseFragment {
|
|
|
+
|
|
|
+ @BindView(R.id.iv_user_face)
|
|
|
+ ImageView iv_user_face;
|
|
|
+ @BindView(R.id.tv_nickname)
|
|
|
+ TextView tv_nickname;
|
|
|
+ @BindView(R.id.tv_user_asset)
|
|
|
+ TextView tv_user_asset;
|
|
|
+ @BindView(R.id.tv_msg_count)
|
|
|
+ TextView tv_msg_count;
|
|
|
+ @BindView(R.id.iv_flag_vip)
|
|
|
+ ImageView iv_flag_vip;
|
|
|
+ @BindView(R.id.tip_news)
|
|
|
+ View tip_news;
|
|
|
+ @BindView(R.id.v_red_dot)
|
|
|
+ View v_red_dot;
|
|
|
+ @BindView(R.id.tv_recommend)
|
|
|
+ TextView tv_recommend;
|
|
|
+ @BindView(R.id.tv_news)
|
|
|
+ TextView tv_news;
|
|
|
+ @BindView(R.id.line_recommend)
|
|
|
+ View line_recommend;
|
|
|
+ @BindView(R.id.line_news)
|
|
|
+ View line_news;
|
|
|
+
|
|
|
+ private UserEntity user;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getLayoutId() {
|
|
|
+ return R.layout.fgt_home;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate(Bundle bundle) {
|
|
|
+ super.onCreate(bundle);
|
|
|
+ EventBus.getDefault().register(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ EventBus.getDefault().unregister(this);
|
|
|
+ super.onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onViewCreated() {
|
|
|
+ getUnReadMessageCounts();
|
|
|
+ ViewUtil.setVisibility(v_red_dot, SpUtils.isVoucherFirst());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
+ super.onViewCreated(view, savedInstanceState);
|
|
|
+ configFragments(savedInstanceState);
|
|
|
+ user = DataUtil.getInstance().getUserEntity();
|
|
|
+ GlideImageLoader.setAvatar(iv_user_face, user.getAvatar(), Color.argb(128, 255, 255, 255));
|
|
|
+ ViewUtil.setVisibility(iv_flag_vip, user.isVIP());
|
|
|
+ tv_user_asset.setText(String.format(Locale.CHINA, "%s\u0020元", NumberFormatUtils.retainMost2W(user.getBalance())));
|
|
|
+ ViewUtil.setText(tv_nickname, user.getNickname());
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.iv_user_face)
|
|
|
+ public void onFaceClick() {
|
|
|
+ Jump2View.getInstance().goPersonnalCenterView(getActivity(), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.iv_msg)
|
|
|
+ public void onMsgClick() {
|
|
|
+ startActivity(new Intent(getActivity(), ActMsg.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.iv_download)
|
|
|
+ public void onDownloadClick() {
|
|
|
+ Jump2View.getInstance().goActDownloadManager(getActivity());
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.iv_search)
|
|
|
+ public void onSearchClick() {
|
|
|
+ Jump2View.getInstance().goActSearch(getActivity());
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.mall_btn)
|
|
|
+ public void onMallClick() {
|
|
|
+ Jump2View.getInstance().gotoYfShop(getActivity());
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick({R.id.recommend_btn, R.id.news_btn})
|
|
|
+ public void onTabClick(View v) {
|
|
|
+ boolean isRecommend = v.getId() == R.id.recommend_btn;
|
|
|
+ switchFragment(isRecommend ? 0 : 1);
|
|
|
+ tv_recommend.setAlpha(isRecommend ? 1 : 0.8f);
|
|
|
+ line_recommend.setVisibility(isRecommend ? View.VISIBLE : View.GONE);
|
|
|
+ tv_news.setAlpha(!isRecommend ? 1 : 0.8f);
|
|
|
+ line_news.setVisibility(!isRecommend ? View.VISIBLE : View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void configFragments(Bundle savedInstanceState) {
|
|
|
+ if (savedInstanceState == null) {
|
|
|
+ Fragment fragment0 = new FgtSmallSheep();
|
|
|
+ Fragment fragment1 = new FgtFind();
|
|
|
+ FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
|
|
+ transaction.add(R.id.fragment_container, fragment1, "fragment_tab_1").hide(fragment1);
|
|
|
+ transaction.add(R.id.fragment_container, fragment0, "fragment_tab_0").show(fragment0);
|
|
|
+ transaction.commitAllowingStateLoss();
|
|
|
+ } else {
|
|
|
+ Fragment fragment0 = getChildFragmentManager().findFragmentByTag("fragment_tab_0");
|
|
|
+ Fragment fragment1 = getChildFragmentManager().findFragmentByTag("fragment_tab_1");
|
|
|
+ FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
|
|
+ transaction.hide(fragment1);
|
|
|
+ transaction.show(fragment0);
|
|
|
+ transaction.commitAllowingStateLoss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void switchFragment(int position) {
|
|
|
+ FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
|
|
+ Fragment fragment0 = getChildFragmentManager().findFragmentByTag("fragment_tab_0");
|
|
|
+ Fragment fragment1 = getChildFragmentManager().findFragmentByTag("fragment_tab_1");
|
|
|
+ if (fragment0 != null) {
|
|
|
+ if (position == 0) {
|
|
|
+ transaction.show(fragment0);
|
|
|
+ fragment0.setUserVisibleHint(true);
|
|
|
+ } else {
|
|
|
+ transaction.hide(fragment0);
|
|
|
+ fragment0.setUserVisibleHint(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fragment1 != null) {
|
|
|
+ if (position == 1) {
|
|
|
+ transaction.show(fragment1);
|
|
|
+ fragment1.setUserVisibleHint(true);
|
|
|
+ } else {
|
|
|
+ transaction.hide(fragment1);
|
|
|
+ fragment1.setUserVisibleHint(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ transaction.commitAllowingStateLoss();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取是否有未读消息并更新图标
|
|
|
+ */
|
|
|
+ private void getUnReadMessageCounts() {
|
|
|
+ SheepApp.getInstance()
|
|
|
+ .getNetComponent()
|
|
|
+ .getApiService()
|
|
|
+ .getMessageUnReadNum(DataUtil.getInstance().getUserId())
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
+ @Override
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
+ MessageUnReadEntity entity = baseMessage.getData(MessageUnReadEntity.class);
|
|
|
+ if (entity != null && entity.getNum() > 0) {
|
|
|
+ ViewUtil.setVisibility(tv_msg_count, true);
|
|
|
+ ViewUtil.setText(tv_msg_count, entity.getNum());
|
|
|
+ DialogNotificationOfArrival.checkNotificationOfArrivalList(getActivity());
|
|
|
+ } else {
|
|
|
+ ViewUtil.setVisibility(tv_msg_count, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe
|
|
|
+ public void onEventMainThread(BigEvent event) {
|
|
|
+ switch (event.getEventTypes()) {
|
|
|
+ case FGT_SHEEP_HOME_MESSAGE_COUNT:
|
|
|
+ if (getActivity() != null) {
|
|
|
+ getActivity().runOnUiThread(() -> getUnReadMessageCounts());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String KEY_HAS_APPOINT_TASK = "hasAppointTask";
|
|
|
+
|
|
|
+ @Subscribe
|
|
|
+ public void whenPersonVoucherClick(FgtPersonalCenter.PersonVoucherClickEvent ev) {
|
|
|
+ ViewUtil.setVisibility(v_red_dot, DataUtil.getAsBoolean(KEY_HAS_APPOINT_TASK, false));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|