Search in sources :

Example 6 with Product

use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product in project mosby by sockeqwe.

the class DetailsInteractor method getProductWithShoppingCartInfo.

private Observable<ProductDetail> getProductWithShoppingCartInfo(int productId) {
    List<Observable<?>> observables = Arrays.asList(backendApi.getProduct(productId), shoppingCart.itemsInShoppingCart());
    return Observable.combineLatest(observables, objects -> {
        Product product = (Product) objects[0];
        List<Product> productsInShoppingCart = (List<Product>) objects[1];
        boolean inShoppingCart = false;
        for (Product p : productsInShoppingCart) {
            if (p.getId() == productId) {
                inShoppingCart = true;
                break;
            }
        }
        return new ProductDetail(product, inShoppingCart);
    });
}
Also used : ProductDetail(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.ProductDetail) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) List(java.util.List) Observable(io.reactivex.Observable)

Example 7 with Product

use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product in project mosby by sockeqwe.

the class ShoppingCartItemViewHolder method bind.

public void bind(ShoppingCartOverviewItem item) {
    this.item = item;
    Product product = item.getProduct();
    Glide.with(itemView.getContext()).load(DependencyInjection.BASE_IMAGE_URL + product.getImage()).centerCrop().into(image);
    name.setText(product.getName());
    price.setText(String.format(Locale.US, "$ %.2f", product.getPrice()));
    if (item.isSelected()) {
        if (Build.VERSION.SDK_INT >= 23) {
            itemView.setForeground(selectedDrawable);
        } else {
            itemView.setBackground(selectedDrawable);
        }
    } else {
        if (Build.VERSION.SDK_INT >= 23) {
            itemView.setForeground(null);
        } else {
            itemView.setBackground(null);
        }
    }
}
Also used : Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product)

Example 8 with Product

use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product in project mosby by sockeqwe.

the class ProductBackendApiDecorator method getAllCategories.

/**
 * Get a list with all categories
 */
public Observable<List<String>> getAllCategories() {
    return getAllProducts().map(products -> {
        Set<String> categories = new HashSet<String>();
        for (Product p : products) {
            categories.add(p.getCategory());
        }
        List<String> result = new ArrayList<String>(categories.size());
        result.addAll(categories);
        return result;
    });
}
Also used : ArrayList(java.util.ArrayList) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) HashSet(java.util.HashSet)

Example 9 with Product

use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product in project mosby by sockeqwe.

the class ShoppingCartOverviewFragment method setUpItemTouchHelper.

private void setUpItemTouchHelper() {
    // 
    // Borrowed from https://github.com/nemanja-kovacevic/recycler-view-swipe-to-delete/blob/master/app/src/main/java/net/nemanjakovacevic/recyclerviewswipetodelete/MainActivity.java
    // 
    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        // we want to cache these and not allocate anything repeatedly in the onChildDraw method
        Drawable background;

        Drawable xMark;

        int xMarkMargin;

        boolean initiated;

        private void init() {
            background = new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.delete_background));
            xMark = ContextCompat.getDrawable(getActivity(), R.drawable.ic_remove);
            xMarkMargin = (int) getActivity().getResources().getDimension(R.dimen.ic_clear_margin);
            initiated = true;
        }

        // not important, we don't want drag & drop
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            int swipedPosition = viewHolder.getAdapterPosition();
            Product productAt = adapter.getProductAt(swipedPosition);
            removeRelay.onNext(productAt);
        }

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            View itemView = viewHolder.itemView;
            // not sure why, but this method get's called for viewholder that are already swiped away
            if (viewHolder.getAdapterPosition() == -1) {
                // not interested in those
                return;
            }
            if (!initiated) {
                init();
            }
            // draw red background
            background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
            background.draw(c);
            // draw x mark
            int itemHeight = itemView.getBottom() - itemView.getTop();
            int intrinsicWidth = xMark.getIntrinsicWidth();
            int intrinsicHeight = xMark.getIntrinsicWidth();
            int xMarkLeft = itemView.getRight() - xMarkMargin - intrinsicWidth;
            int xMarkRight = itemView.getRight() - xMarkMargin;
            int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
            int xMarkBottom = xMarkTop + intrinsicHeight;
            xMark.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom);
            // xMark.draw(c);
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    };
    ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    mItemTouchHelper.attachToRecyclerView(recyclerView);
}
Also used : ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) ColorDrawable(android.graphics.drawable.ColorDrawable) Canvas(android.graphics.Canvas) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) RecyclerView(android.support.v7.widget.RecyclerView) BindView(butterknife.BindView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 10 with Product

use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product in project mosby by sockeqwe.

the class HomePresenterTest method loadingFirstPageAndFailLoadingNextPage.

@Test
public void loadingFirstPageAndFailLoadingNextPage() throws Exception {
    // 
    // Prepare mock server to deliver mock response on incoming http request
    // 
    List<Product> mockProductsFirstPage = Arrays.asList(new Product(1, "image", "name", "category1", "description", 21.9), new Product(2, "image", "name", "category1", "description", 21.9), new Product(3, "image", "name", "category1", "description", 21.9), new Product(4, "image", "name", "category1", "description", 21.9), new Product(5, "image", "name", "category1", "description", 21.9));
    mockWebServer.enqueue(new MockResponse().setBody(adapter.toJson(mockProductsFirstPage)));
    // 
    // init the robot to drive to View which triggers intents on the presenter
    // 
    HomePresenter presenter = // In a real app you could use dagger or instantiate the Presenter manually like new HomePresenter(...)
    new DependencyInjection().newHomePresenter();
    HomeViewRobot robot = new HomeViewRobot(presenter);
    // 
    // We are ready, so let's start: fire intents
    // 
    robot.fireLoadFirstPageIntent();
    // 
    // we expect that 2 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator
    // 2. show the items with the first page
    // 
    List<FeedItem> expectedDataAfterFristPage = Arrays.asList(new SectionHeader("category1"), mockProductsFirstPage.get(0), mockProductsFirstPage.get(1), mockProductsFirstPage.get(2), new AdditionalItemsLoadable(2, "category1", false, null));
    HomeViewState loadingFirstPage = new HomeViewState.Builder().firstPageLoading(true).build();
    HomeViewState firstPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage);
    // 
    // Fire second intent
    // 
    // causes loading next page error
    mockWebServer.shutdown();
    robot.fireLoadNextPageIntent();
    // 
    // we expect that 4 view.render() events happened with the following HomeViewState:
    // 1. show loading indicator (caused by loadFirstPageIntent)
    // 2. show the items with the first page (caused by loadFirstPageIntent)
    // 3. show loading next page indicator
    // 4. show next page error (plus original first page content)
    // 
    HomeViewState nextPageLoading = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageLoading(true).build();
    HomeViewState nextPage = new HomeViewState.Builder().data(expectedDataAfterFristPage).nextPageError(new ConnectException()).build();
    // Check if as expected
    robot.assertViewStateRendered(loadingFirstPage, firstPage, nextPageLoading, nextPage);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Product(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product) DependencyInjection(com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection) FeedItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem) SectionHeader(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader) AdditionalItemsLoadable(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

Product (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.Product)11 AdditionalItemsLoadable (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.AdditionalItemsLoadable)7 FeedItem (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem)7 SectionHeader (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.SectionHeader)7 DependencyInjection (com.hannesdorfmann.mosby3.sample.mvi.dependencyinjection.DependencyInjection)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 Test (org.junit.Test)7 ConnectException (java.net.ConnectException)3 Canvas (android.graphics.Canvas)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 View (android.view.View)1 BindView (butterknife.BindView)1 ProductDetail (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.ProductDetail)1 Observable (io.reactivex.Observable)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1