307 lines
11 KiB
Java
307 lines
11 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.os.Bundle;
|
|
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.*;
|
|
import android.view.inputmethod.EditorInfo;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.*;
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.*;
|
|
import com.gh.gamecenter.adapter.VoteAdapter;
|
|
import com.gh.gamecenter.db.VersionVoteDao;
|
|
import com.gh.gamecenter.db.info.VersionVoteInfo;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import okhttp3.*;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import retrofit2.adapter.rxjava.HttpException;
|
|
import rx.Observable;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.functions.Func1;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
/**
|
|
* Created by khy on 2017/4/11.
|
|
* 求版本投票页面
|
|
*/
|
|
public class VoteActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, VoteAdapter.OnAddVoteListener {
|
|
|
|
@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 VersionVoteDao mVoteDao;
|
|
|
|
private String mGameId;
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mAdapter = new VoteAdapter(VoteActivity.this, VoteActivity.this, VoteActivity.this, mGameId, mVoteDao);
|
|
mVoteRv.setAdapter(mAdapter);
|
|
}
|
|
};
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
String gameName = getIntent().getExtras().getString("gameName");
|
|
mGameId = getIntent().getExtras().getString("gameId");
|
|
|
|
mVoteDao = new VersionVoteDao(this);
|
|
|
|
View view = View.inflate(this, R.layout.activity_vote, null);
|
|
init(view, gameName + " - 求版本");
|
|
|
|
layoutManager = new LinearLayoutManager(this);
|
|
mVoteRv.setLayoutManager(layoutManager);
|
|
mAdapter = new VoteAdapter(this, this, this, mGameId, mVoteDao);
|
|
mVoteRv.setAdapter(mAdapter);
|
|
|
|
mVoteRefresh.setColorSchemeResources(R.color.theme);
|
|
mVoteRefresh.setOnRefreshListener(this);
|
|
|
|
mAddTv.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
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("输入选项名字");
|
|
|
|
final EditText input = (EditText) view.findViewById(R.id.dialog_nickname_input);
|
|
input.setHint("");
|
|
input.setSelection(input.getText().length());
|
|
|
|
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
@Override
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
|
String nickname = input.getText().toString().trim();
|
|
if (TextUtils.isEmpty(nickname)) {
|
|
Toast.makeText(VoteActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();
|
|
return true;
|
|
}
|
|
|
|
dialog.dismiss();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
});
|
|
|
|
// 取消按钮
|
|
TextView cancel = (TextView) view.findViewById(R.id.dialog_nickname_cannel);
|
|
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 = input.getText().toString().trim();
|
|
if (TextUtils.isEmpty(nickname)) {
|
|
Toast.makeText(VoteActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();
|
|
return;
|
|
}
|
|
postVersionVote(nickname, true, true);
|
|
dialog.dismiss();
|
|
}
|
|
});
|
|
|
|
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
@Override
|
|
public void onDismiss(DialogInterface dialog) {
|
|
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
|
|
}
|
|
});
|
|
|
|
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
dialog.setContentView(view);
|
|
dialog.show();
|
|
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
sleep(300);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
InputMethodManager imm = (InputMethodManager) getApplicationContext()
|
|
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.showSoftInput(input, InputMethodManager.SHOW_FORCED);
|
|
}
|
|
}.start();
|
|
}
|
|
|
|
private void postVersionVote(final String name, final boolean isCheck, final boolean isNewVote) {
|
|
final Dialog waitDialog = DialogUtils.showWaitDialog(VoteActivity.this, "提交中...");
|
|
|
|
JSONObject object = new JSONObject();
|
|
try {
|
|
object.put("name", name);
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
Utils.log("=====object" + object.toString());
|
|
|
|
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), object.toString());
|
|
|
|
TokenUtils
|
|
.getToken(VoteActivity.this, isCheck)
|
|
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
|
@Override
|
|
public Observable<ResponseBody> call(String token) {
|
|
return RetrofitManager.getApi().postVersionVote(token, 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();
|
|
Utils.log("========提交成功" + string);
|
|
JSONObject responseObject = new JSONObject(string);
|
|
boolean cast = responseObject.getBoolean("cast");
|
|
String id = responseObject.getString("_id");
|
|
|
|
if (cast) {
|
|
Utils.toast(VoteActivity.this, "投票成功");
|
|
} else {
|
|
Utils.toast(VoteActivity.this, "你已经投过了");
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(id)) {
|
|
mVoteDao.add(new VersionVoteInfo(id));
|
|
mAdapter.voteCallBack(cast, isNewVote, id, name);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
super.onFailure(e);
|
|
Utils.toast(VoteActivity.this, "提交失败");
|
|
waitDialog.dismiss();
|
|
|
|
if (e != null) {
|
|
if (e.code() == 401) {
|
|
postVersionVote(name, false, isNewVote);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@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 loadDone() {
|
|
super.loadDone();
|
|
mVoteRefresh.setRefreshing(false);
|
|
mVoteLoading.setVisibility(View.GONE);
|
|
mVoteRv.setVisibility(View.VISIBLE);
|
|
mAddTv.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadDone(Object obj) {
|
|
super.loadDone(obj);
|
|
postVersionVote((String) obj, true, false); // 投票
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
super.loadError();
|
|
mVoteRefresh.setRefreshing(false);
|
|
mNoConnection.setVisibility(View.VISIBLE);
|
|
mVoteRv.setVisibility(View.GONE);
|
|
mAddTv.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void loadEmpty() {
|
|
super.loadEmpty();
|
|
mVoteRefresh.setRefreshing(false);
|
|
mAddTv.setVisibility(View.VISIBLE);
|
|
mVoteLoading.setVisibility(View.GONE);
|
|
}
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
mVoteRefresh.postDelayed(runnable, 1000);
|
|
}
|
|
|
|
@Override
|
|
public void addVote() {
|
|
showAddVoteDialog();
|
|
}
|
|
}
|