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.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.ActivityInviteCodeListBinding;
|
import com.tejia.lijin.app.databinding.ItemRecyclerviewBottom3Binding;
|
import com.tejia.lijin.app.entity.GoldExchangeState;
|
import com.tejia.lijin.app.entity.InviteCodeInfo;
|
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.InviteCodeExchangeContract;
|
import com.tejia.lijin.app.ui.gold.presenter.InviteCodeExchangePresenter;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.ui.mine.ActivationInviteCodeAcitvity;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
|
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class InviteCodeExchangeActivity extends BaseMVPActivity<InviteCodeExchangePresenter>
|
implements InviteCodeExchangeContract.inviteCodeExchange, View.OnClickListener {
|
|
ActivityInviteCodeListBinding binding;
|
ItemRecyclerviewBottom3Binding bottomBinding;
|
|
InviteCodeExchangeAdapter adapter;
|
List<InviteCodeInfo> mList = new ArrayList<>();
|
|
int page = 1;
|
int count = 0;
|
boolean isLoad = true;
|
|
String helpLink = "";
|
|
@Override
|
protected void initActivityView(Bundle savedInstanceState) {
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_invite_code_list);
|
}
|
|
@Override
|
protected void initView() {
|
TopStatusSettings.setStatusView(this, binding.includeStatusBar.vStatusBar);
|
binding.includeTopBar.tvTopBarMiddle.setText("兑换邀请码");
|
binding.includeTopBar.ivTopBarRight1.setVisibility(View.VISIBLE);
|
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
|
binding.rvInviteCodeList.setLayoutManager(manager);
|
adapter = new InviteCodeExchangeAdapter(this, mList, mPresenter);
|
binding.rvInviteCodeList.setAdapter(adapter);
|
DividerItemDecoration decoration = new DividerItemDecoration(DividerItemDecoration.VERTICAL);
|
decoration.setSize(1);
|
binding.rvInviteCodeList.addItemDecoration(decoration);
|
|
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.includeTopBar.tvTopBarLeft.setOnClickListener(this);
|
binding.includeTopBar.ivTopBarRight1.setOnClickListener(this);
|
binding.srlInviteCodeList.setColorSchemeColors(getResources().getColor(R.color.theme));
|
binding.rvInviteCodeList.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int first = layoutManager.findFirstVisibleItemPosition();
|
int last = layoutManager.findLastVisibleItemPosition();
|
int total = layoutManager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (first == 0) {
|
binding.ivSlidTop.setVisibility(View.GONE);
|
}
|
if (last == (total - 1) && mList.size() < count && isLoad) {
|
page++;
|
isLoad = false;
|
bottomBinding.ivLoading.setVisibility(View.VISIBLE);
|
bottomBinding.tvLoading.setText("正在加载更多是数据");
|
mPresenter.getInviteCodeList(page);
|
}
|
}
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
if (Math.abs(dy) > 10) {
|
binding.ivSlidTop.setVisibility(dy > 0 ? View.GONE : View.VISIBLE);
|
}
|
}
|
});
|
|
binding.srlInviteCodeList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
mPresenter.getInviteCodeList(page);
|
}
|
});
|
|
mPresenter.getInviteCodeList(page);
|
}
|
|
@Override
|
protected InviteCodeExchangePresenter createPresenter() {
|
return new InviteCodeExchangePresenter(this);
|
}
|
|
|
@Override
|
public void onGetInviteCodeListSuccess(JSONObject jsonObject) {
|
binding.srlInviteCodeList.setRefreshing(false);
|
isLoad = true;
|
if (jsonObject.optInt("code") == 0) {
|
count = jsonObject.optJSONObject("data").optInt("count");
|
helpLink = jsonObject.optJSONObject("data").optString("helpLink");
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<InviteCodeInfo> list = gson.fromJson(jsonObject
|
.optJSONObject("data").optJSONArray("list").toString(),
|
new TypeToken<List<InviteCodeInfo>>() {
|
}.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(InviteCodeExchangeActivity.this,
|
jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onGetInviteCodeListFailure(String errorStr) {
|
binding.srlInviteCodeList.setRefreshing(false);
|
bottomBinding.getRoot().setVisibility(View.GONE);
|
isLoad = true;
|
}
|
|
@Override
|
public void onGoExchangeVerifySuccess(final JSONObject jsonObject) {
|
if (jsonObject.optInt("code") == 0) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
final 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 {
|
GoldExchangeStateDialog.Builder builder = new GoldExchangeStateDialog.Builder(this);
|
builder.setTitle("邀请码激活卡兑换")
|
.setType(1).setInfo(info).setPositiveButton("确认兑换", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
mPresenter.inviteCodeExchange(info.getId());
|
dialog.dismiss();
|
}
|
}).create().show();
|
}
|
|
} else {
|
Toast.makeText(InviteCodeExchangeActivity.this, jsonObject
|
.optString("msg"), Toast.LENGTH_LONG).show();
|
|
}
|
}
|
|
@Override
|
public void onGoExchangeVerifyFailure(String errorStr) {
|
|
}
|
|
@Override
|
public void onGoExchangeSuccess(JSONObject jsonObject, String id) {
|
if (jsonObject.optInt("code") == 0) {
|
Toast.makeText(InviteCodeExchangeActivity.this,
|
jsonObject.optJSONObject("data").optString("msg"), Toast.LENGTH_LONG).show();
|
|
ClipboardUtil.copy(getApplicationContext(), jsonObject.optJSONObject("data").optString("inviteCode"));
|
|
startActivity(new Intent(InviteCodeExchangeActivity.this, ActivationInviteCodeAcitvity.class));
|
} else {
|
Toast.makeText(InviteCodeExchangeActivity.this,
|
jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onGoExchangeFailure(String errorStr) {
|
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.iv_top_bar_right1:
|
Intent intent = new Intent(InviteCodeExchangeActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", helpLink);
|
startActivity(intent);
|
break;
|
}
|
}
|
}
|