Skip to content

Instantly share code, notes, and snippets.

@rhlmshr
Last active November 10, 2017 21:55
Show Gist options
  • Save rhlmshr/5f02062b1535b2e1916bf61e1cec550e to your computer and use it in GitHub Desktop.
Save rhlmshr/5f02062b1535b2e1916bf61e1cec550e to your computer and use it in GitHub Desktop.
package com.rahulmishra.sarasmelaapp.activites;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ValueEventListener;
import com.mancj.materialsearchbar.MaterialSearchBar;
import com.rahulmishra.sarasmelaapp.R;
import com.rahulmishra.sarasmelaapp.Utils.GridAutoFitLayoutManager;
import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.google.firebase.database.DatabaseReference;
import com.wang.avi.AVLoadingIndicatorView;
public class CategoryActivity extends AppCompatActivity {
TextView category_title;
Button category_filter;
MaterialSearchBar categorySearchBar;
RecyclerView.Adapter productsRecyclerAdapter;
private DatabaseReference mDatabase;
private ProgressDialog progressDialog;
List<ProductEntity> productList;
RecyclerView products;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Intent intent = getIntent();
category_title = findViewById(R.id.category_title);
category_title.setText(intent.getStringExtra("title"));
productList = new ArrayList<>();
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
products = findViewById(R.id.product_catalog);
mDatabase = FirebaseDatabase.getInstance().getReference("products");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
progressDialog.dismiss();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ProductEntity upload = postSnapshot.getValue(ProductEntity.class);
Log.d("error", "onDataChange: " + upload.about);
List<ProductImage> images = new ArrayList<>() ;
for(DataSnapshot datas : postSnapshot.child("images").getChildren()){
ProductImage upload_image = datas.getValue(ProductImage.class);
images.add(upload_image) ;
}
upload.image = images ;
productList.add(upload);
}
GridAutoFitLayoutManager layoutManager = new GridAutoFitLayoutManager(getBaseContext(), 240);
products.setLayoutManager(layoutManager);
products.setNestedScrollingEnabled(false);
productsRecyclerAdapter = new ProductsRecyclerAdapter(productList, CategoryActivity.this);
products.setAdapter(productsRecyclerAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
NestedScrollView root_layout = findViewById(R.id.category_root_layout);
Snackbar.make(root_layout, "Internal Error!!", Snackbar.LENGTH_SHORT).show();
}
});
category_filter = findViewById(R.id.category_filter);
category_filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(CategoryActivity.this, category_filter);
popupMenu.getMenuInflater().inflate(R.menu.filter_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Collections.sort(productList, new Comparator<ProductEntity>() {
@Override
public int compare(ProductEntity lhs, ProductEntity rhs) {
return lhs.about.compareTo(rhs.about);
}
});
productsRecyclerAdapter.notifyDataSetChanged();
return true;
}
});
popupMenu.show();
}
});
categorySearchBar = findViewById(R.id.categorySearchBar);
categorySearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//// TODO: 09-11-2017
// send the entered text to our filter and let it manage everything
productsRecyclerAdapter.getFilter().filter(categorySearchBar.getText());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.cart:
Intent intent = new Intent(CategoryActivity.this, Wishlist.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// void feed_data() {
// final List<ProductEntity> feededProducts = new ArrayList<>();
// feededProducts.add(new ProductEntity(10001, "Marble Chowki pair with intricate floral Painting by Handicrafts Paradise",
// "09171430513",
// "Rajasthan Handicraft And Textiles, Opposite Dada Bari, amer, Sagar Rd, Devisinghpura, Rajasthan 302028",
// "Handicraft",
// new ArrayList<ProductImage>() {{
// add(new ProductImage("https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fgramophone.jpg?alt=media&token=8a5cb8a1-4786-4d1b-b5cc-1072de9331de"));
// add(new ProductImage("https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fgramophone1.jpg?alt=media&token=6992be5f-2153-4cc5-89ef-1e36850ae21f"));
// }}, "Stall No. 4, Saras Mela, Patna"
// )) ;
// feededProducts.add(new ProductEntity(10002, "Generic ITOS365 Handmade Vintage Dummy Gramophone Only For Home Décor",
// "01412630837",
// "Near Rmgarh Turn, Amber Road, Sanganeer, Amber Road, Jaipur, Rajasthan 302002",
// "Handicraft",
// new ArrayList<ProductImage>() {{
// add(new ProductImage("https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fchowki.jpg?alt=media&token=a74bf78e-f5ad-410c-aa28-9cbcacf386c4"));
// add(new ProductImage("https://firebasestorage.googleapis.com/v0/b/sarasmelaapp.appspot.com/o/products_images%2Fchowki1.jpg?alt=media&token=f91b064f-98fa-4504-91ff-ef56b43ab1fc"));
// }}, "Stall No. 56, Saras Mela, Patna"
// )) ;
// }
}
class ProductEntity implements Parcelable {
int id;
String about;
String contact_vendor;
String vendor_address;
String category;
List<ProductImage> image;
public String shop_location;
public ProductEntity(int id, String about, String contact_vendor,
String vendor_address, String category, ArrayList<ProductImage> image, String shop_location) {
this.id = id;
this.about = about;
this.contact_vendor = contact_vendor;
this.vendor_address = vendor_address;
this.category = category;
this.image = image;
this.shop_location = shop_location;
}
public ProductEntity() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.about);
dest.writeString(this.contact_vendor);
dest.writeString(this.vendor_address);
dest.writeString(this.category);
dest.writeTypedList(this.image);
dest.writeString(this.shop_location);
}
protected ProductEntity(Parcel in) {
this.id = in.readInt();
this.about = in.readString();
this.contact_vendor = in.readString();
this.vendor_address = in.readString();
this.category = in.readString();
this.image = in.createTypedArrayList(ProductImage.CREATOR);
this.shop_location = in.readString();
}
public static final Creator<ProductEntity> CREATOR = new Creator<ProductEntity>() {
@Override
public ProductEntity createFromParcel(Parcel source) {
return new ProductEntity(source);
}
@Override
public ProductEntity[] newArray(int size) {
return new ProductEntity[size];
}
};
}
class ProductsRecyclerAdapter extends RecyclerView.Adapter<ProductsRecyclerAdapter.MyViewHolder> implements Filterable {
List<ProductEntity> productList = Collections.emptyList();
Context context;
List<ProductEntity> filteredProductList = Collections.emptyList();
public ProductsRecyclerAdapter(List<ProductEntity> productList, Context context) {
this.productList = productList;
this.context = context;
filteredProductList = productList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView product_name;
ImageView product_image;
CardView product_catalog_card;
AVLoadingIndicatorView progress_ball_image ;
public MyViewHolder(View view) {
super(view);
product_name = view.findViewById(R.id.product_name);
product_image = view.findViewById(R.id.product_image);
product_catalog_card = view.findViewById(R.id.product_catalog_card);
progress_ball_image = view.findViewById(R.id.progress_ball_image) ;
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_entity_design, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.product_name.setText(productList.get(position).about);
holder.progress_ball_image.setVisibility(View.VISIBLE) ;
Picasso.with(context)
.load(productList.get(position).image.get(0).url)
.centerInside()
.resize(200, 200)
.memoryPolicy(MemoryPolicy.NO_STORE)
.into(holder.product_image, new Callback() {
@Override
public void onSuccess() {
holder.progress_ball_image.smoothToHide(); ;
}
@Override
public void onError() {
}
});
holder.product_catalog_card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ProductDetail.class);
intent.putExtra("product", productList.get(position));
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
filteredProductList = productList;
} else {
ArrayList<ProductEntity> filteredList = new ArrayList<>();
for (ProductEntity productEntity : productList) {
if (productEntity.about.toLowerCase().contains(charString)) {
filteredList.add(productEntity);
}
}
filteredProductList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredProductList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredProductList = (ArrayList<ProductEntity>) filterResults.values;
notifyDataSetChanged();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment