1. 程式人生 > >購物車的第二種方法的架構

購物車的第二種方法的架構

 

//CartModel

 public void getData(String url, INetCallBack callBack, Type type) {
        HttpUtils.getInstance().get(url, callBack, type);
    }

//IView


public interface IView {
    void success(MessageBean<List<Shopper<List<Product>>>> data);

    void failed(Exception e);
}

//CartPresenter

public class CartPresenter {
    private IView iv;
    private CartModel model;

    public void attach(IView iv) {
        this.iv = iv;
        model = new CartModel();
    }

    public void getData() {
        String url = "http://www.zhaoapi.cn/product/getCarts?uid=1538";
        Type type = new TypeToken<MessageBean<List<Shopper<List<Product>>>>>() {
        }.getType();

        model.getData(url, new INetCallBack() {
            @Override
            public void success(Object obj) {
                MessageBean<List<Shopper<List<Product>>>> data = (MessageBean<List<Shopper<List<Product>>>>) obj;
                iv.success(data);
            }

            @Override
            public void failed(Exception e) {
                iv.failed(e);
            }
        }, type);

    }

    public void detach() {
        if (iv != null) {
            iv = null;
        }
    }
}

//

public class MainActivity extends AppCompatActivity implements IView {
    private static final String TAG = "MainActivity";
    //    MessageBean<List<ShopperAdapter<List<Product>>>>
    private TextView txtEditFinish;
    private CheckBox cbTotal;
    private TextView txtPrice;
    private Button btnCalu;
    private RecyclerView rvShoper;

    private CartPresenter presenter;
    private ShopperAdapter adapter;

    private List<Shopper<List<Product>>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        setListener();

    }

    private void setListener() {
//        cbTotal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                // 遍歷一級列表,和下方的全選狀態一致
//                for (Shopper<List<Product>> listShopper : list) {
//                    listShopper.setChecked(isChecked);
//                    // 遍歷二級列表,和下方的全選狀態一致
//                    List<Product> products = listShopper.getList();
//                    for (Product product : products) {
//                        product.setChecked(isChecked);
//                    }
//                }
//                adapter.notifyDataSetChanged();
//            }
//        });

        cbTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = cbTotal.isChecked();
                // 遍歷一級列表,和下方的全選狀態一致
                for (Shopper<List<Product>> listShopper : list) {
                    listShopper.setChecked(isChecked);
                    // 遍歷二級列表,和下方的全選狀態一致
                    List<Product> products = listShopper.getList();
                    for (Product product : products) {
                        product.setChecked(isChecked);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        // 商家的列表
        adapter = new ShopperAdapter(this, list);
        // 新增一級條目(商家)狀態發生變化時
        adapter.setOnShopperClickListener(new ShopperAdapter.OnShopperClickListener() {
            @Override
            public void onShopperClick(int position, boolean isCheck) {
                // 為了效率考慮,當點選狀態變成未選中時,全選按鈕肯定就不是全選了,就不用再迴圈一次
                if (!isCheck) {
                    cbTotal.setChecked(false);
                } else {
                    // 如果是商家變成選中狀態時,需要迴圈遍歷所有的商家是否被選中
                    // 迴圈遍歷之前先設定一個true標誌位,只要有一個是未選中就改變這個標誌位為false
                    boolean isAllShopperChecked = true;
                    for (Shopper<List<Product>> listShopper : list) {
                        // 只要有一個商家沒有被選中,全選複選框就變成未選中狀態,並且結束迴圈
                        if (!listShopper.isChecked()) {
                            isAllShopperChecked = false;
                            break;
                        }
                    }
                    cbTotal.setChecked(isAllShopperChecked);
                }

                // 一級條目發生變化時,計算一下總價
                calculatePrice();
            }
        });


        adapter.setOnAddDecreaseProductListener(new ProductAdapter.OnAddDecreaseProductListener() {
            @Override
            public void onChange(int position, int num) {
                calculatePrice();
            }
        });


        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvShoper.setLayoutManager(layoutManager);
        rvShoper.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rvShoper.setAdapter(adapter);

        presenter = new CartPresenter();
        presenter.attach(this);
        presenter.getData();
    }

    // 計算商品總價
    private void calculatePrice() {
        // 遍歷商家
        float totalPrice = 0;
        for (Shopper<List<Product>> listShopper : list) {
            // 遍歷商家的商品
            List<Product> list = listShopper.getList();
            for (Product product : list) {
                // 如果商品被選中
                if (product.isChecked()) {
                    totalPrice += product.getNum() * product.getPrice();
                }
            }
        }

        txtPrice.setText("總價:" + totalPrice);

    }

    private void initView() {
        txtEditFinish = findViewById(R.id.txt_edit_or_finish);
        cbTotal = findViewById(R.id.cb_total_select);
        txtPrice = findViewById(R.id.txt_total_price);
        btnCalu = findViewById(R.id.btn_calu);
        rvShoper = findViewById(R.id.rv_shopper);
    }

    @Override
    public void success(MessageBean<List<Shopper<List<Product>>>> data) {
        if (data != null) {
            // 獲取商家列表
            List<Shopper<List<Product>>> shoppers = data.getData();
            if (shoppers != null) {
                list.clear();
                list.addAll(shoppers);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void failed(Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detach();
        }
    }
}