重构专题页面相关逻辑
其他toolbar逻辑移除
This commit is contained in:
@ -1,349 +1,29 @@
|
||||
package com.gh.gamecenter;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
||||
import com.gh.base.BaseActivity;
|
||||
import com.gh.base.OnRequestCallBackListener;
|
||||
import com.gh.common.util.CheckLoginUtils;
|
||||
import com.gh.common.util.DialogUtils;
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.gamecenter.adapter.VoteAdapter;
|
||||
import com.gh.gamecenter.entity.UserDataEntity;
|
||||
import com.gh.gamecenter.entity.VersionVoteEntity;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
import com.lightgame.utils.Util_System_Keyboard;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.HttpException;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
import com.halo.assistant.fragment.VoteFragment;
|
||||
import com.halo.assistant.ui.IntentFactory;
|
||||
|
||||
/**
|
||||
* Created by khy on 2017/4/11.
|
||||
* 求版本投票页面
|
||||
*/
|
||||
public class VoteActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,
|
||||
VoteAdapter.OnAddVoteListener, OnRequestCallBackListener<String> {
|
||||
|
||||
@BindView(R.id.vote_rv)
|
||||
RecyclerView mVoteRv;
|
||||
@BindView(R.id.vote_refresh)
|
||||
SwipeRefreshLayout mVoteRefresh;
|
||||
@BindView(R.id.vote_loading)
|
||||
ProgressBarCircularIndeterminate mVoteLoading;
|
||||
@BindView(R.id.reuse_no_connection)
|
||||
LinearLayout mNoConnection;
|
||||
@BindView(R.id.vote_add_tv)
|
||||
TextView mAddTv;
|
||||
|
||||
private VoteAdapter mAdapter;
|
||||
|
||||
private String mGameId;
|
||||
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mAdapter = new VoteAdapter(VoteActivity.this, VoteActivity.this, VoteActivity.this, mGameId);
|
||||
mVoteRv.setAdapter(mAdapter);
|
||||
}
|
||||
};
|
||||
private LinearLayoutManager layoutManager;
|
||||
|
||||
@Deprecated
|
||||
public class VoteActivity extends CommonActivity {
|
||||
|
||||
@NonNull
|
||||
public static Intent getIntent(Context context, String gameName, String gameId) {
|
||||
Intent intent = new Intent(context, VoteActivity.class);
|
||||
intent.putExtra(EntranceUtils.KEY_GAMENAME, gameName);
|
||||
intent.putExtra(EntranceUtils.KEY_GAMEID, gameId);
|
||||
return intent;
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(VoteActivity.class)
|
||||
.setFragment(VoteFragment.class).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.activity_vote;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
String gameName = getIntent().getExtras().getString(EntranceUtils.KEY_GAMENAME);
|
||||
mGameId = getIntent().getExtras().getString(EntranceUtils.KEY_GAMEID);
|
||||
|
||||
setNavigationTitle(getString(R.string.title_vote_formatable, gameName));
|
||||
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
mVoteRv.setLayoutManager(layoutManager);
|
||||
mAdapter = new VoteAdapter(this, this, this, mGameId);
|
||||
mVoteRv.setAdapter(mAdapter);
|
||||
|
||||
mVoteRefresh.setColorSchemeResources(R.color.theme);
|
||||
mVoteRefresh.setOnRefreshListener(this);
|
||||
|
||||
mAddTv.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
CheckLoginUtils.checkLogin(VoteActivity.this, new CheckLoginUtils.OnLoggenInListener() {
|
||||
@Override
|
||||
public void onLoggedIn() {
|
||||
showAddVoteDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
mVoteRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
||||
super.onScrollStateChanged(recyclerView, newState);
|
||||
if (newState == RecyclerView.SCROLL_STATE_IDLE && mAdapter.isLoaded() && !mAdapter.isRemove()
|
||||
&& mAdapter.getItemCount() == layoutManager.findLastVisibleItemPosition() + 1) {
|
||||
mAdapter.getVersionVote(mAdapter.getItemCount() - 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadDone() {
|
||||
mVoteRefresh.setRefreshing(false);
|
||||
mVoteLoading.setVisibility(View.GONE);
|
||||
mVoteRv.setVisibility(View.VISIBLE);
|
||||
mAddTv.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadDone(final String obj) {
|
||||
CheckLoginUtils.checkLogin(VoteActivity.this, new CheckLoginUtils.OnLoggenInListener() {
|
||||
@Override
|
||||
public void onLoggedIn() {
|
||||
postVersionVote(obj, false); // 投票
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadError() {
|
||||
mVoteRefresh.setRefreshing(false);
|
||||
mNoConnection.setVisibility(View.VISIBLE);
|
||||
mVoteRv.setVisibility(View.GONE);
|
||||
mAddTv.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadEmpty() {
|
||||
mVoteRefresh.setRefreshing(false);
|
||||
mAddTv.setVisibility(View.VISIBLE);
|
||||
mVoteLoading.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private void showAddVoteDialog() {
|
||||
final Dialog dialog = new Dialog(VoteActivity.this);
|
||||
|
||||
View view = View.inflate(VoteActivity.this, R.layout.dialog_modify_nickname, null);
|
||||
|
||||
TextView title = (TextView) view.findViewById(R.id.dialog_nickname_title);
|
||||
title.setText(R.string.vote_input_hint);
|
||||
|
||||
final EditText mEdtInput = (EditText) view.findViewById(R.id.dialog_nickname_input);
|
||||
mEdtInput.setHint("");
|
||||
mEdtInput.setSelection(mEdtInput.getText().length());
|
||||
|
||||
mEdtInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||
String nickname = mEdtInput.getText().toString().trim();
|
||||
if (TextUtils.isEmpty(nickname)) {
|
||||
Utils.toast(VoteActivity.this, getString(R.string.vote_empty_hint));
|
||||
return true;
|
||||
}
|
||||
postVersionVote(nickname, true);
|
||||
dialog.dismiss();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
// 取消按钮
|
||||
TextView cancel = (TextView) view.findViewById(R.id.dialog_nickname_cancel);
|
||||
cancel.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
// 确定按钮
|
||||
TextView confirm = (TextView) view.findViewById(R.id.dialog_nickname_confirm);
|
||||
confirm.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String nickname = mEdtInput.getText().toString().trim();
|
||||
if (TextUtils.isEmpty(nickname)) {
|
||||
Utils.toast(VoteActivity.this, getString(R.string.vote_empty_hint));
|
||||
return;
|
||||
}
|
||||
postVersionVote(nickname, true);
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
Util_System_Keyboard.hideSoftKeyboard(VoteActivity.this);
|
||||
}
|
||||
});
|
||||
|
||||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
dialog.setContentView(view);
|
||||
dialog.show();
|
||||
|
||||
mVoteRv.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Util_System_Keyboard.showSoftKeyboard(VoteActivity.this, mEdtInput);
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
private void postVersionVote(final String name, final boolean isNewVote) {
|
||||
if (isNewVote) {
|
||||
for (VersionVoteEntity voteEntity : mAdapter.getDataList()) {
|
||||
if (name.equals(voteEntity.getName())) {
|
||||
UserDataEntity userDataEntity = voteEntity.getUserData();
|
||||
if (userDataEntity != null && userDataEntity.isVersionRequested()) {
|
||||
Utils.toast(this, getString(R.string.vote_success));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final Dialog waitDialog = DialogUtils.showWaitDialog(VoteActivity.this, getString(R.string.vote_post));
|
||||
|
||||
JSONObject object = new JSONObject();
|
||||
try {
|
||||
object.put("name", name);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), object.toString());
|
||||
|
||||
RetrofitManager.getInstance(this).getApi()
|
||||
.postVersionVote(body, mGameId)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
waitDialog.dismiss();
|
||||
|
||||
try {
|
||||
String string = response.string();
|
||||
JSONObject responseObject = new JSONObject(string);
|
||||
boolean cast = responseObject.getBoolean("cast");
|
||||
String id = responseObject.getString("_id");
|
||||
if (cast) {
|
||||
Utils.toast(VoteActivity.this, getString(R.string.vote_success));
|
||||
} else {
|
||||
if (isNewVote) {
|
||||
Utils.toast(VoteActivity.this, "已经存在相同的选项");
|
||||
} else {
|
||||
Utils.toast(VoteActivity.this, "你已经投过了");
|
||||
}
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(id)) {
|
||||
mAdapter.voteCallBack(cast, isNewVote, id, name);
|
||||
|
||||
if (isNewVote) {
|
||||
mVoteRv.scrollToPosition(mAdapter.getItemCount() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (e != null && e.code() == 403) {
|
||||
try {
|
||||
String string = e.response().errorBody().string();
|
||||
JSONObject errorJson = new JSONObject(string);
|
||||
String detail = errorJson.getString("detail");
|
||||
if ("illegal".equals(detail)) {
|
||||
Utils.toast(VoteActivity.this, getString(R.string.vote_illegal_hint));
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
toast(getString(R.string.post_failure_hint));
|
||||
}
|
||||
|
||||
waitDialog.dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@OnClick(R.id.reuse_no_connection)
|
||||
public void reconnection() { // 重新连接
|
||||
mVoteRv.setVisibility(View.VISIBLE);
|
||||
mVoteLoading.setVisibility(View.VISIBLE);
|
||||
mNoConnection.setVisibility(View.GONE);
|
||||
mVoteRefresh.postDelayed(runnable, 1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
mVoteRefresh.postDelayed(runnable, 1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVote() {
|
||||
CheckLoginUtils.checkLogin(this, new CheckLoginUtils.OnLoggenInListener() {
|
||||
@Override
|
||||
public void onLoggedIn() {
|
||||
showAddVoteDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user