316 lines
13 KiB
Java
316 lines
13 KiB
Java
package com.gh.gamecenter.ask;
|
|
|
|
import android.app.Activity;
|
|
import android.app.Dialog;
|
|
import android.os.Bundle;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.view.Window;
|
|
import android.widget.ProgressBar;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.AskErrorResponseUtils;
|
|
import com.gh.common.util.CheckLoginUtils;
|
|
import com.gh.common.util.PackageUtils;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.adapter.AskSelectGameAdapter;
|
|
import com.gh.gamecenter.ask.entity.AskGameSelectEntity;
|
|
import com.gh.gamecenter.ask.entity.CommunitiesGameEntity;
|
|
import com.gh.gamecenter.ask.entity.MeEntity;
|
|
import com.gh.gamecenter.baselist.ListFragment;
|
|
import com.gh.gamecenter.baselist.LoadType;
|
|
import com.gh.gamecenter.db.info.InstallInfo;
|
|
import com.gh.gamecenter.entity.InstallGameEntity;
|
|
import com.gh.gamecenter.entity.SettingsEntity;
|
|
import com.gh.gamecenter.eventbus.EBReuse;
|
|
import com.gh.gamecenter.manager.UserManager;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.halo.assistant.HaloApp;
|
|
import com.lightgame.utils.Util_System_Phone_State;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import okhttp3.MediaType;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.ResponseBody;
|
|
import retrofit2.HttpException;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
/**
|
|
* Created by khy on 11/12/17.
|
|
*/
|
|
|
|
public class SelectGameFragment extends ListFragment<AskGameSelectEntity, SelectGameViewModel> {
|
|
|
|
@BindView(R.id.ask_selectgame_rv_title)
|
|
TextView mRvTitle;
|
|
|
|
private AskSelectGameAdapter mAdapter;
|
|
private RelativeLayout.LayoutParams mParams;
|
|
|
|
@Override
|
|
protected AskSelectGameAdapter provideListAdapter() {
|
|
return mAdapter == null ? mAdapter = new AskSelectGameAdapter(getContext(), this) : mAdapter;
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.fragment_ask_selectgame;
|
|
}
|
|
|
|
@Override
|
|
protected RecyclerView.ItemDecoration getItemDecoration() {
|
|
return new VerticalItemDecoration(getContext(), 1, true);
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
setNavigationTitle(getString(R.string.title_select_game));
|
|
|
|
mParams = (RelativeLayout.LayoutParams) mRvTitle.getLayoutParams();
|
|
mListRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
|
|
int position = mLayoutManager.findFirstVisibleItemPosition();
|
|
if (position == mAdapter.getTitlePosition()) {
|
|
mRvTitle.setText("已开通的游戏");
|
|
int buttom = mLayoutManager.findViewByPosition(position).getBottom();
|
|
if (buttom <= mRvTitle.getHeight()) {
|
|
mParams.topMargin = buttom - mRvTitle.getHeight();
|
|
mRvTitle.setLayoutParams(mParams);
|
|
} else {
|
|
mParams.topMargin = 0;
|
|
mRvTitle.setLayoutParams(mParams);
|
|
}
|
|
} else {
|
|
mParams.topMargin = 0;
|
|
mRvTitle.setLayoutParams(mParams);
|
|
if (position > mAdapter.getTitlePosition()) {
|
|
mRvTitle.setText("投票中的游戏");
|
|
} else {
|
|
mRvTitle.setText("已开通的游戏");
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onListClick(View view, int position, Object data) {
|
|
super.onListClick(view, position, data);
|
|
switch (view.getId()) {
|
|
case R.id.ask_selectgame_item_constraintlayout:
|
|
AskGameSelectEntity entity = (AskGameSelectEntity) data;
|
|
if ("opened".equals(entity.getStatus())) {
|
|
UserManager.getInstance().setCommunityId(getContext(), entity.getId(), entity.getName());
|
|
mAdapter.resetCommunityId(entity.getId());
|
|
if (getActivity() != null) {
|
|
getActivity().setResult(Activity.RESULT_OK);
|
|
getActivity().finish();
|
|
}
|
|
}
|
|
break;
|
|
case R.id.footerview_item:
|
|
if (mAdapter.isNetworkError()) {
|
|
mListViewModel.load(LoadType.RETRY);
|
|
} else if (mAdapter.isOver()) {
|
|
CheckLoginUtils.checkLogin(getContext(), this::showSelectDialog);
|
|
}
|
|
|
|
break;
|
|
case R.id.ask_selectgame_item_votebtn:
|
|
CheckLoginUtils.checkLogin(getContext(), () -> {
|
|
AskGameSelectEntity askGameSelectEntity = (AskGameSelectEntity) data;
|
|
MeEntity me = askGameSelectEntity.getMe();
|
|
if (me == null || !me.isCommunityVoted())
|
|
vote(askGameSelectEntity.getId());
|
|
|
|
|
|
});
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
private void showSelectDialog() {
|
|
Dialog selectGameDialog = new Dialog(getContext());
|
|
|
|
View view = View.inflate(getContext(), R.layout.dialog_suggest_select_game, null);
|
|
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dialog_suggest_game_rv);
|
|
TextView back = (TextView) view.findViewById(R.id.dialog_suggest_game_back);
|
|
ProgressBar pb = (ProgressBar) view.findViewById(R.id.dialog_suggest_game_load);
|
|
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
|
|
recyclerView.setAdapter(new SelectGameDialogAdapter(getContext(), new SelectGameDialogAdapter.OnGameSelectListener() {
|
|
@Override
|
|
public void onSelectGame(InstallInfo installInfo) {
|
|
selectGameDialog.dismiss();
|
|
postCommunities(installInfo);
|
|
}
|
|
|
|
@Override
|
|
public void onSelectApp(InstallGameEntity installInfo) {
|
|
sendSuggestion(installInfo);
|
|
selectGameDialog.dismiss();
|
|
}
|
|
}, pb));
|
|
|
|
selectGameDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
selectGameDialog.setCanceledOnTouchOutside(false);
|
|
selectGameDialog.setContentView(view);
|
|
selectGameDialog.show();
|
|
|
|
back.setOnClickListener((v) -> {
|
|
selectGameDialog.cancel();
|
|
});
|
|
}
|
|
|
|
private void sendSuggestion(InstallGameEntity entity) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("message", "问答社区推荐收录:" + entity.getGameName() + "(" + entity.getPackageName() + ", " + entity.getGameVersion() + ")");
|
|
params.put("from", "");
|
|
params.put("ghversion", PackageUtils.getPatchVersionName());
|
|
params.put("channel", HaloApp.getInstance().getChannel());
|
|
params.put("type", android.os.Build.MODEL);
|
|
params.put("sdk", String.valueOf(android.os.Build.VERSION.SDK_INT));
|
|
params.put("version", android.os.Build.VERSION.RELEASE);
|
|
params.put("source", getString(R.string.app_name));
|
|
params.put("suggestion_type", "游戏收录");
|
|
params.put("imei", Util_System_Phone_State.getDeviceId(getActivity()));
|
|
|
|
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
|
new JSONObject(params).toString());
|
|
|
|
RetrofitManager.getInstance(getContext()).getApi().postSuggestion(body)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<ResponseBody>() {
|
|
@Override
|
|
public void onResponse(ResponseBody response) {
|
|
toast("提交成功");
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
toast("提交失败,请检查网络状态");
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void postCommunities(InstallInfo installInfo) {
|
|
JSONObject object = new JSONObject();
|
|
try {
|
|
object.put("game_id", installInfo.getId());
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
RequestBody body = RequestBody.create(MediaType.parse("application/json"), object.toString());
|
|
|
|
RetrofitManager.getInstance(getContext()).getApi()
|
|
.postCommunities(body)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<ResponseBody>() {
|
|
@Override
|
|
public void onResponse(ResponseBody response) {
|
|
super.onResponse(response);
|
|
toast(R.string.vote_success);
|
|
try {
|
|
JSONObject jsonResponse = new JSONObject(response.string());
|
|
String status = jsonResponse.getString("status");
|
|
if ("show".equals(status)) {
|
|
String id = jsonResponse.getString("_id");
|
|
AskGameSelectEntity entity = new AskGameSelectEntity();
|
|
CommunitiesGameEntity communitiesGameEntity = new CommunitiesGameEntity();
|
|
communitiesGameEntity.setId(installInfo.getId());
|
|
communitiesGameEntity.setIcon(installInfo.getIcon());
|
|
communitiesGameEntity.setName(installInfo.getGameName());
|
|
MeEntity meEntity = new MeEntity();
|
|
meEntity.setCommunityVoted(true);
|
|
entity.setId(id);
|
|
entity.setVote(1);
|
|
entity.setStatus("voting");
|
|
entity.setMe(meEntity);
|
|
entity.setGame(communitiesGameEntity);
|
|
mAdapter.postCommunitiesSuccess(entity);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
super.onFailure(e);
|
|
// toast(R.string.vote_failure);
|
|
AskErrorResponseUtils.errorResponseControl(getContext(), e);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void vote(final String communityId) {
|
|
RetrofitManager.getInstance(getContext()).getApi()
|
|
.communitiesVote(communityId)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<ResponseBody>() {
|
|
@Override
|
|
public void onResponse(ResponseBody response) {
|
|
super.onResponse(response);
|
|
try {
|
|
String string = response.string();
|
|
JSONObject object = new JSONObject(string);
|
|
int vote = object.getInt("vote");
|
|
if (vote == 200) {
|
|
|
|
}
|
|
toast(R.string.vote_success);
|
|
mAdapter.voteSuccess(communityId);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
super.onFailure(e);
|
|
// toast(R.string.vote_failure);
|
|
AskErrorResponseUtils.errorResponseControl(getContext(), e);
|
|
}
|
|
});
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBReuse reuse) {
|
|
if ("Refresh".equals(reuse.getType()) && TextUtils.isEmpty(provideListAdapter().getCommunityId())) {
|
|
SettingsEntity settings = Config.getSettings();
|
|
if (settings != null && settings.getCommunity() != null) {
|
|
SettingsEntity.Community community = settings.getCommunity();
|
|
UserManager.getInstance().setCommunityId(getContext(), community.getId(), community.getName());
|
|
mAdapter.resetCommunityId(community.getId());
|
|
mAdapter.notifyDataSetChanged();
|
|
}
|
|
}
|
|
}
|
|
}
|