678 lines
31 KiB
Java
678 lines
31 KiB
Java
package com.gh.gamecenter.personal;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.content.Intent;
|
||
import android.content.SharedPreferences;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.preference.PreferenceManager;
|
||
import android.text.TextUtils;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.RelativeLayout;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.annotation.Nullable;
|
||
import androidx.appcompat.widget.Toolbar;
|
||
import androidx.core.content.ContextCompat;
|
||
import androidx.lifecycle.Observer;
|
||
import androidx.lifecycle.ViewModelProviders;
|
||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||
import androidx.recyclerview.widget.RecyclerView;
|
||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||
|
||
import com.facebook.drawee.view.SimpleDraweeView;
|
||
import com.gh.base.fragment.BaseFragment;
|
||
import com.gh.common.databind.BindingAdapters;
|
||
import com.gh.common.notifier.Notifier;
|
||
import com.gh.common.util.CheckLoginUtils;
|
||
import com.gh.common.util.DataUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.DirectUtils;
|
||
import com.gh.common.util.DisplayUtils;
|
||
import com.gh.common.util.EntranceUtils;
|
||
import com.gh.common.util.ImageUtils;
|
||
import com.gh.common.util.LogUtils;
|
||
import com.gh.common.util.LoginHelper;
|
||
import com.gh.common.util.MtaHelper;
|
||
import com.gh.common.util.NetworkUtils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.StringUtils;
|
||
import com.gh.common.view.VerticalItemDecoration;
|
||
import com.gh.gamecenter.GameDetailActivity;
|
||
import com.gh.gamecenter.MainActivity;
|
||
import com.gh.gamecenter.MessageActivity;
|
||
import com.gh.gamecenter.NewsDetailActivity;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.entity.AppEntity;
|
||
import com.gh.gamecenter.entity.FunctionalGroupEntity;
|
||
import com.gh.gamecenter.entity.FunctionalLinkEntity;
|
||
import com.gh.gamecenter.entity.FunctionalMessageType;
|
||
import com.gh.gamecenter.entity.LoginTokenEntity;
|
||
import com.gh.gamecenter.entity.MessageUnreadEntity;
|
||
import com.gh.gamecenter.entity.SignEntity;
|
||
import com.gh.gamecenter.entity.UserInfoEntity;
|
||
import com.gh.gamecenter.eventbus.EBConcernChanged;
|
||
import com.gh.gamecenter.eventbus.EBNetworkState;
|
||
import com.gh.gamecenter.eventbus.EBReuse;
|
||
import com.gh.gamecenter.eventbus.EBSkip;
|
||
import com.gh.gamecenter.fragment.MainWrapperFragment;
|
||
import com.gh.gamecenter.manager.UserManager;
|
||
import com.gh.gamecenter.message.MessageUnreadRepository;
|
||
import com.gh.gamecenter.message.MessageUnreadViewModel;
|
||
import com.gh.gamecenter.personalhome.UserHomeViewModel;
|
||
import com.gh.gamecenter.qa.answer.detail.AnswerDetailActivity;
|
||
import com.gh.gamecenter.retrofit.Response;
|
||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||
import com.gh.gamecenter.room.AppDatabase;
|
||
import com.gh.gamecenter.subject.SubjectActivity;
|
||
import com.gh.gamecenter.user.ApiResponse;
|
||
import com.gh.gamecenter.user.UserViewModel;
|
||
import com.google.android.material.appbar.AppBarLayout;
|
||
import com.halo.assistant.HaloApp;
|
||
import com.jakewharton.rxbinding2.view.RxView;
|
||
import com.lightgame.utils.Utils;
|
||
import com.tencent.connect.common.Constants;
|
||
|
||
import org.greenrobot.eventbus.EventBus;
|
||
import org.greenrobot.eventbus.Subscribe;
|
||
import org.greenrobot.eventbus.ThreadMode;
|
||
import org.json.JSONObject;
|
||
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.Locale;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
import butterknife.BindView;
|
||
import butterknife.OnClick;
|
||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||
import io.reactivex.schedulers.Schedulers;
|
||
import okhttp3.MediaType;
|
||
import okhttp3.RequestBody;
|
||
import okhttp3.ResponseBody;
|
||
import retrofit2.HttpException;
|
||
|
||
import static com.gh.gamecenter.fragment.MainWrapperFragment.INDEX_PERSONAL;
|
||
|
||
public class PersonalFragment extends BaseFragment implements Observer<ApiResponse<UserInfoEntity>> {
|
||
|
||
@BindView(R.id.list_refresh)
|
||
SwipeRefreshLayout mListRefresh;
|
||
@BindView(R.id.appbar)
|
||
AppBarLayout appbar;
|
||
@BindView(R.id.toolbar)
|
||
Toolbar toolbar;
|
||
@BindView(R.id.personal_sign)
|
||
ImageView mPersonalSign;
|
||
@BindView(R.id.personal_msg)
|
||
ImageView mPersonalMsg;
|
||
@BindView(R.id.login_message_hint)
|
||
TextView mLoginMessageHint;
|
||
@BindView(R.id.personal_user_icon)
|
||
SimpleDraweeView mUserIcon;
|
||
@BindView(R.id.personal_user_small_icon)
|
||
SimpleDraweeView mUserIconSmall;
|
||
@BindView(R.id.personal_user_badge)
|
||
SimpleDraweeView mUserBadge;
|
||
@BindView(R.id.personal_login)
|
||
View mPersonalLogin;
|
||
@BindView(R.id.personal_home)
|
||
TextView mPersonalHome;
|
||
@BindView(R.id.personal_user_name)
|
||
TextView mPersonalUserName;
|
||
@BindView(R.id.personal_user_name_small)
|
||
TextView mPersonalUserNameSmall;
|
||
@BindView(R.id.personal_badge)
|
||
RelativeLayout mPersonalBadge;
|
||
@BindView(R.id.personal_badge_tv)
|
||
TextView mPersonalBadgeTv;
|
||
@BindView(R.id.personal_badge_tips)
|
||
View mPersonalBadgeTips;
|
||
@BindView(R.id.personal_func)
|
||
RecyclerView mFuncRecyclerView;
|
||
@BindView(R.id.personal_new_fans_tips)
|
||
View personalNewFansTips;
|
||
|
||
public final static String LOGIN_TAG = "login_tag"; //登入标识
|
||
public final static String LOGOUT_TAG = "logout_tag"; // 登出标识
|
||
public final static String MESSAGE_UNREAD_TAG = "message_unread_tag";
|
||
public final static String MESSAGE_READ_OVER = "MESSAGE_READ_OVER";
|
||
private final static int REQUEST_MESSAGE = 199;
|
||
|
||
private UserInfoEntity mUserInfoEntity;
|
||
|
||
private SharedPreferences sp;
|
||
private AppDatabase mDatabase;
|
||
|
||
private UserViewModel mUserViewModel;
|
||
private MessageUnreadViewModel mUnreadViewModel;
|
||
private UserHomeViewModel mUserHomeViewModel;
|
||
private PersonalViewModel mPersonalViewModel;
|
||
|
||
private PersonalFunctionGroupAdapter mPersonalFuncGroupAdapter;
|
||
|
||
private boolean mIsLogging = false;
|
||
|
||
@Override
|
||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||
super.onActivityResult(requestCode, resultCode, data);
|
||
if (requestCode == Constants.REQUEST_LOGIN) { // QQ Login callback
|
||
LoginHelper.onQQLoginCallback(requestCode, resultCode, data);
|
||
} else if (requestCode == 32973) {// 32973 WeiBo requestCode
|
||
LoginHelper.onWeiboLoginCallback(requestCode, resultCode, data);
|
||
} else if (requestCode == REQUEST_MESSAGE) {
|
||
if (mUnreadViewModel != null) mUnreadViewModel.retry();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.fragment_personal;
|
||
}
|
||
|
||
@SuppressLint("CheckResult")
|
||
@Override
|
||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
|
||
sp = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||
mDatabase = AppDatabase.getInstance(getContext());
|
||
|
||
mPersonalFuncGroupAdapter = new PersonalFunctionGroupAdapter(requireContext());
|
||
mFuncRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||
mFuncRecyclerView.setAdapter(mPersonalFuncGroupAdapter);
|
||
mFuncRecyclerView.addItemDecoration(new VerticalItemDecoration(requireContext(), 8f, false));
|
||
|
||
final UserViewModel.Factory factory = new UserViewModel.Factory(getActivity().getApplication());
|
||
mUserViewModel = ViewModelProviders.of(this, factory).get(UserViewModel.class);
|
||
mUserViewModel.getLoginObsUserinfo().observe(this, this);
|
||
|
||
mUserHomeViewModel = ViewModelProviders.of(this, new UserHomeViewModel.Factory(HaloApp.getInstance().getApplication(),
|
||
UserManager.getInstance().getUserId())).get(UserHomeViewModel.class);
|
||
mUserHomeViewModel.getBadges().observe(this, badgeEntities -> {
|
||
if (badgeEntities.isEmpty()) {
|
||
mPersonalBadgeTv.setText("领取徽章");
|
||
} else {
|
||
mPersonalBadgeTv.setText(String.format(Locale.CHINA, "%d枚徽章", badgeEntities.size()));
|
||
}
|
||
});
|
||
mUserHomeViewModel.getAvailableBadgeCount().observe(this, count -> mPersonalBadgeTips.setVisibility(count > 0 ? View.VISIBLE : View.GONE));
|
||
|
||
mPersonalViewModel = ViewModelProviders.of(this).get(PersonalViewModel.class);
|
||
mPersonalViewModel.getHaloAddData().observe(this, datas -> {
|
||
if (!datas.isEmpty()) {
|
||
mPersonalFuncGroupAdapter.setListData(datas);
|
||
checkUpdate();
|
||
MessageUnreadRepository.INSTANCE.loadMessageUnreadTotal();
|
||
}
|
||
});
|
||
mPersonalViewModel.getHaloAddons();
|
||
|
||
mUnreadViewModel = ViewModelProviders.of(this,
|
||
new MessageUnreadViewModel.Factory(HaloApp.getInstance().getApplication())).get(MessageUnreadViewModel.class);
|
||
mUnreadViewModel.getLiveData().observe(this
|
||
, messageUnread -> {
|
||
if (messageUnread != null && messageUnread.getTotal() > 0) {
|
||
personalNewFansTips.setVisibility(messageUnread.getFans() > 0 ? View.VISIBLE : View.GONE);
|
||
mPersonalHome.setText(messageUnread.getFans() > 0 ? ((messageUnread.getFans() < 100 ? messageUnread.getFans() : "99+") + "位新粉丝") : "个人主页");
|
||
int count = messageUnread.getTotal() - messageUnread.getFans();
|
||
mLoginMessageHint.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
|
||
BindingAdapters.setMessageUnread(mLoginMessageHint, count);
|
||
|
||
// 第一次开启应用时检查消息中心看有没有未读消息确定需不需要弹 Notifier 通知
|
||
showNotifier(messageUnread);
|
||
EventBus.getDefault().post(new EBReuse(MESSAGE_UNREAD_TAG));
|
||
} else {
|
||
mLoginMessageHint.setVisibility(View.GONE);
|
||
mPersonalHome.setText("个人主页");
|
||
personalNewFansTips.setVisibility(View.GONE);
|
||
EventBus.getDefault().post(new EBReuse(MESSAGE_READ_OVER));
|
||
}
|
||
});
|
||
mUnreadViewModel.getZixunConcernLiveData().observe(this, hasConcern -> {
|
||
notifyItemChange("游戏动态", hasConcern ? FunctionalMessageType.NEW_MESSAGE : null);
|
||
});
|
||
mUnreadViewModel.getAddonsUnreadLiveData().observe(this, entity -> {
|
||
notifyItemChange("我的收藏", entity.getFavorite() > 0 ? FunctionalMessageType.NEW_MESSAGE : null);
|
||
});
|
||
|
||
// 微信/签到
|
||
RxView.clicks(mPersonalSign)
|
||
.throttleFirst(1, TimeUnit.SECONDS)
|
||
.subscribe(aVoid -> {
|
||
if (CheckLoginUtils.isLogin()) {
|
||
MtaHelper.onEvent("我的光环", "签到");
|
||
MtaHelper.onEvent("我的光环_新", "签到", "点击签到");
|
||
sign();
|
||
} else {
|
||
MtaHelper.onEvent("我的光环_新", "功能入口-跳转登录", "签到");
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-签到", () -> {
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||
super.onViewCreated(view, savedInstanceState);
|
||
int statusBarHeight = Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ? 0 : DisplayUtils.getStatusBarHeight(getResources());
|
||
ViewGroup.LayoutParams params = toolbar.getLayoutParams();
|
||
params.height = DisplayUtils.dip2px(50) + statusBarHeight;
|
||
toolbar.setLayoutParams(params);
|
||
|
||
appbar.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
|
||
int absOffset = Math.abs(verticalOffset);
|
||
int invisibleOffset = DisplayUtils.dip2px(40F);
|
||
if (absOffset <= invisibleOffset) {
|
||
mUserIconSmall.setVisibility(View.GONE);
|
||
mPersonalUserNameSmall.setVisibility(View.GONE);
|
||
} else {
|
||
mUserIconSmall.setVisibility(View.VISIBLE);
|
||
mPersonalUserNameSmall.setVisibility(View.VISIBLE);
|
||
}
|
||
mListRefresh.setEnabled(absOffset <= 2);
|
||
});
|
||
mListRefresh.setColorSchemeColors(ContextCompat.getColor(requireContext(), R.color.theme));
|
||
mListRefresh.setProgressViewOffset(false, 0, DisplayUtils.dip2px(80) + DisplayUtils.getStatusBarHeight(requireContext().getResources()));
|
||
mListRefresh.setOnRefreshListener(() -> {
|
||
mPersonalViewModel.getHaloAddons();
|
||
if (CheckLoginUtils.isLogin()) {
|
||
mUnreadViewModel.retry();
|
||
MessageUnreadRepository.INSTANCE.loadMessageUnreadTotal();
|
||
mUserHomeViewModel.getBadgeList();
|
||
mUserHomeViewModel.availableBadges();
|
||
}
|
||
mBaseHandler.postDelayed(() -> {
|
||
mListRefresh.setRefreshing(false);
|
||
}, 2000);
|
||
MtaHelper.onEvent("我的光环_新", "下拉刷新", "下拉刷新");
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onResume() {
|
||
super.onResume();
|
||
if (NetworkUtils.isNetworkConnected(requireContext())) {
|
||
mUnreadViewModel.retry();
|
||
mUserHomeViewModel.getBadgeList();
|
||
mUserHomeViewModel.availableBadges();
|
||
}
|
||
int currentItem = ((MainWrapperFragment) getParentFragment()).getCurrentItem();
|
||
if (currentItem == INDEX_PERSONAL) {
|
||
DisplayUtils.setLightStatusBar(requireActivity(), false);
|
||
}
|
||
}
|
||
|
||
private void showNotifier(MessageUnreadEntity messageUnread) {
|
||
if (mUnreadViewModel.isFirstTimeInit()) {
|
||
mUnreadViewModel.flagFirstTimeInit(false);
|
||
|
||
if (messageUnread.getMeta() != null && messageUnread.getMeta().getUser() != null) {
|
||
if (messageUnread.getMeta() != null) {
|
||
String displayText = StringUtils.shrinkStringWithDot(messageUnread.getMeta().getUser().getName(), 8);
|
||
String suffixText;
|
||
if ("follow_question".equals(messageUnread.getMeta().getType())) {
|
||
suffixText = "回答了你关注的问题";
|
||
} else {
|
||
suffixText = "回答了你的问题";
|
||
}
|
||
displayText += suffixText;
|
||
|
||
String content = messageUnread.getMeta().getAnswerId() + displayText;
|
||
|
||
if (Notifier.shouldShowNotifier(content)) {
|
||
Notifier.create(getActivity())
|
||
.setText(displayText)
|
||
.setDuration(5000)
|
||
.setIcon(messageUnread.getMeta().getUser().getIcon())
|
||
.setOnClickListener(view -> {
|
||
Bundle bundle = new Bundle();
|
||
bundle.putString(EntranceUtils.KEY_ANSWER_ID, messageUnread.getMeta().getAnswerId());
|
||
bundle.putString(EntranceUtils.KEY_ENTRANCE, EntranceUtils.ENTRANCE_UMENG);
|
||
bundle.putString(EntranceUtils.KEY_TO, AnswerDetailActivity.class.getName());
|
||
EntranceUtils.jumpActivity(getActivity(), bundle);
|
||
|
||
MtaHelper.onEvent("消息弹窗", suffixText, "Does not contains any parameter.");
|
||
|
||
// 标记已读
|
||
JSONObject jsonObject = new JSONObject();
|
||
try {
|
||
jsonObject.put("type", messageUnread.getMeta().getType());
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
|
||
RetrofitManager.getInstance(requireContext()).getApi()
|
||
.postMessageRead(UserManager.getInstance().getUserId(), messageUnread.getMeta().getMessageId(), body)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<ResponseBody>() {
|
||
@Override
|
||
public void onResponse(@Nullable ResponseBody response) {
|
||
super.onResponse(response);
|
||
MessageUnreadRepository.INSTANCE.loadMessageUnreadData();
|
||
}
|
||
});
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
Notifier.hide();
|
||
})
|
||
.show(true, 500L);
|
||
Notifier.tagNotifierAsShowed(content);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@OnClick({R.id.toolbar, R.id.collapsingToolbar, R.id.iv_arrow, R.id.personal_info, R.id.personal_login, R.id.personal_user_small_icon, R.id.personal_user_icon, R.id.personal_msg,
|
||
R.id.personal_user_name_small, R.id.personal_user_name, R.id.personal_badge, R.id.personal_home})
|
||
public void onViewClicked(View view) {
|
||
switch (view.getId()) {
|
||
case R.id.collapsingToolbar:
|
||
case R.id.toolbar:
|
||
case R.id.personal_info:
|
||
if (mUserInfoEntity == null) {
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-手机登录", null);
|
||
}
|
||
break;
|
||
case R.id.personal_login:
|
||
MtaHelper.onEvent("我的光环_新", "立即登录", "点击登录");
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-立即登录", null);
|
||
break;
|
||
case R.id.personal_msg:
|
||
if (CheckLoginUtils.isLogin()) {
|
||
MtaHelper.onEvent("我的光环", "消息");
|
||
MtaHelper.onEvent("我的光环_新", "消息中心", "点击消息中心");
|
||
startActivityForResult(MessageActivity.getIntent(getContext(), "(我的光环)+(消息中心)"), REQUEST_MESSAGE);
|
||
} else {
|
||
MtaHelper.onEvent("我的光环_新", "功能入口-跳转登录", "消息中心");
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-消息", () -> {
|
||
});
|
||
}
|
||
break;
|
||
case R.id.personal_user_small_icon:
|
||
case R.id.personal_user_icon:
|
||
if (mUserInfoEntity != null) {
|
||
MtaHelper.onEvent("我的光环", "个人中心");
|
||
MtaHelper.onEvent("我的光环_新", "头像", "点击头像");
|
||
DirectUtils.directToHomeActivity(requireContext(), UserManager.getInstance().getUserId(), "", "我的光环");
|
||
} else {
|
||
MtaHelper.onEvent("我的光环", "手机登录");
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-手机登录", null);
|
||
}
|
||
break;
|
||
case R.id.personal_user_name_small:
|
||
case R.id.personal_user_name:
|
||
if (mUserInfoEntity != null) {
|
||
MtaHelper.onEvent("我的光环", "个人中心");
|
||
MtaHelper.onEvent("我的光环_新", "昵称", "点击昵称");
|
||
DirectUtils.directToHomeActivity(requireContext(), UserManager.getInstance().getUserId(), "", "我的光环");
|
||
} else {
|
||
MtaHelper.onEvent("我的光环", "手机登录");
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-手机登录", null);
|
||
}
|
||
break;
|
||
case R.id.iv_arrow:
|
||
case R.id.personal_home:
|
||
if (mUserInfoEntity != null) {
|
||
MtaHelper.onEvent("我的光环", "个人主页");
|
||
MtaHelper.onEvent("我的光环_新", "个人主页", "进入个人主页");
|
||
DirectUtils.directToHomeActivity(requireContext(), UserManager.getInstance().getUserId(), "", "我的光环");
|
||
} else {
|
||
CheckLoginUtils.checkLogin(getContext(), "我的光环-个人主页", null);
|
||
}
|
||
break;
|
||
case R.id.personal_badge:
|
||
MtaHelper.onEvent("我的光环_新", "徽章中心", "进入徽章中心");
|
||
DirectUtils.directToBadgeWall(requireContext(), mUserInfoEntity.getUserId(), mUserInfoEntity.getName(), mUserInfoEntity.getIcon());
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 连接上网络事件
|
||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||
public void onEventMainThread(EBNetworkState busNetworkState) {
|
||
if (busNetworkState.isNetworkConnected()
|
||
&& mUserViewModel.isLogin()
|
||
&& (mUserInfoEntity == null || TextUtils.isEmpty(UserManager.getInstance().getToken()))) {
|
||
mUserViewModel.retryCheckLogin();
|
||
}
|
||
if (busNetworkState.isNetworkConnected()) {
|
||
mPersonalViewModel.getHaloAddons();
|
||
}
|
||
}
|
||
|
||
|
||
// @Subscribe(threadMode = ThreadMode.MAIN)
|
||
// public void onEventMainThread(EBUISwitch busNine) {
|
||
// if (MainWrapperFragment.EB_MAIN_SCROLL_TOP.equals(busNine.getFrom())
|
||
// && INDEX_PERSONAL == busNine.getPosition()) {
|
||
// //mScrollView.fullScroll(ScrollView.FOCUS_UP);
|
||
// }
|
||
// }
|
||
|
||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||
public void onEventMainThread(EBReuse reuse) {
|
||
if (MESSAGE_READ_OVER.equals(reuse.getType())) { // 消息阅读完成
|
||
if (mLoginMessageHint != null) {
|
||
mLoginMessageHint.setVisibility(View.GONE);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
private void sign() {
|
||
RetrofitManager.getInstance(getContext()).getApi()
|
||
.postSign(UserManager.getInstance().getUserId())
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<SignEntity>() {
|
||
@Override
|
||
public void onResponse(SignEntity signEntity) {
|
||
mPersonalSign.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.personal_sign_icon_yellow));
|
||
if (isCanSign(signEntity.getLastTime())) {
|
||
DialogUtils.showSignDialog(getContext(), "签到成功,获得经验:1"
|
||
, getString(R.string.sign_dialog_content, signEntity.getSerialSign())
|
||
, getString(R.string.sign_dialog_content2, signEntity.getExperience())
|
||
, getSignSkipText(signEntity.getTitle()), () -> {
|
||
signSkip(signEntity);
|
||
}
|
||
);
|
||
signEntity.setLastTime(System.currentTimeMillis() / 1000);
|
||
} else {
|
||
DialogUtils.showSignDialog(getContext(), "今天已签到,明天再来吧~"
|
||
, getString(R.string.sign_dialog_content, signEntity.getSerialSign())
|
||
, getString(R.string.sign_dialog_content2, signEntity.getExperience())
|
||
, getSignSkipText(signEntity.getTitle()), () -> {
|
||
signSkip(signEntity);
|
||
}
|
||
);
|
||
}
|
||
|
||
signEntity.setId(UserManager.getInstance().getUserId());
|
||
if (mDatabase.signDao().updateSignEntity(signEntity) <= 0) {
|
||
mDatabase.signDao().addSignEntity(signEntity);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
if (e == null || e.code() != 401) {
|
||
toast(R.string.loading_network_error);
|
||
}
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
private boolean isCanSign(long time) {
|
||
SimpleDateFormat formatDay = new SimpleDateFormat("dd", Locale.CHINA);
|
||
|
||
long lastSignTime = time * 1000;
|
||
long curTime = Utils.getTime(getContext()) * 1000;
|
||
int lastSignDay = Integer.parseInt(formatDay.format(lastSignTime));
|
||
int curDay = Integer.parseInt(formatDay.format(curTime));
|
||
if (lastSignDay != curDay || curTime - lastSignTime > 24 * 60 * 60 * 1000) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
private String getSignSkipText(String text) {
|
||
return TextUtils.isEmpty(text) ? "去首页看看" : text;
|
||
}
|
||
|
||
private void signSkip(SignEntity signEntity) {
|
||
SignEntity.Data data = signEntity.getData();
|
||
String entrance = "(我的光环)+(签到)";
|
||
if (data == null || TextUtils.isEmpty(data.getType())) {
|
||
EventBus.getDefault().post(new EBSkip(MainActivity.EB_SKIP_MAIN, MainWrapperFragment.INDEX_HOME));
|
||
return;
|
||
}
|
||
switch (data.getType()) {
|
||
case "game":
|
||
DataUtils.onMtaEvent(getActivity(), "我的光环_签到跳转", "游戏", signEntity.getTitle());
|
||
GameDetailActivity.startGameDetailActivity(getContext(), data.getLink(), entrance);
|
||
break;
|
||
case "news":
|
||
DataUtils.onMtaEvent(getActivity(), "我的光环_签到跳转", "文章", signEntity.getTitle());
|
||
startActivity(NewsDetailActivity.getIntentById(getContext(), data.getLink(), entrance));
|
||
break;
|
||
case "column":
|
||
DataUtils.onMtaEvent(getActivity(), "我的光环_签到跳转", "专题", signEntity.getTitle());
|
||
SubjectActivity.startSubjectActivity(getContext(), data.getLink(), null, false, entrance);
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onChanged(ApiResponse<UserInfoEntity> userInfoEntity) {
|
||
UserInfoEntity notifyUserInfo;
|
||
if (userInfoEntity != null) {
|
||
notifyUserInfo = userInfoEntity.getData();
|
||
} else {
|
||
notifyUserInfo = null;
|
||
}
|
||
|
||
if (notifyUserInfo != null && mUserInfoEntity == null) { // 单个用户,首次触发
|
||
EventBus.getDefault().post(new EBConcernChanged());
|
||
|
||
LoginTokenEntity loginTokenEntity = UserManager.getInstance().getLoginTokenEntity();
|
||
if (mIsLogging && loginTokenEntity != null) {
|
||
String loginType = loginTokenEntity.getLoginType();
|
||
LogUtils.login("success", loginType, getLoginEntranceByType(loginType));
|
||
mIsLogging = false;
|
||
}
|
||
}
|
||
|
||
mUserInfoEntity = notifyUserInfo;
|
||
if (notifyUserInfo == null) {
|
||
mUnreadViewModel.clean();
|
||
changeLoginState(false);
|
||
EventBus.getDefault().post(new EBConcernChanged());
|
||
} else {
|
||
changeLoginState(true);
|
||
}
|
||
|
||
}
|
||
|
||
public static String getLoginEntranceByType(String loginTag) {
|
||
String entrance = "";
|
||
switch (loginTag) {
|
||
case "qq":
|
||
entrance = "我的光环-QQ";
|
||
break;
|
||
case "wechat":
|
||
entrance = "我的光环-微信";
|
||
break;
|
||
case "weibo":
|
||
entrance = "我的光环-新浪微博";
|
||
break;
|
||
}
|
||
return entrance;
|
||
}
|
||
|
||
private void changeLoginState(boolean isLogin) {
|
||
if (isLogin) {
|
||
SignEntity signEntity = mDatabase.signDao().getSignEntityById(UserManager.getInstance().getUserId());
|
||
if (signEntity != null && !isCanSign(signEntity.getLastTime())) {
|
||
mPersonalSign.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.personal_sign_icon_yellow));
|
||
} else {
|
||
mPersonalSign.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.personal_sign_icon_white));
|
||
}
|
||
if (mUserInfoEntity != null) {
|
||
ImageUtils.displayIcon(mUserIcon, mUserInfoEntity.getIcon());
|
||
ImageUtils.displayIcon(mUserIconSmall, mUserInfoEntity.getIcon());
|
||
mPersonalUserName.setVisibility(View.VISIBLE);
|
||
mPersonalBadge.setVisibility(View.VISIBLE);
|
||
mPersonalHome.setVisibility(View.VISIBLE);
|
||
mPersonalLogin.setVisibility(View.GONE);
|
||
mPersonalUserName.setText(mUserInfoEntity.getName());
|
||
mPersonalUserNameSmall.setText(mUserInfoEntity.getName());
|
||
if (mUserInfoEntity.getAuth() != null) {
|
||
mUserBadge.setVisibility(View.VISIBLE);
|
||
ImageUtils.display(mUserBadge, mUserInfoEntity.getAuth().getIcon());
|
||
}
|
||
}
|
||
mUserHomeViewModel.setUserId(UserManager.getInstance().getUserId());
|
||
mUserHomeViewModel.getBadgeList();
|
||
mUserHomeViewModel.availableBadges();
|
||
} else {
|
||
mUserIcon.setImageURI("");
|
||
mUserIconSmall.setImageURI("");
|
||
mPersonalBadgeTv.setText("领取徽章");
|
||
mPersonalUserNameSmall.setText("立即登录");
|
||
mUserBadge.setVisibility(View.GONE);
|
||
mPersonalUserName.setVisibility(View.GONE);
|
||
mPersonalHome.setVisibility(View.GONE);
|
||
mPersonalBadge.setVisibility(View.GONE);
|
||
mPersonalLogin.setVisibility(View.VISIBLE);
|
||
|
||
if (mLoginMessageHint.getVisibility() == View.VISIBLE) {
|
||
mLoginMessageHint.setVisibility(View.GONE);
|
||
EventBus.getDefault().post(new EBReuse(MESSAGE_READ_OVER));
|
||
}
|
||
}
|
||
}
|
||
|
||
private void checkUpdate() {
|
||
String channel = HaloApp.getInstance().getChannel();
|
||
RetrofitManager.getInstance(getContext())
|
||
.getApi()
|
||
.getUpdate(PackageUtils.getVersionName(), PackageUtils.getVersionCode(), channel)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<AppEntity>() {
|
||
@Override
|
||
public void onResponse(AppEntity response) {
|
||
super.onResponse(response);
|
||
if (response.getVersionCode() > PackageUtils.getVersionCode()) {
|
||
notifyItemChange("设置", FunctionalMessageType.NEW_VERSION);
|
||
} else {
|
||
notifyItemChange("设置", null);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
private void notifyItemChange(String funcName, FunctionalMessageType msg) {
|
||
ArrayList<FunctionalGroupEntity> mEntityList = mPersonalFuncGroupAdapter.getMEntityList();
|
||
for (FunctionalGroupEntity mEntity : mEntityList) {
|
||
for (FunctionalLinkEntity linkEntity : mEntity.getAddons()) {
|
||
if (TextUtils.equals(linkEntity.getType(), funcName)) {
|
||
linkEntity.setMessage(msg);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
mPersonalFuncGroupAdapter.notifyDataSetChanged();
|
||
}
|
||
}
|