|
@@ -0,0 +1,219 @@
|
|
|
|
|
+package com.miekir.ym.ui.coupon;
|
|
|
|
|
+
|
|
|
|
|
+import android.content.DialogInterface;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+
|
|
|
|
|
+import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import com.miekir.common.utils.ToastTool;
|
|
|
|
|
+import com.miekir.mvp.presenter.InjectPresenter;
|
|
|
|
|
+import com.miekir.mvp.view.BaseMvpFragment;
|
|
|
|
|
+import com.miekir.ym.R;
|
|
|
|
|
+import com.miekir.ym.constant.FragmentType;
|
|
|
|
|
+import com.miekir.ym.listener.ITopActionListener;
|
|
|
|
|
+import com.miekir.ym.listener.OnRcvScrollListener;
|
|
|
|
|
+import com.miekir.ym.widget.NormalDividerItemDecoration;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 模板Fragment
|
|
|
|
|
+ */
|
|
|
|
|
+public class CouponFragment extends BaseMvpFragment implements ICouponView<CouponBean>, ITopActionListener {
|
|
|
|
|
+ private static final int PAGE_START = 0;
|
|
|
|
|
+ private static final int PAGE_SIZE = 20;
|
|
|
|
|
+
|
|
|
|
|
+ private int mTemplateType = FragmentType.DIGITAL;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectPresenter
|
|
|
|
|
+ CouponPresenter mPresenter;
|
|
|
|
|
+
|
|
|
|
|
+ private SwipeRefreshLayout srl_template;
|
|
|
|
|
+ private List<CouponBean> mCouponBeanList = new ArrayList<>();
|
|
|
|
|
+ private CouponAdapter mAdapter;
|
|
|
|
|
+ private RecyclerView rv_template;
|
|
|
|
|
+
|
|
|
|
|
+ /**当前页*/
|
|
|
|
|
+ private int mCurrentPage = PAGE_START;
|
|
|
|
|
+ /**是否下拉获取数据*/
|
|
|
|
|
+ private boolean mIsRefresh = true;
|
|
|
|
|
+
|
|
|
|
|
+ public CouponFragment(int templateType) {
|
|
|
|
|
+ mTemplateType = templateType;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getLayoutId() {
|
|
|
|
|
+ return R.layout.fragment_coupon;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onViewInit() {
|
|
|
|
|
+ rv_template = rootView.findViewById(R.id.rv_template);
|
|
|
|
|
+ // 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
|
|
|
|
|
+ LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
|
|
|
|
|
+ rv_template.setLayoutManager(layoutManager);
|
|
|
|
|
+
|
|
|
|
|
+ int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
|
|
|
|
|
+ NormalDividerItemDecoration decoration = new NormalDividerItemDecoration(dividerWidth);
|
|
|
|
|
+ rv_template.addItemDecoration(decoration);
|
|
|
|
|
+ mAdapter = new CouponAdapter(getActivity(), mCouponBeanList);
|
|
|
|
|
+ mAdapter.setCouponLongClickListener(this::showAdminDialog);
|
|
|
|
|
+ rv_template.setAdapter(mAdapter);
|
|
|
|
|
+ // 必须先绑定RecyclerView
|
|
|
|
|
+ mAdapter.setEmptyView(R.layout.view_empty, rv_template);
|
|
|
|
|
+
|
|
|
|
|
+ // 不要滑动一段距离
|
|
|
|
|
+ rv_template.smoothScrollToPosition(0);
|
|
|
|
|
+ srl_template = rootView.findViewById(R.id.srl_template);
|
|
|
|
|
+ srl_template.setColorSchemeResources(R.color.black_theme, R.color.blue_droid, R.color.yellow_droid, R.color.red_droid);
|
|
|
|
|
+
|
|
|
|
|
+ // 下拉刷新
|
|
|
|
|
+ srl_template.setOnRefreshListener(() -> {
|
|
|
|
|
+ mIsRefresh = true;
|
|
|
|
|
+ mCurrentPage = PAGE_START;
|
|
|
|
|
+ mPresenter.getTemplateData(mCurrentPage, PAGE_SIZE);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 加载更多
|
|
|
|
|
+ rv_template.addOnScrollListener(new OnRcvScrollListener(){
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onBottom() {
|
|
|
|
|
+ super.onBottom();
|
|
|
|
|
+ // 如果到底部了,而且不是正在加载状态,就变为正在加载状态,并及时去加载数据
|
|
|
|
|
+ if (!srl_template.isRefreshing()) {
|
|
|
|
|
+ mIsRefresh = false;
|
|
|
|
|
+ srl_template.setRefreshing(true);
|
|
|
|
|
+ mPresenter.getTemplateData(mCurrentPage, PAGE_SIZE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onLazyLoad() {
|
|
|
|
|
+ // 获取数据
|
|
|
|
|
+ srl_template.setRefreshing(true);
|
|
|
|
|
+ mPresenter.getTemplateData(mCurrentPage, PAGE_SIZE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改商品或者删除商品
|
|
|
|
|
+ */
|
|
|
|
|
+ private void showAdminDialog(int position) {
|
|
|
|
|
+ CouponBean couponBean = mCouponBeanList.get(position);
|
|
|
|
|
+ if (couponBean.isLocal) {
|
|
|
|
|
+ // 本地写死的不能删除
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
|
|
|
|
|
+ .setMessage("选择操作")
|
|
|
|
|
+ .setPositiveButton("修改", (dialog, which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+// Intent modifyIntent = new Intent(getActivity(), AddCouponActivity.class);
|
|
|
|
|
+// modifyIntent.putExtra(AddCouponActivity.KEY_MODIFY_COUPON, couponBean);
|
|
|
|
|
+// RxActivityResult.on(getActivity())
|
|
|
|
|
+// .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 = mCouponBeanList.get(position);
|
|
|
|
|
+ AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
|
|
|
|
|
+ .setMessage("确定删除优惠券:" + couponBean.couponName)
|
|
|
|
|
+ .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
|
|
|
|
|
+ .setPositiveButton("确定", (DialogInterface dialog, int which) -> {
|
|
|
|
|
+ dialog.dismiss();
|
|
|
|
|
+ // 删除商品
|
|
|
|
|
+ mPresenter.deleteCouponById(position, couponBean.id);
|
|
|
|
|
+ }).create();
|
|
|
|
|
+ alertDialog.show();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCouponDataCome(boolean success, String message, List<CouponBean> couponBeanList) {
|
|
|
|
|
+ srl_template.setRefreshing(false);
|
|
|
|
|
+ if (!success) {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (mCurrentPage == PAGE_START) {
|
|
|
|
|
+ mCouponBeanList.clear();
|
|
|
|
|
+ if (couponBeanList != null) {
|
|
|
|
|
+ mCouponBeanList.addAll(couponBeanList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
|
|
|
|
|
+ // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
|
|
|
|
|
+ if (mIsRefresh) {
|
|
|
|
|
+ ToastTool.showShort("刷新成功");
|
|
|
|
|
+ rv_template.smoothScrollToPosition(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (couponBeanList != null) {
|
|
|
|
|
+ mCouponBeanList.addAll(couponBeanList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 请求到数据了,页数自增
|
|
|
|
|
+ if (couponBeanList != null && couponBeanList.size() > 0) {
|
|
|
|
|
+ mCurrentPage++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ToastTool.showShort("没有更多数据了");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除商品结果
|
|
|
|
|
+ * @param success
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @param position
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDeleteCoupon(boolean success, String message, int position) {
|
|
|
|
|
+ if (success) {
|
|
|
|
|
+ mAdapter.notifyItemRemoved(position);
|
|
|
|
|
+ ToastTool.showShort("删除成功");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onTopAction() {
|
|
|
|
|
+ rv_template.smoothScrollToPosition(0);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|