Невозможно обновить представление об изменении данных в Epoxy Controller - PullRequest
0 голосов
/ 24 сентября 2019

Я использую Epoxy Controller для Recycler View.У меня возникают проблемы при смене представления после изменения данных в результате действия пользователя.

В основном у меня есть кнопка переключения в представлении, которое используется внутри представления переработчика, и я пытаюсь обновить представление об изменении состояния кнопки переключателя,Я вызываю requestModelBuild () в функции setProductList () контроллера эпоксидной смолы, но изменение не отражается в представлении.

public class SellerInventoryListEpoxyController extends EpoxyController {

private List<Product> productList = Collections.emptyList();
private Context context;
private SellerInventoryListEpoxyController.Callbacks callbacks;

public void setProductList(List<Product> productList, Context context, SellerInventoryListEpoxyController.Callbacks callbacks) {
    this.productList = productList;
    this.context = context;
    this.callbacks = callbacks;
    requestModelBuild();
}

@Override
protected void buildModels() {
    for (int i = 0; i < productList.size(); i++) {
        new InventoryProductDetailModel_()
                .id(productList.get(i).getId())
                .product(productList.get(i))
                .position(i)
                .listSize(productList.size())
                .callbacks(callbacks)
                .context(context)
                .addTo(this);
    }
}


public interface Callbacks {
    void onViewComboClick(Product productComboList);

    void onProductListingStatusChanged(Boolean newStatus, int productSellerId);

    void onRecyclerViewReachEnd();
}

}

public class InventoryProductDetailModel extends EpoxyModelWithHolder<InventoryProductDetailModel.ViewHolder> implements CompoundButton.OnCheckedChangeListener {


@EpoxyAttribute
Product product;
@EpoxyAttribute
int position;
@EpoxyAttribute
int listSize;
@EpoxyAttribute
Context context;
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
SellerInventoryListEpoxyController.Callbacks callbacks;

@Override
protected ViewHolder createNewHolder() {
    return new ViewHolder();
}

@Override
protected int getDefaultLayout() {
    return R.layout.inventroy_item_layout;
}

private DrawableCrossFadeFactory factory =
        new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();

@Override
public void bind(@NonNull InventoryProductDetailModel.ViewHolder holder) {
    super.bind(holder);
    holder.quantity.setText(String.format("Available :%d", product.getTotalStock()));
    holder.brand.setText(product.getProduct().getBrandName());
    holder.title.setText(product.getProduct().getTitle());
    holder.category.setText(product.getProduct().getCategoryName());
    holder.sku.setText(String.format("Sku: %s", product.getSku()));
    holder.inventoryItemConstrainLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ProductDetailActivity.class);
            intent.putExtra("product_id", product.getId());
            context.startActivity(intent);
        }
    });
    if (product.getProductCombos() != null && product.getProductCombos().size() > 0) {
        holder.variationCount.setVisibility(View.GONE);
        holder.comboBtn.setVisibility(View.VISIBLE);
        holder.comboBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callbacks.onViewComboClick(product);
            }
        });
    }
    if (product.getSellerActive()) {
        holder.productStatusSwitch.setText("Active");
        holder.productStatusSwitch.setOnCheckedChangeListener(null);
        holder.productStatusSwitch.setChecked(true);
        holder.productStatusSwitch.setOnCheckedChangeListener(this);
        holder.productStatusSwitch.setTextColor(context.getResources().getColor(R.color.colorAccent));
    } else {
        holder.productStatusSwitch.setText("Inactive");
        holder.productStatusSwitch.setOnCheckedChangeListener(null);
        holder.productStatusSwitch.setChecked(false);
        holder.productStatusSwitch.setOnCheckedChangeListener(this);
        holder.productStatusSwitch.setTextColor(Color.parseColor("#ff0000"));
    }
    holder.variationCount.setText(format("Variation(%d)", product.getVariantCount()));
    holder.variationCount.setVisibility(View.VISIBLE);
    holder.comboBtn.setVisibility(View.GONE);
    loadImage(holder.productImage, Utils.getRequiredUrlForThisImage(holder.productImage, product.getProduct().getImage()));
    if (position == listSize - 2) {
        callbacks.onRecyclerViewReachEnd();
    }
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    callbacks.onProductListingStatusChanged(isChecked, product.getId());
}

private void loadImage(ImageView imageView, String url) {
    Glide.with(imageView.getContext()).asBitmap()
            .load(Utils.getRequiredUrlForThisImage(imageView, url))
            .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .fitCenter())
            .transition(withCrossFade(factory))
            .placeholder(R.mipmap.product)
            .into(imageView);
}

@Override
public void unbind(@NonNull InventoryProductDetailModel.ViewHolder holder) {
    super.unbind(holder);
}


public static class ViewHolder extends EpoxyHolder {

    TextView quantity, brand, title, category, variationCount, comboBtn;
    ImageView productImage, btn_product_detail;
    ProgressBar progressBar;
    ConstraintLayout inventoryItemConstrainLayout;
    private TextView sku;
    private Switch productStatusSwitch;

    @Override
    protected void bindView(@NonNull View itemView) {
        productStatusSwitch = itemView.findViewById(R.id.productStatusSwitch);
        quantity = itemView.findViewById(R.id.product_qty);
        brand = itemView.findViewById(R.id.product_brand);
        title = itemView.findViewById(R.id.product_title);
        sku = itemView.findViewById(R.id.sku);
        category = itemView.findViewById(R.id.product_category);
        variationCount = itemView.findViewById(R.id.variantCount);
        productImage = itemView.findViewById(R.id.product_image);
        btn_product_detail = itemView.findViewById(R.id.btn_product_detail);
        inventoryItemConstrainLayout = itemView.findViewById(R.id.inventory_item_constrain_layout);
        comboBtn = itemView.findViewById(R.id.combo_btn);
        progressBar = itemView.findViewById(R.id.progressbar);
        progressBar.setVisibility(View.GONE);
    }
}

@Override
public int hashCode() {
    super.hashCode();
    return product.hashCode();
}

@Override
public boolean equals(Object o) {
    return super.equals(o);
}

}

private void addProductListingChangeObserver(final Boolean newStatus, final int productSellerId) {
    ProductUpdate productUpdate = new ProductUpdate();
    productUpdate.setSellerActive(newStatus);
    mInventoryViewModel.updateProductSeller(productSellerId, productUpdate).observe(this, new Observer<Resource<ProductSeller>>() {
        @Override
        public void onChanged(Resource<ProductSeller> productSellerResource) {
            if (productSellerResource.status == Status.ERROR) {
                progressBar.setVisibility(View.GONE);
            } else if (productSellerResource.status == Status.SUCCESS) {
                progressBar.setVisibility(View.GONE);
                if (productSellerResource.data != null && productSellerResource.data.isSellerActive() == newStatus) {
                    for (int i = 0; i < productList.size(); i++) {
                        if (productList.get(i).getId() == productSellerId) {
                            productList.get(i).setSellerActive(newStatus);
                            break;
                        }
                    }
                    sellerInventoryListEpoxyController.setProductList(productList, getContext(), InventoryFragment.this);
                }
            } else {
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    });
}

В функции addProductListingChangeObserver () один объект productList изменяется, и новый productList передается в EpoxyController и вызывается requestModelbuild, но представление не изменяется должным образом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...