|
@@ -0,0 +1,246 @@
|
|
|
|
|
+package com.miekir.ym.ui.home.search;
|
|
|
|
|
+
|
|
|
|
|
+import android.content.DialogInterface;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+import android.text.TextUtils;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.widget.Button;
|
|
|
|
|
+import android.widget.EditText;
|
|
|
|
|
+
|
|
|
|
|
+import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
+
|
|
|
|
|
+import com.miekir.common.utils.ToastTool;
|
|
|
|
|
+import com.miekir.common.utils.ViewTool;
|
|
|
|
|
+import com.miekir.mvp.presenter.InjectPresenter;
|
|
|
|
|
+import com.miekir.ym.R;
|
|
|
|
|
+import com.miekir.ym.base.YangActivity;
|
|
|
|
|
+import com.miekir.ym.listener.OnRcvScrollListener;
|
|
|
|
|
+import com.miekir.ym.ui.home.coupon.CouponAdapter;
|
|
|
|
|
+import com.miekir.ym.ui.home.coupon.CouponBean;
|
|
|
|
|
+import com.miekir.ym.ui.home.coupon.CouponPresenter;
|
|
|
|
|
+import com.miekir.ym.ui.home.coupon.ICouponView;
|
|
|
|
|
+import com.miekir.ym.ui.home.more.add.AddCouponActivity;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import rx_activity_result2.RxActivityResult;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Copyright (C), 2019-2020, Miekir
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author Miekir
|
|
|
|
|
+ * @date 2020/8/12 19:40
|
|
|
|
|
+ * Description: 输入文字搜索
|
|
|
|
|
+ */
|
|
|
|
|
+public class SearchActivity extends YangActivity implements View.OnClickListener,
|
|
|
|
|
+ ISearchView, View.OnFocusChangeListener, ICouponView<CouponBean> {
|
|
|
|
|
+ private static final int PAGE_START = 0;
|
|
|
|
|
+ private static final int PAGE_SIZE = 20;
|
|
|
|
|
+ private int mCurrentPage = PAGE_START;
|
|
|
|
|
+
|
|
|
|
|
+ private EditText et_search;
|
|
|
|
|
+ private Button btn_search;
|
|
|
|
|
+
|
|
|
|
|
+ private RecyclerView rv_search_result;
|
|
|
|
|
+ private List<CouponBean> mCouponList = new ArrayList<>();
|
|
|
|
|
+ private CouponAdapter mAdapter;
|
|
|
|
|
+ private boolean mIsRefresh = true;
|
|
|
|
|
+
|
|
|
|
|
+ private String mKeyword;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectPresenter
|
|
|
|
|
+ SearchPresenter searchPresenter;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectPresenter
|
|
|
|
|
+ CouponPresenter couponPresenter;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getLayoutId() {
|
|
|
|
|
+ return R.layout.activity_search;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void initViews(Bundle savedInstanceState) {
|
|
|
|
|
+ setTitle("搜索");
|
|
|
|
|
+ et_search = findViewById(R.id.et_search);
|
|
|
|
|
+ et_search.setOnFocusChangeListener(this);
|
|
|
|
|
+ btn_search = findViewById(R.id.btn_search);
|
|
|
|
|
+
|
|
|
|
|
+ rv_search_result = findViewById(R.id.rv_search_result);
|
|
|
|
|
+ // 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
|
|
|
|
|
+ LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
|
|
|
|
|
+ rv_search_result.setLayoutManager(layoutManager);
|
|
|
|
|
+ int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
|
|
|
|
|
+ mAdapter = new CouponAdapter(this, mCouponList);
|
|
|
|
|
+ mAdapter.setCouponLongClickListener(this::showAdminDialog);
|
|
|
|
|
+ rv_search_result.setAdapter(mAdapter);
|
|
|
|
|
+ mAdapter.setEmptyView(R.layout.view_empty, rv_search_result);
|
|
|
|
|
+ // 加载更多
|
|
|
|
|
+ rv_search_result.addOnScrollListener(new OnRcvScrollListener(){
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onBottom() {
|
|
|
|
|
+ super.onBottom();
|
|
|
|
|
+ // 如果到底部了,而且不是正在加载状态,就变为正在加载状态,并及时去加载数据
|
|
|
|
|
+ showLoading();
|
|
|
|
|
+ mIsRefresh = false;
|
|
|
|
|
+ searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ ViewTool.requestInputFocus(this, et_search);
|
|
|
|
|
+ ViewTool.setOnClickListener(this, this, new int[]{R.id.ll_search, R.id.btn_search, R.id.fl_clear});
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(View v) {
|
|
|
|
|
+
|
|
|
|
|
+ switch (v.getId()) {
|
|
|
|
|
+ case R.id.btn_search:
|
|
|
|
|
+ et_search.clearFocus();
|
|
|
|
|
+ mKeyword = et_search.getText().toString();
|
|
|
|
|
+ if (TextUtils.isEmpty(mKeyword)) {
|
|
|
|
|
+ ToastTool.showShort("请输入关键字或链接");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ showLoading();
|
|
|
|
|
+ mCurrentPage = PAGE_START;
|
|
|
|
|
+ mCouponList.clear();
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ mIsRefresh = true;
|
|
|
|
|
+ searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case R.id.ll_search:
|
|
|
|
|
+ et_search.clearFocus();
|
|
|
|
|
+ break;
|
|
|
|
|
+ case R.id.fl_clear:
|
|
|
|
|
+ et_search.setText("");
|
|
|
|
|
+ ViewTool.requestInputFocus(this, et_search);
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改商品或者删除商品
|
|
|
|
|
+ */
|
|
|
|
|
+ private void showAdminDialog(int position) {
|
|
|
|
|
+ CouponBean couponBean = mCouponList.get(position);
|
|
|
|
|
+ AlertDialog alertDialog = new AlertDialog.Builder(this)
|
|
|
|
|
+ .setMessage("选择操作")
|
|
|
|
|
+ .setPositiveButton("修改", (dialog, which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+ Intent modifyIntent = new Intent(this, AddCouponActivity.class);
|
|
|
|
|
+ modifyIntent.putExtra(AddCouponActivity.KEY_MODIFY_COUPON, couponBean);
|
|
|
|
|
+ RxActivityResult.on(this).startIntent(modifyIntent)
|
|
|
|
|
+ .filter(result -> result.resultCode() == RESULT_OK)
|
|
|
|
|
+ .doOnNext(result -> {
|
|
|
|
|
+ // 修改商品成功之后刷新item
|
|
|
|
|
+ CouponBean modifiedBean = (CouponBean) result.data().getSerializableExtra(AddCouponActivity.KEY_MODIFY_COUPON);
|
|
|
|
|
+ if (modifiedBean == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ couponBean.updateData(modifiedBean);
|
|
|
|
|
+ mAdapter.notifyItemChanged(position);
|
|
|
|
|
+ })
|
|
|
|
|
+ .subscribe();
|
|
|
|
|
+ })
|
|
|
|
|
+ .setNeutralButton("取消", (dialog, which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+ })
|
|
|
|
|
+ .setNegativeButton("删除", (DialogInterface dialog, int which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+ showDeleteDialog(position);
|
|
|
|
|
+ }).create();
|
|
|
|
|
+ alertDialog.setCanceledOnTouchOutside(false);
|
|
|
|
|
+ alertDialog.setCancelable(false);
|
|
|
|
|
+ alertDialog.show();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否删除商品
|
|
|
|
|
+ */
|
|
|
|
|
+ private void showDeleteDialog(int position) {
|
|
|
|
|
+ CouponBean couponBean = mCouponList.get(position);
|
|
|
|
|
+ AlertDialog alertDialog = new AlertDialog.Builder(this)
|
|
|
|
|
+ .setMessage("确定删除优惠:" + couponBean.couponName)
|
|
|
|
|
+ .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
|
|
|
|
|
+ .setPositiveButton("确定", (DialogInterface dialog, int which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+ // 删除商品
|
|
|
|
|
+ couponPresenter.deleteCouponById(position, couponBean.id);
|
|
|
|
|
+ }).create();
|
|
|
|
|
+ alertDialog.show();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onSearchKeywordResult(boolean success, String message, List<CouponBean> goodsList) {
|
|
|
|
|
+ hideLoading();
|
|
|
|
|
+
|
|
|
|
|
+ if (!success) {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (mCurrentPage == PAGE_START) {
|
|
|
|
|
+ mCouponList.clear();
|
|
|
|
|
+ if (goodsList != null) {
|
|
|
|
|
+ mCouponList.addAll(goodsList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 本地写死的数据
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
|
|
|
|
|
+ // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
|
|
|
|
|
+ if (mIsRefresh) {
|
|
|
|
|
+ rv_search_result.smoothScrollToPosition(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (goodsList != null) {
|
|
|
|
|
+ mCouponList.addAll(goodsList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 请求到数据了,页数自增
|
|
|
|
|
+ if (goodsList != null && goodsList.size() > 0) {
|
|
|
|
|
+ mCurrentPage++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ToastTool.showShort("没有更多数据了");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
|
+ if (v.getId() == R.id.et_search) {
|
|
|
|
|
+ if (hasFocus) {
|
|
|
|
|
+ rv_search_result.setVisibility(View.GONE);
|
|
|
|
|
+ btn_search.setVisibility(View.VISIBLE);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ rv_search_result.setVisibility(View.VISIBLE);
|
|
|
|
|
+ btn_search.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCouponDataCome(boolean success, String message, List<CouponBean> couponBeanList) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDeleteCoupon(boolean success, String message, int position) {
|
|
|
|
|
+ // 删除结果
|
|
|
|
|
+ if (success) {
|
|
|
|
|
+ mAdapter.notifyItemRemoved(position);
|
|
|
|
|
+ ToastTool.showShort("删除成功");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|