303 lines
11 KiB
Java
303 lines
11 KiB
Java
package com.gh.gamecenter.servers;
|
|
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.recyclerview.widget.DefaultItemAnimator;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
import com.gh.base.fragment.BaseFragment;
|
|
import com.gh.common.exposure.ExposureListener;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.MtaHelper;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.entity.ServerCalendarEntity;
|
|
import com.gh.gamecenter.eventbus.EBDownloadStatus;
|
|
import com.lightgame.download.DataWatcher;
|
|
import com.lightgame.download.DownloadEntity;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
/**
|
|
* Created by khy on 18/08/17.
|
|
*/
|
|
public class GameServersContentFragment extends BaseFragment {
|
|
|
|
@BindView(R.id.kaifu_rv)
|
|
RecyclerView mRecyclerView;
|
|
@BindView(R.id.kaifu_item_time_tv)
|
|
TextView mKaifuItemTimeTv;
|
|
@BindView(R.id.kaifu_item_header)
|
|
ImageView mKaifuItemHeaderIv;
|
|
@BindView(R.id.kaifu_item_time)
|
|
LinearLayout mKaiFuTimeLl;
|
|
@BindView(R.id.reuse_ll_loading)
|
|
View mLoading;
|
|
@BindView(R.id.reuse_no_connection)
|
|
LinearLayout mNoConn;
|
|
@BindView(R.id.reuse_none_data)
|
|
LinearLayout mNoneData;
|
|
@BindView(R.id.remove_filter)
|
|
View mRemoveFilter;
|
|
|
|
private GameServersContentAdapter mAdapter;
|
|
private LinearLayoutManager mLayoutManager;
|
|
|
|
private RelativeLayout.LayoutParams mLlParams;
|
|
|
|
private String mDay;
|
|
private String mHour;
|
|
private String mTags;
|
|
private String mType;
|
|
|
|
private boolean mIsRefreshDownload = true;
|
|
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
ArrayList<Integer> locationList = mAdapter.getLocationMap().get(downloadEntity.getPackageName());
|
|
if (locationList != null && mIsRefreshDownload) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = mAdapter.getDataList().get(location);
|
|
if (gameEntity != null) {
|
|
DownloadItemUtils.processDate(getContext(), gameEntity, downloadEntity,
|
|
mAdapter, location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_kaifu_vp;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
if (getArguments() != null) {
|
|
mType = getArguments().getString(EntranceUtils.KEY_TYPE, "开服");
|
|
}
|
|
|
|
mLlParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
((DefaultItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
mLayoutManager = new LinearLayoutManager(getContext());
|
|
mRecyclerView.setLayoutManager(mLayoutManager);
|
|
mAdapter = new GameServersContentAdapter(getContext(), this, this::scrollToTop,
|
|
mEntrance,
|
|
mType,
|
|
mDay,
|
|
mHour,
|
|
mTags);
|
|
mRecyclerView.addOnScrollListener(new ExposureListener(this, mAdapter));
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
|
|
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (mLayoutManager.findLastVisibleItemPosition() == mAdapter.getItemCount() - 1
|
|
&& newState == RecyclerView.SCROLL_STATE_IDLE && !mAdapter.isRemove() && mAdapter.isLoaded()) {
|
|
mAdapter.addList();
|
|
}
|
|
|
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
|
mIsRefreshDownload = true;
|
|
} else {
|
|
mIsRefreshDownload = false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
int position = mLayoutManager.findFirstVisibleItemPosition();
|
|
if (RecyclerView.NO_POSITION == position) return;
|
|
|
|
List<GameEntity> dataList = mAdapter.getDataList();
|
|
GameEntity gameEntity = dataList.get(position + 1);
|
|
Long kaifuTimeHint = gameEntity.getKaifuTimeHint();
|
|
|
|
// 悬挂的文案
|
|
GameEntity timeGameEntity = dataList.get(position);
|
|
boolean fixedTopHint = timeGameEntity.getFixedTop();
|
|
|
|
if (fixedTopHint) {
|
|
mKaifuItemTimeTv.setText("热门开服");
|
|
mKaifuItemTimeTv.setTag(timeGameEntity.getKaifuTimeHint());
|
|
mKaifuItemHeaderIv.setImageResource(R.drawable.ic_kaifu_hot);
|
|
} else {
|
|
Long timeHint = timeGameEntity.getKaifuTimeHint();
|
|
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd (EEEE) HH:mm", Locale.CHINA);
|
|
if (timeHint == null || timeHint == 0) {
|
|
ServerCalendarEntity serverEntity = timeGameEntity.getServerEntity();
|
|
if (serverEntity != null) {
|
|
mKaifuItemTimeTv.setText(dateFm.format(serverEntity.getTime() * 1000));
|
|
mKaifuItemHeaderIv.setImageResource(R.drawable.kaifu_time_icon);
|
|
mKaifuItemTimeTv.setTag(serverEntity.getTime());
|
|
}
|
|
} else {
|
|
mKaifuItemTimeTv.setText(dateFm.format(timeHint * 1000));
|
|
mKaifuItemHeaderIv.setImageResource(R.drawable.kaifu_time_icon);
|
|
mKaifuItemTimeTv.setTag(timeHint);
|
|
}
|
|
}
|
|
|
|
// 悬挂界面移动
|
|
if (kaifuTimeHint != null && kaifuTimeHint != 0) {
|
|
int buttom = mLayoutManager.findViewByPosition(position).getBottom();
|
|
if (buttom <= mKaiFuTimeLl.getHeight()) {
|
|
mLlParams.topMargin = buttom - mKaiFuTimeLl.getHeight();
|
|
} else {
|
|
mLlParams.topMargin = 0;
|
|
}
|
|
} else {
|
|
mLlParams.topMargin = 0;
|
|
}
|
|
mKaiFuTimeLl.setLayoutParams(mLlParams);
|
|
}
|
|
});
|
|
|
|
mRemoveFilter.setOnClickListener(v -> {
|
|
Fragment parentFragment = getParentFragment();
|
|
if (parentFragment instanceof GameServersTestFragment) {
|
|
MtaHelper.onEvent("开服表", "开测Tab", "查看全部");
|
|
((GameServersTestFragment) parentFragment).resetFilter();
|
|
} else if (parentFragment instanceof GameServersPublishFragment) {
|
|
MtaHelper.onEvent("开服表", "开服Tab", "查看全部");
|
|
((GameServersPublishFragment) parentFragment).resetFilter();
|
|
}
|
|
});
|
|
}
|
|
|
|
public void filterData(String day, String hour, String type, String tags) {
|
|
mDay = day;
|
|
mHour = hour;
|
|
mType = type;
|
|
mTags = tags;
|
|
onRefreshPage();
|
|
}
|
|
|
|
public void vpScrollState(int status) {
|
|
if (status == ViewPager.SCROLL_STATE_IDLE) {
|
|
mIsRefreshDownload = true;
|
|
} else if (status == ViewPager.SCROLL_STATE_DRAGGING) {
|
|
mIsRefreshDownload = false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (mAdapter != null) {
|
|
mAdapter.notifyDataSetChanged();
|
|
DownloadManager.getInstance(getContext()).addObserver(dataWatcher);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
DownloadManager.getInstance(getContext()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@OnClick(R.id.reuse_no_connection)
|
|
public void onRefreshPage() {
|
|
mLoading.setVisibility(View.VISIBLE);
|
|
mNoConn.setVisibility(View.GONE);
|
|
mNoneData.setVisibility(View.GONE);
|
|
mKaiFuTimeLl.setVisibility(View.GONE);
|
|
mAdapter = new GameServersContentAdapter(getContext(), this, this::scrollToTop,
|
|
mEntrance,
|
|
mType,
|
|
mDay,
|
|
mHour,
|
|
mTags);
|
|
mRecyclerView.setAdapter(mAdapter);
|
|
mRecyclerView.addOnScrollListener(new ExposureListener(this, mAdapter));
|
|
}
|
|
|
|
// 下载被删除事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBDownloadStatus status) {
|
|
if ("delete".equals(status.getStatus())) {
|
|
DownloadManager.getInstance(getContext()).removePlatform(status.getName(), status.getPlatform());
|
|
|
|
ArrayList<Integer> locationList = mAdapter.getLocationMap().get(status.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = mAdapter.getDataList().get(location);
|
|
if (gameEntity != null && gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(status.getPlatform());
|
|
}
|
|
mAdapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
super.loadDone();
|
|
mKaiFuTimeLl.setVisibility(View.VISIBLE);
|
|
mNoConn.setVisibility(View.GONE);
|
|
mNoneData.setVisibility(View.GONE);
|
|
mLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
if (!isAdded()) return;
|
|
|
|
super.loadError();
|
|
toast(R.string.loading_failed_hint);
|
|
mKaiFuTimeLl.setVisibility(View.GONE);
|
|
mNoConn.setVisibility(View.VISIBLE);
|
|
mNoneData.setVisibility(View.GONE);
|
|
mLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
super.loadEmpty();
|
|
mKaiFuTimeLl.setVisibility(View.GONE);
|
|
mNoConn.setVisibility(View.GONE);
|
|
mNoneData.setVisibility(View.VISIBLE);
|
|
mLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
private void scrollToTop() {
|
|
mLayoutManager.scrollToPosition(0);
|
|
mBaseHandler.postDelayed(() -> {
|
|
mLlParams.topMargin = 0;
|
|
mKaiFuTimeLl.setLayoutParams(mLlParams);
|
|
}, 10);
|
|
}
|
|
}
|