package com.tejia.lijin.app.ui.gold.view;
|
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import androidx.databinding.DataBindingUtil;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.os.Bundle;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.Toast;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.databinding.ActivityGoldExchangeBinding;
|
import com.tejia.lijin.app.databinding.ItemRecyclerviewBottom3Binding;
|
import com.tejia.lijin.app.entity.GoldExchange;
|
import com.tejia.lijin.app.entity.GoldExchangeState;
|
import com.tejia.lijin.app.ui.dialog.GoldExchangeNotEnoughDialog;
|
import com.tejia.lijin.app.ui.dialog.GoldExchangeStateDialog;
|
import com.tejia.lijin.app.ui.gold.base.BaseMVPActivity;
|
import com.tejia.lijin.app.ui.gold.contract.GoldExchangeContract;
|
import com.tejia.lijin.app.ui.gold.presenter.GoldExchangePresenter;
|
import com.tejia.lijin.app.ui.goldtask.GoldTaskActivity;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 金币兑换Activity
|
*/
|
public class GoldExchangeActivity extends BaseMVPActivity<GoldExchangePresenter> implements View.OnClickListener, GoldExchangeContract.goldExchageInfo {
|
|
ActivityGoldExchangeBinding binding;
|
ItemRecyclerviewBottom3Binding bottomBinding;
|
GoldExchangeAdapter adapter;
|
List<GoldExchange> mList;
|
int page = 1;
|
int count = 0;
|
boolean isLoad = true;
|
boolean isTitleInput = false;
|
|
@Override
|
protected void initActivityView(Bundle savedInstanceState) {
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_gold_exchange);
|
}
|
|
@Override
|
protected void initView() {
|
TopStatusSettings.setStatusView(this, binding.includeStatusBar.vStatusBar);
|
binding.includeTopBar.tvTopBarMiddle.setText("金币兑换");
|
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
|
binding.rvGoldExchange.setLayoutManager(manager);
|
DividerItemDecoration decoration = new DividerItemDecoration(DividerItemDecoration.VERTICAL);
|
decoration.setSize(1);
|
binding.rvGoldExchange.addItemDecoration(decoration);
|
|
mList = new ArrayList<>();
|
adapter = new GoldExchangeAdapter(this, mList, mPresenter);
|
adapter.setHasStableIds(true);
|
binding.rvGoldExchange.setAdapter(adapter);
|
|
bottomBinding = DataBindingUtil.inflate(LayoutInflater.from(this),
|
R.layout.item_recyclerview_bottom3, null, false);
|
adapter.addFooterView(bottomBinding.getRoot());
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
bottomBinding.ivLoading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
bottomBinding.getRoot().setVisibility(View.GONE);
|
binding.llAvailableGold.setVisibility(View.GONE);
|
|
isTitleInput = Boolean.parseBoolean(getIntent().getStringExtra("balanceMore"));
|
binding.tvAvailableGold.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
isTitleInput ? R.drawable.ic_gold_exchange_input : 0, 0);
|
binding.includeTopBar.tvTopBarLeft.setOnClickListener(this);
|
binding.llAvailableGold.setOnClickListener(this);
|
binding.srlGoldExchange.setColorSchemeColors(getResources().getColor(R.color.theme));
|
binding.rvGoldExchange.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int last = layoutManager.findLastVisibleItemPosition();
|
int total = layoutManager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (last == total - 1 && mList.size() < count && isLoad) {
|
page++;
|
isLoad = false;
|
bottomBinding.ivLoading.setVisibility(View.VISIBLE);
|
bottomBinding.tvLoading.setText("正在加载更多是数据");
|
mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
|
}
|
}
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
}
|
});
|
binding.srlGoldExchange.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
|
}
|
});
|
mPresenter.getGoldExchangeList(GoldExchangeActivity.this, page);
|
}
|
|
@Override
|
protected GoldExchangePresenter createPresenter() {
|
return new GoldExchangePresenter(this);
|
}
|
|
@Override
|
public void onGainListDataSuccess(JSONObject jsonObject) {
|
isLoad = true;
|
binding.srlGoldExchange.setRefreshing(false);
|
if (jsonObject.optInt("code") == 0) {
|
binding.llAvailableGold.setVisibility(View.VISIBLE);
|
count = jsonObject.optJSONObject("data").optInt("count");
|
binding.tvAvailableGold.setText(jsonObject.optJSONObject("data").optString("goldCoin"));
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<GoldExchange> list = gson.fromJson(jsonObject
|
.optJSONObject("data").optJSONArray("list").toString(),
|
new TypeToken<List<GoldExchange>>() {
|
}.getType());
|
if (page == 1) {
|
mList.clear();
|
}
|
mList.addAll(list);
|
adapter.notifyDataSetChanged();
|
|
if (mList.size() == count) {
|
bottomBinding.getRoot().setVisibility(View.VISIBLE);
|
bottomBinding.ivLoading.setVisibility(View.GONE);
|
bottomBinding.tvLoading.setText("更多金币兑换,敬请期待^_^");
|
}
|
} else {
|
Toast.makeText(GoldExchangeActivity.this,
|
jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onGainListDataFailure(String errorStr) {
|
binding.srlGoldExchange.setRefreshing(false);
|
bottomBinding.getRoot().setVisibility(View.GONE);
|
isLoad = true;
|
Log.e("GoldExchangeActivity", "onGainListDataFailure---" + errorStr);
|
}
|
|
@Override
|
public void onGoExchangeVerifySuccess(JSONObject jsonObject) {
|
if (jsonObject.optInt("code") == 0) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
GoldExchangeState info = gson.fromJson(jsonObject.optJSONObject("data")
|
.optJSONObject("result").toString(), new TypeToken<GoldExchangeState>() {
|
}.getType());
|
|
if (info.getType().equals("notEnough")) {
|
GoldExchangeNotEnoughDialog.Builder builder = new GoldExchangeNotEnoughDialog.Builder(this);
|
builder.setTitle("金币不足提醒")
|
.setMessage(info.getTip())
|
.setInfo(info.getGoldCoin())
|
.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
}
|
});
|
builder.create().show();
|
} else if (info.getType().equals("inviteCodeActivate")) {//直接跳转邀请码激活页面
|
startActivity(new Intent(GoldExchangeActivity.this, InviteCodeExchangeActivity.class));
|
} else {
|
goldExchangeState(info);
|
}
|
|
} else {
|
Toast.makeText(GoldExchangeActivity.this,
|
jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onGoExchangeVerifyFailure(String errorStr) {
|
Log.e("GoldExchangeActivity", "onGoExchangeVerifyFailure---" + errorStr);
|
}
|
|
@Override
|
public void onGoExchangeSuccess(JSONObject jsonObject, String id) {
|
if (jsonObject.optInt("code") == 0) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
GoldExchange info = gson.fromJson(jsonObject.optJSONObject("data")
|
.optJSONObject("result").toString(), new TypeToken<GoldExchange>() {
|
}.getType());
|
|
for (int i = 0; i < mList.size(); i++) {
|
if (mList.get(i).getId().equals(id)) {
|
mList.remove(i);
|
if (info != null) {
|
mList.add(i, info);
|
adapter.notifyItemChanged(i);
|
} else {
|
adapter.notifyDataSetChanged();
|
}
|
break;
|
}
|
}
|
binding.tvAvailableGold.setText(jsonObject.optJSONObject("data").optString("goldCoin"));
|
Toast.makeText(GoldExchangeActivity.this,
|
"兑换成功,金币已消耗", Toast.LENGTH_LONG).show();
|
} else {
|
Toast.makeText(GoldExchangeActivity.this,
|
jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onGoExchangeFailure(String errorStr) {
|
Log.e("GoldExchangeActivity", "onGoExchangeFailure---" + errorStr);
|
}
|
|
private void goldExchangeState(final GoldExchangeState info) {
|
GoldExchangeStateDialog.Builder builder = new GoldExchangeStateDialog.Builder(this);
|
if (info.getType().equals("freeCouponBuy")) {
|
builder.setTitle("自购免单券兑换")
|
.setType(1);
|
} else if (info.getType().equals("freeCouponGive")) {
|
builder.setTitle("赠送免单券兑换")
|
.setType(1);
|
} else if (info.getType().equals("rebatePercentCoupon")) {
|
builder.setTitle("返利奖励券兑换")
|
.setType(1);
|
} else if (info.getType().equals("inviteCodePublish")) {
|
builder.setTitle("邀请码发布卡兑换")
|
.setType(2);
|
} else if (info.getType().equals("taoLiJin")) {
|
builder.setTitle("推广红包兑换")
|
.setType(3);
|
} else if (info.getType().equals("cash")) {
|
builder.setTitle("现金红包兑换")
|
.setType(4);
|
}
|
builder.setInfo(info).setPositiveButton("确认兑换", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
mPresenter.goExchangeGoods(GoldExchangeActivity.this, info.getId());
|
dialog.dismiss();
|
}
|
}).create().show();
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.ll_available_gold:
|
if (isTitleInput) {
|
startActivity(new Intent(GoldExchangeActivity.this, GoldTaskActivity.class));
|
}
|
break;
|
}
|
}
|
}
|