Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wangerekaharun/14be3d7b6f2e8cd1cae88e37381ca6f9 to your computer and use it in GitHub Desktop.
Save wangerekaharun/14be3d7b6f2e8cd1cae88e37381ca6f9 to your computer and use it in GitHub Desktop.
// Adapter Class
package ke.co.appslab.letafood.adapters;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import ke.co.appslab.letafood.R;
import ke.co.appslab.letafood.callbacks.FoodCartListener;
import ke.co.appslab.letafood.models.Menu;
import ke.co.appslab.letafood.utils.Utils;
import static ke.co.appslab.letafood.api.ApiEndpoints.IMAGE_URL;
public class RestaurantMenuAdapter extends RecyclerView.Adapter<RestaurantMenuAdapter.MyViewHolder>{
private List<Menu> foodModelList;
private Context context;
private FoodCartListener listener;
private static int totalCartItems = 0;
public RestaurantMenuAdapter(List<Menu> foodModelList, Context context, FoodCartListener listener) {
this.foodModelList = foodModelList;
this.context = context;
this.listener = listener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.image)
ImageView foodMenuImg;
@BindView(R.id.name)
TextView foodMenuTitleText;
@BindView(R.id.price)
TextView foodMenuPriceText;
@BindView(R.id.description)
TextView foodMenuDescText;
@BindView(R.id.add_cart)
ImageButton addCart;
@BindView(R.id.remove_cart)
ImageButton removeCart;
@BindView(R.id.number_in_cart)
TextView numberInCart;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
addCart.setOnClickListener(v -> {
listener.onAdd(getAdapterPosition());
});
removeCart.setOnClickListener(v -> listener.remove(getAdapterPosition()));
itemView.setOnClickListener(v -> listener.onMenuClicked(getAdapterPosition()));
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_menu_details_2, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Menu foodItem = foodModelList.get(position);
Glide.with(context).load(IMAGE_URL + foodItem.getPhoto()).apply(new RequestOptions().placeholder(R.drawable.food)).into(holder.foodMenuImg);
holder.foodMenuTitleText.setText(foodItem.getName());
holder.foodMenuPriceText.setText(Utils.formatNumber(foodItem.getPrice()));
holder.foodMenuDescText.setText(foodItem.getDescription());
holder.numberInCart.setText(String.valueOf(foodItem.getNumberInCart()));
}
@Override
public int getItemCount() {
return foodModelList.size();
}
}
// Fragment Class
package ke.co.appslab.letafood.views.fragments;
import android.app.AlertDialog;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.snackbar.Snackbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ke.co.appslab.letafood.R;
import ke.co.appslab.letafood.adapters.RestaurantMenuAdapter;
import ke.co.appslab.letafood.callbacks.FoodCartListener;
import ke.co.appslab.letafood.models.CartModel;
import ke.co.appslab.letafood.models.Menu;
import ke.co.appslab.letafood.ui.ConnectionReceiver;
import ke.co.appslab.letafood.utils.LetaFood;
import ke.co.appslab.letafood.utils.Utils;
import ke.co.appslab.letafood.viewmodels.CartViewModel;
import static ke.co.appslab.letafood.api.ApiEndpoints.IMAGE_URL;
public class HotelMenuFragment extends Fragment implements FoodCartListener, ConnectionReceiver.ConnectionReceiverListener {
static RecyclerView.LayoutManager mLayoutManager;
@BindView(R.id.restaurantMenuRv)
RecyclerView restaurantMenuRv;
RestaurantMenuAdapter restaurantMenuAdapter;
Unbinder unbinder;
String restName, restSlogan, image;
int hotelId, restRating;
List<Menu> allMenus;
private CartViewModel cartViewModel;
List<CartModel> cartModelList = new ArrayList<>();
@BindView(R.id.container)
CoordinatorLayout coordinatorLayout;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hotel_menu, container, false);
unbinder = ButterKnife.bind(this, view);
if(!checkConnection()){
noInternetSnackBar();
}
cartViewModel = ViewModelProviders.of(this).get(CartViewModel.class);
Bundle bundle = this.getArguments();
if (bundle != null) {
String list = bundle.getString("menu_list");
restName = bundle.getString("restName");
restSlogan = bundle.getString("restSlogan");
image = bundle.getString("restImage");
hotelId = bundle.getInt("hotelId");
restRating = bundle.getInt("restRating");
Gson gson = new Gson();
allMenus = gson.fromJson(list, new TypeToken<ArrayList<Menu>>() {
}.getType());
initView();
}
cartViewModel.getListLiveData().observe(this, cartModelList1 -> cartModelList = cartModelList1);
return view;
}
public void initView() {
List<Menu> transfomer = cartViewModel.cartTransfomer(allMenus);
if(!transfomer.isEmpty()){
allMenus.clear();
allMenus.addAll(transfomer);
}
restaurantMenuAdapter = new RestaurantMenuAdapter(allMenus, getActivity(), this);
mLayoutManager = new LinearLayoutManager(getActivity());
restaurantMenuRv.setLayoutManager(mLayoutManager);
restaurantMenuRv.setItemAnimator(new DefaultItemAnimator());
restaurantMenuRv.setAdapter(restaurantMenuAdapter);
restaurantMenuRv.setNestedScrollingEnabled(true);
}
@Override
public void onAdd(int position) {
Menu menu = allMenus.get(position);
//if menu exists in cart, update number in cart else add new CartViewModel to cart
CartModel cartModel=new CartModel();
cartModel.setCartNoOfItems(menu.getNumberInCart()+1);
cartModel.setMenuId(menu.getId());
cartModel.setCartFoodPrice(Integer.parseInt(menu.getPrice()));
cartModel.setRestaurantId(Integer.parseInt(menu.getHotelId()));
cartModel.setCartFoodTitle(menu.getName());
cartModel.setFoodImage(menu.getPhoto());
cartModel.setRestaurantName(restName);
cartViewModel.addItemsToCart(cartModel);
numberInCart(menu.getId(),position);
}
@Override
public void remove(int position) {
Menu menu = allMenus.get(position);
//check if exist in cart
//remove
cartViewModel.delete(menu.getId());
numberInCart(menu.getId(),position);
}
@Override
public void onMenuClicked(int position) {
showMenuDetails(position);
}
private void showMenuDetails(int position) {
Menu menu = allMenus.get(position);
AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
View view=getLayoutInflater().inflate(R.layout.product_details,null,false);
ImageView imageView=view.findViewById(R.id.image);
TextView name=view.findViewById(R.id.name);
TextView description=view.findViewById(R.id.description);
Glide.with(getContext()).load(IMAGE_URL + menu.getPhoto()).into(imageView);
name.setText(menu.getName());
description.setText(menu.getDescription());
TextView numInCart=view.findViewById(R.id.number_in_cart);
numInCart.setText(""+menu.getNumberInCart());
ImageButton addCart=view.findViewById(R.id.add_cart);
addCart.setOnClickListener(v->{
int num=menu.getNumberInCart();
num+=1;
menu.setNumberInCart(num);
onAdd(position);
numInCart.setText(""+menu.getNumberInCart());
});
ImageButton removeCart=view.findViewById(R.id.remove_cart);
removeCart.setOnClickListener(v->{
int num=menu.getNumberInCart();
if(num>0){
num-=1;
menu.setNumberInCart(num);
remove(position);
numInCart.setText(""+menu.getNumberInCart());
}
});
TextView category=view.findViewById(R.id.category);
category.setText(menu.getCategory());
TextView price=view.findViewById(R.id.price);
price.setText(Utils.formatNumber(menu.getPrice()));
ImageButton close=view.findViewById(R.id.close_btn);
builder.setView(view);
AlertDialog alertDialog = builder.create();
close.setOnClickListener(v-> alertDialog.dismiss());
alertDialog.show();
}
private void numberInCart(int id, int position) {
cartViewModel.countInCart(id).observe(this, integer -> {
//update position in adapter
allMenus.get(position).setNumberInCart(integer);
restaurantMenuAdapter.notifyItemChanged(position,allMenus.get(position));
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onResume() {
super.onResume();
LetaFood.getInstance().setConnectionListener(this);
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
if (!isConnected) {
Snackbar snackbar = Snackbar.make(coordinatorLayout, "You are currently offline", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("RETRY", view -> {
reloadFragment();
});
snackbar.show();
}
}
private void noInternetSnackBar() {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "You are currently offline", Snackbar.LENGTH_INDEFINITE)
.setAction("RETRY", view -> {
reloadFragment();
});
snackbar.show();
}
private boolean checkConnection() {
return ConnectionReceiver.isConnected();
}
private void reloadFragment() {
Fragment newFragment = new HomeFragment();
FragmentTransaction ft = null;
if (getFragmentManager() != null) {
ft = getFragmentManager().beginTransaction();
}
if (ft != null) {
ft.replace(R.id.content_home, newFragment).commit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment