package com.shayu.onetoone.activity; import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.alibaba.android.arouter.facade.annotation.Route; import com.shayu.onetoone.R; import com.shayu.onetoone.adapter.DiamondExchangeAdapter; import com.shayu.onetoone.bean.ExchangeModel; import com.shayu.onetoone.event.DiamondExchangeEvent; import com.shayu.onetoone.manager.OTONetManager; import com.shayu.onetoone.manager.RouteManager; import com.yunbao.common.custom.ItemDecoration; import com.yunbao.common.http.base.HttpCallback; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.views.weight.ViewClicksAntiShake; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.List; @Route(path = RouteManager.ACTIVITY_DIAMOND_EXCHANGE) public class DiamondExchangeActivity extends AbsOTOActivity { private RecyclerView diamondExchangeList; private DiamondExchangeAdapter exchangeAdapter; private TextView title, totalConvertibility; private String type = "yuanbao"; private EditText diamondExchangeInput; private String number; private int index = 0; @Override protected int getLayoutId() { return R.layout.activity_diamond_exchange; } @Override protected void main(Bundle savedInstanceState) { Bus.getOn(this); initView(); initData(); } @Override protected void onDestroy() { super.onDestroy(); Bus.getOff(this); } private void initView() { diamondExchangeList = findViewById(R.id.diamond_exchange_list); title = findViewById(R.id.title); diamondExchangeInput = findViewById(R.id.diamond_exchange_input); totalConvertibility = findViewById(R.id.total_convertibility); diamondExchangeList.addItemDecoration(new ItemDecoration(mContext, Color.parseColor("#ffffff"), 10, 2)); diamondExchangeList.setLayoutManager(new GridLayoutManager(mContext, 3)); exchangeAdapter = new DiamondExchangeAdapter(); diamondExchangeList.setAdapter(exchangeAdapter); ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.arn_toggle), new ViewClicksAntiShake.ViewClicksCallBack() { @Override public void onViewClicks() { if (TextUtils.equals(type, "yuanbao")) { Bundle bundle = new Bundle(); bundle.putString("type", "coin"); RouteManager.forwardActivity(RouteManager.ACTIVITY_DIAMOND_EXCHANGE, bundle); finish(); } else { Bundle bundle = new Bundle(); bundle.putString("type", "yuanbao"); RouteManager.forwardActivity(RouteManager.ACTIVITY_DIAMOND_EXCHANGE, bundle); finish(); } } }); ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.exchange_record), new ViewClicksAntiShake.ViewClicksCallBack() { @Override public void onViewClicks() { if (TextUtils.equals(type, "yuanbao")) { Bundle bundle = new Bundle(); bundle.putString("type", "coin"); RouteManager.forwardActivity(RouteManager.ACTIVITY_EXCHANGE_RECORD, bundle); } else { Bundle bundle = new Bundle(); bundle.putString("type", "yuanbao"); RouteManager.forwardActivity(RouteManager.ACTIVITY_EXCHANGE_RECORD, bundle); } } }); ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.immediate_exchange), new ViewClicksAntiShake.ViewClicksCallBack() { @Override public void onViewClicks() { String exchangeNumber = ""; if (TextUtils.isEmpty(number)) { exchangeNumber = diamondExchangeInput.getText().toString(); } else { exchangeNumber = number; } OTONetManager.getInstance(mContext).getTransform(type, exchangeNumber, new HttpCallback>() { @Override public void onSuccess(List data) { if (data.size() > 0) { if ((data.size() - 1) > index && index > 0) { data.get(index).setSelect(true); totalConvertibility.setText(String.valueOf(data.get(0).getNum())); number = data.get(index).getNum(); } else { if (TextUtils.isEmpty(diamondExchangeInput.getText().toString())) { data.get(0).setSelect(true); totalConvertibility.setText(String.valueOf(data.get(0).getNum())); number = data.get(0).getNum(); } else { totalConvertibility.setText(String.valueOf(data.get(0).getNum())); for (int i = 0; i < data.size(); i++) { data.get(i).setSelect(false); } } } ToastUtil.show(R.string.exchange_success); exchangeAdapter.addData(data); } } @Override public void onError(String error) { ToastUtil.show(error); } }); } }); diamondExchangeInput.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (!TextUtils.isEmpty(diamondExchangeInput.getText().toString()) && !TextUtils.isEmpty(number)) { exchangeAdapter.unSelect(); number = null; index = -1; } } }); ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.one_back), new ViewClicksAntiShake.ViewClicksCallBack() { @Override public void onViewClicks() { finish(); } }); } private void initData() { if (getIntent() != null && getIntent().getExtras() != null) { type = getIntent().getExtras().getString("type"); if (TextUtils.equals(type, "yuanbao")) { title.setText(getString(R.string.my_arnings_exchange_star)); diamondExchangeInput.setText(R.string.diamond_exchange_input_hint); } else { title.setText(getString(R.string.my_arnings_exchange_diamond)); diamondExchangeInput.setText(R.string.diamond_exchange_input_hint2); } } OTONetManager.getInstance(mContext).getExchangeList(type, new HttpCallback>() { @Override public void onSuccess(List data) { if (data.size() > 0) { data.get(0).setSelect(true); totalConvertibility.setText(String.valueOf(data.get(0).getNum())); number = data.get(0).getNum(); exchangeAdapter.addData(data); } } @Override public void onError(String error) { ToastUtil.show(error); } }); } @Subscribe(threadMode = ThreadMode.MAIN) public void onExchangeModel(DiamondExchangeEvent event) { number = event.getNum(); index = event.getIndex(); diamondExchangeInput.setText(""); } }