Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Sabrina/recycler view adapter #18

Merged
merged 2 commits into from Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,82 @@
package com.fullstorydev.shoppedemo.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.fullstorydev.shoppedemo.ui.market.MarketEventHandlers;
import com.fullstorydev.shoppedemo.R;
import com.fullstorydev.shoppedemo.data.Product;
import com.fullstorydev.shoppedemo.databinding.ListItemMarketBinding;

import java.util.ArrayList;
import java.util.List;

public class MarketProductAdapter extends RecyclerView.Adapter<MarketProductAdapter.MarketProductViewHolder> {
private List<Product> mProductList;
private LayoutInflater layoutInflater;
private MarketEventHandlers mMarketHandlers;

public MarketProductAdapter(MarketEventHandlers marketHandlers) {
mMarketHandlers = marketHandlers;
mProductList = new ArrayList<>();
}

@NonNull
@Override
public MarketProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (layoutInflater == null) {
layoutInflater = LayoutInflater.from(parent.getContext());
}
// binding class automatically generated by Data Binding Library from list_item_market
ListItemMarketBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item_market, parent, false);
return new MarketProductViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull MarketProductViewHolder holder, int position) {
holder.bind(mProductList.get(position));
}

@Override
public int getItemCount() {
if(mProductList==null) return 0;
return mProductList.size();
}

public void setProductList(List<Product> products){
//TODO: check diff and use callback & DiffUtil.DiffResult to avoid recreating every view
mProductList = products;
notifyDataSetChanged();
}

class MarketProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ListItemMarketBinding binding;
MarketProductViewHolder(@NonNull ListItemMarketBinding binding){
super(binding.getRoot());
this.binding = binding;
// bind the on click handler for add to cart buttons
this.binding.setHandlers(mMarketHandlers);
}

void bind(Product product) {
binding.setProduct(product);
binding.executePendingBindings();
}

@Override
public void onClick(View v) {
// getting clicked product from adapter position
Product product = mProductList.get(getAdapterPosition());
try {
//TODO: When recyclerview is clicked, get the product and perform action here, i.e. show product details fragment
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@@ -0,0 +1,8 @@
package com.fullstorydev.shoppedemo.ui.market;

import com.fullstorydev.shoppedemo.data.Product;

// interface for all action handlers for the market fragment
public interface MarketEventHandlers {
void onClickAddToCart(Product product);
}
Expand Up @@ -9,21 +9,28 @@
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.fullstorydev.shoppedemo.R;
import com.fullstorydev.shoppedemo.adapters.MarketProductAdapter;
import com.fullstorydev.shoppedemo.data.Product;

import java.util.List;

public class MarketFragment extends Fragment {
public class MarketFragment extends Fragment implements MarketEventHandlers {

private MarketViewModel marketViewModel;
private MarketProductAdapter mMarketProductAdapter;

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_market, container, false);

mMarketProductAdapter = new MarketProductAdapter(this);
RecyclerView mRecyclerView = root.findViewById(R.id.rv_product);
mRecyclerView.setAdapter(mMarketProductAdapter);

return root;
}

Expand All @@ -32,12 +39,15 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

marketViewModel = new ViewModelProvider(this).get(MarketViewModel.class);

marketViewModel.getProductList().observe(this.getViewLifecycleOwner(), new Observer<List<Product>>() {
@Override
public void onChanged(List<Product> products) {
// products holds the list of products, live data is updated when retrieved from API, this data will be used to populate the recycler view
mMarketProductAdapter.setProductList(products);
}
});
}

public void onClickAddToCart(Product product){
marketViewModel.increaseQuantityInCart(product);
}
}
Expand Up @@ -14,15 +14,17 @@ public class MarketViewModel extends AndroidViewModel {
private ProductRepository mRepository;
private LiveData<List<Product>> mProductList;


public MarketViewModel(Application application) {
super(application);

mRepository = new ProductRepository(application);
mProductList = mRepository.getAllFromAPI();
}

public LiveData<List<Product>> getProductList() {
return mProductList;
}

public void increaseQuantityInCart(Product product){
mRepository.increaseQuantityInCart(product);
}
}
4 changes: 3 additions & 1 deletion java/app/src/main/res/layout/fragment_market.xml
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_product" />
android:id="@+id/rv_product"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
4 changes: 4 additions & 0 deletions java/app/src/main/res/layout/list_item_market.xml
Expand Up @@ -6,6 +6,9 @@
<variable
name="product"
type="com.fullstorydev.shoppedemo.data.Product" />
<variable
name="handlers"
type="com.fullstorydev.shoppedemo.ui.market.MarketEventHandlers"/>
</data>

<LinearLayout
Expand Down Expand Up @@ -43,6 +46,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add_to_cart"
android:onClick="@{()->handlers.onClickAddToCart(product)}"
android:text="@string/add_to_cart">
</Button>
</LinearLayout>
Expand Down