|
@@ -1,34 +1,59 @@
|
|
|
package com.itant.shibei.ui.function.template;
|
|
package com.itant.shibei.ui.function.template;
|
|
|
|
|
|
|
|
|
|
+import android.content.DialogInterface;
|
|
|
|
|
+import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.widget.ImageView;
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
+import androidx.appcompat.app.AlertDialog;
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
|
|
|
|
import com.itant.shibei.R;
|
|
import com.itant.shibei.R;
|
|
|
import com.itant.shibei.base.ITopActionListener;
|
|
import com.itant.shibei.base.ITopActionListener;
|
|
|
import com.itant.shibei.bean.CouponBean;
|
|
import com.itant.shibei.bean.CouponBean;
|
|
|
|
|
+import com.itant.shibei.tool.DataTool;
|
|
|
import com.itant.shibei.ui.function.FunctionFragment;
|
|
import com.itant.shibei.ui.function.FunctionFragment;
|
|
|
|
|
+import com.itant.shibei.ui.mine.goods.AddGoodsActivity;
|
|
|
import com.itant.shibei.widget.DividerItemDecoration;
|
|
import com.itant.shibei.widget.DividerItemDecoration;
|
|
|
|
|
+import com.itant.shibei.widget.bottomlistener.OnRcvScrollListener;
|
|
|
|
|
+import com.miekir.common.utils.ToastTool;
|
|
|
import com.miekir.mvp.presenter.InjectPresenter;
|
|
import com.miekir.mvp.presenter.InjectPresenter;
|
|
|
import com.miekir.mvp.view.BaseMVPFragment;
|
|
import com.miekir.mvp.view.BaseMVPFragment;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+import rx_activity_result2.RxActivityResult;
|
|
|
|
|
+
|
|
|
|
|
+import static android.app.Activity.RESULT_OK;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 模板Fragment
|
|
* 模板Fragment
|
|
|
*/
|
|
*/
|
|
|
public class TemplateFragment extends BaseMVPFragment implements ITemplateView, ITopActionListener {
|
|
public class TemplateFragment extends BaseMVPFragment implements ITemplateView, ITopActionListener {
|
|
|
|
|
+ private static final int PAGE_START = 0;
|
|
|
|
|
+ private static final int PAGE_SIZE = 20;
|
|
|
|
|
+
|
|
|
private int mTemplateType = FunctionFragment.TEMPLATE_TYPE_BONUS;
|
|
private int mTemplateType = FunctionFragment.TEMPLATE_TYPE_BONUS;
|
|
|
- private List<CouponBean> mCouponBeanList = new ArrayList<>();
|
|
|
|
|
- private TemplateAdapter mAdapter;
|
|
|
|
|
- private RecyclerView rv_template;
|
|
|
|
|
|
|
|
|
|
@InjectPresenter
|
|
@InjectPresenter
|
|
|
TemplatePresenter mPresenter;
|
|
TemplatePresenter mPresenter;
|
|
|
|
|
+
|
|
|
|
|
+ private SwipeRefreshLayout srl_template;
|
|
|
|
|
+ private List<CouponBean> mCouponBeanList = new ArrayList<>();
|
|
|
|
|
+ private TemplateAdapter mAdapter;
|
|
|
|
|
+ private RecyclerView rv_template;
|
|
|
|
|
|
|
|
|
|
+ /**当前页*/
|
|
|
|
|
+ private int mCurrentPage = PAGE_START;
|
|
|
|
|
+ /**是否下拉获取数据*/
|
|
|
|
|
+ private boolean mIsRefresh = true;
|
|
|
|
|
+ private ImageView iv_empty;
|
|
|
|
|
+
|
|
|
public TemplateFragment(int templateType) {
|
|
public TemplateFragment(int templateType) {
|
|
|
mTemplateType = templateType;
|
|
mTemplateType = templateType;
|
|
|
}
|
|
}
|
|
@@ -42,25 +67,158 @@ public class TemplateFragment extends BaseMVPFragment implements ITemplateView,
|
|
|
public void onCreateViewFinished(@Nullable Bundle savedInstanceState) {
|
|
public void onCreateViewFinished(@Nullable Bundle savedInstanceState) {
|
|
|
super.onCreateViewFinished(savedInstanceState);
|
|
super.onCreateViewFinished(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
+ iv_empty = rootView.findViewById(R.id.iv_empty);
|
|
|
rv_template = rootView.findViewById(R.id.rv_template);
|
|
rv_template = rootView.findViewById(R.id.rv_template);
|
|
|
// 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
|
|
// 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
|
|
|
rv_template.setLayoutManager(layoutManager);
|
|
rv_template.setLayoutManager(layoutManager);
|
|
|
|
|
+
|
|
|
int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
|
|
int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
|
|
|
DividerItemDecoration decoration = new DividerItemDecoration(dividerWidth);
|
|
DividerItemDecoration decoration = new DividerItemDecoration(dividerWidth);
|
|
|
rv_template.addItemDecoration(decoration);
|
|
rv_template.addItemDecoration(decoration);
|
|
|
|
|
+ // 本地写死的数据
|
|
|
|
|
+ mCouponBeanList.addAll(DataTool.getTemplateList(mTemplateType));
|
|
|
mAdapter = new TemplateAdapter(getActivity(), mCouponBeanList);
|
|
mAdapter = new TemplateAdapter(getActivity(), mCouponBeanList);
|
|
|
|
|
+ mAdapter.setCouponLongClickListener(this::showAdminDialog);
|
|
|
rv_template.setAdapter(mAdapter);
|
|
rv_template.setAdapter(mAdapter);
|
|
|
|
|
+ // 不要滑动一段距离
|
|
|
|
|
+ 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(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onRefresh() {
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 获取数据
|
|
|
|
|
+ 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(), AddGoodsActivity.class);
|
|
|
|
|
+ modifyIntent.putExtra(AddGoodsActivity.KEY_MODIFY_GOODS, couponBean);
|
|
|
|
|
+ RxActivityResult.on(getActivity()).startIntent(modifyIntent)
|
|
|
|
|
+ .filter(result -> result.resultCode() == RESULT_OK)
|
|
|
|
|
+ .subscribe(result -> {
|
|
|
|
|
+ // 修改商品成功之后刷新item
|
|
|
|
|
+ mAdapter.notifyItemChanged(position);
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ .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 onTemplateDataCome(boolean success, String message, List<CouponBean> couponBeanList) {
|
|
|
|
|
+ srl_template.setRefreshing(false);
|
|
|
|
|
+ if (!success) {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (mCurrentPage == PAGE_START) {
|
|
|
|
|
+ mCouponBeanList.clear();
|
|
|
|
|
+ // 本地写死的数据
|
|
|
|
|
+ mCouponBeanList.addAll(DataTool.getTemplateList(mTemplateType));
|
|
|
|
|
+ mCouponBeanList.addAll(couponBeanList);
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
|
|
|
|
|
+ // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
|
|
|
|
|
+ if (mIsRefresh) {
|
|
|
|
|
+ rv_template.smoothScrollToPosition(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ mCouponBeanList.addAll(couponBeanList);
|
|
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 请求到数据了,页数自增
|
|
|
|
|
+ if (couponBeanList != null && couponBeanList.size() > 0) {
|
|
|
|
|
+ mCurrentPage++;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 从服务器获取数据
|
|
|
|
|
- mPresenter.getTemplateData(mTemplateType);
|
|
|
|
|
|
|
+ if (mCouponBeanList.size() == 0) {
|
|
|
|
|
+ iv_empty.setVisibility(View.VISIBLE);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ iv_empty.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除商品结果
|
|
|
|
|
+ * @param success
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @param position
|
|
|
|
|
+ */
|
|
|
@Override
|
|
@Override
|
|
|
- public void onTemplateDataCome(List<CouponBean> couponBeanList) {
|
|
|
|
|
- // todo 分页,判断是否第0页(下拉刷新)
|
|
|
|
|
- mCouponBeanList.addAll(couponBeanList);
|
|
|
|
|
- mAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
+ public void onDeleteCouponResult(boolean success, String message, int position) {
|
|
|
|
|
+ if (success) {
|
|
|
|
|
+ mAdapter.notifyItemRemoved(position);
|
|
|
|
|
+ ToastTool.showShort("删除成功");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ToastTool.showShort(message);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|