Skip to content

Commit

Permalink
Add to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
aggarwalcode committed Mar 10, 2018
1 parent 18b0828 commit d4f92ef
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

Expand Down Expand Up @@ -41,6 +39,8 @@ public class AddToCart extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final String TAG = "AddToCart";

public Button addToCart;

// TODO: Rename and change types of parameters
Expand Down Expand Up @@ -81,7 +81,6 @@ public void onCreate(Bundle savedInstanceState) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}

}

@Override
Expand All @@ -97,6 +96,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onClick(View v) {//!(Arrays.asList(FragmentCart.prodBuyArray).contains(mParam1))
if(!(Arrays.asList(fragmentCart.prodBuyArray).contains(mParam1)))
FragmentCart.keysAddedToCart.add(mParam1);
FragmentCart frg = new FragmentCart();
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
return;
}
});

Expand All @@ -105,10 +110,10 @@ public void onClick(View v) {//!(Arrays.asList(FragmentCart.prodBuyArray).contai
mDatabaseRef.child(mParam1).child("allimages").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
allImages.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
String upload = (String) postSnapshot.getValue();
allImages.add(upload);
//stockArr = allImages.toArray(stockArr);
stringArray = allImages.toArray(new String[0]);
}
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class FragmentCart extends Fragment {
public ProductsAdapter mAdapter;
public List<ProductsHolder> mProducts = new ArrayList <ProductsHolder>();
public RecyclerView recyclerViewProducts;
public static final String TAG = "FragmentCart";

private OnFragmentInteractionListener mListener;

Expand Down Expand Up @@ -96,10 +97,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Products");
for(int i =0; i<prodBuyArray.length; i++) {
prodn = prodBuyArray[i];

mProducts.clear();
mDatabaseRef.child(prodn).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//mProducts.clear();
product = dataSnapshot.getValue(ProductsHolder.class);
mProducts.add(product);
mAdapter = new ProductsAdapter(getActivity(),mProducts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
Expand All @@ -22,7 +20,6 @@
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
Expand All @@ -41,8 +38,7 @@ public class MainLandingPage extends AppCompatActivity
AddToCart.OnFragmentInteractionListener,
FragmentCart.OnFragmentInteractionListener {

private static final String TAG = "EcomLanding";

public static final String TAG = "EcomLanding";
RecyclerView recyclerViewEcom;
ImageAdapter mAdapter;
private DatabaseReference mDatabaseRef;
Expand All @@ -51,13 +47,16 @@ public class MainLandingPage extends AppCompatActivity
android.widget.SearchView searchView;
ImageButton shopByCatBut;
FragmentCart fragmentCart = FragmentCart.newInstance(null, null);
TextView textCartItemCount;
int mCartItemCount = 10;
Toolbar toolbar;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_landing_page);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
Toolbar toolbarSecond = (Toolbar) findViewById(R.id.toolbarSecond);
shopByCatBut = (ImageButton) findViewById(R.id.shopByCatBut);

Expand All @@ -75,15 +74,14 @@ protected void onCreate(Bundle savedInstanceState) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);


//Calling fragment Here
shopByCatBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ItemFragmentCat frag = new ItemFragmentCat();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,frag,"Test Fragment");
transaction.replace(R.id.container,frag,"ItemFragmentCat");
transaction.commit();
transaction.addToBackStack(null);
}
Expand Down Expand Up @@ -133,8 +131,11 @@ public void onCancelled(DatabaseError databaseError) {
}
})
);

mCartItemCount = FragmentCart.keysAddedToCart.size();
}


@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Expand All @@ -151,7 +152,23 @@ public void onBackPressed() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.ecom_landing, menu);

final MenuItem menuItem = menu.findItem(R.id.action_drawer_cart);

View actionView = MenuItemCompat.getActionView(menuItem);
textCartItemCount = (TextView) actionView.findViewById(R.id.cart_badge);

setupBadge();

actionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
});

return true;
}

Expand All @@ -163,7 +180,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
FragmentCart frag = new FragmentCart();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,frag,"Cart Fragment");
transaction.replace(R.id.container,frag,"FragmentCart");
transaction.commit();
transaction.addToBackStack(null);

Expand All @@ -174,9 +191,26 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
*/
invalidateOptionsMenu();
return super.onOptionsItemSelected(item);
}

private void setupBadge() {
if (textCartItemCount != null) {
if (mCartItemCount == 0) {
if (textCartItemCount.getVisibility() != View.GONE) {
textCartItemCount.setVisibility(View.GONE);
}
} else {
textCartItemCount.setText(String.valueOf(Math.min(mCartItemCount, 99)));
if (textCartItemCount.getVisibility() != View.VISIBLE) {
textCartItemCount.setVisibility(View.VISIBLE);
}
}
invalidateOptionsMenu();
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Expand Down Expand Up @@ -219,6 +253,6 @@ public void onFragmentInteraction(Uri uri) {
}

public void homePage(View view) {
startActivity(new Intent(MainLandingPage.this, MainLandingPage.class));
//startActivity(new Intent(MainLandingPage.this, MainLandingPage.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ProductsListFragment extends Fragment implements AddToCart.OnFragme
// TODO: Rename and change types of parameters
public String mParam1;
public String mParam2;
public static final String TAG = "ProductsListFragment";
public OnFragmentInteractionListener mListener;

public ProductsListFragment() {
Expand Down Expand Up @@ -90,12 +91,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
//recyclerViewProducts.setHasFixedSize(true);

mDatabaseRef = FirebaseDatabase.getInstance().getReference("Products");

mProducts.clear();
//Change categoty1 to mParam1
mDatabaseRef.orderByChild("other").startAt(mParam1).endAt(mParam1).addChildEventListener(new ChildEventListener() {

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

//mProducts.clear();
product = dataSnapshot.getValue(ProductsHolder.class);
product.key = dataSnapshot.getKey();
mProducts.add(product);
Expand Down Expand Up @@ -132,7 +135,7 @@ public void onCancelled(DatabaseError databaseError) {
AddToCart frag = AddToCart.newInstance(mProducts.get(position).getKey(),"Param Two");
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,frag,"AddToCart Fragment");
transaction.replace(R.id.container,frag,"AddToCart");
transaction.commit();
transaction.addToBackStack(null);
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/shape_notification.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#22000000" android:width="2dp"/>
<corners android:radius="5dp" />
<solid android:color="#CC0001"/>
</shape>
7 changes: 3 additions & 4 deletions app/src/main/res/layout/app_bar_ecom_landing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@

<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="homePage"
android:textSize="15sp"
android:text="Your Shop"/>


</LinearLayout>

</android.support.v7.widget.Toolbar>
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/feed_update_count.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@drawable/action_bar_cart"/>

<TextView
android:id="@+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="@drawable/shape_notification"
android:gravity="center"
android:padding="3dp"
android:textColor="@android:color/white"
android:text="0"
android:textSize="10sp"/>

</FrameLayout>
6 changes: 4 additions & 2 deletions app/src/main/res/menu/ecom_landing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<menu xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto">


<item
android:id="@+id/action_drawer_cart"
android:icon="@drawable/action_bar_cart"
android:orderInCategory="200"
android:orderInCategory="1"
android:title="@string/cart"
app:showAsAction="ifRoom" />
app:actionLayout="@layout/feed_update_count"
app:showAsAction="always" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="cart">Cart</string>
<string name="cartb">cartb</string>

</resources>

0 comments on commit d4f92ef

Please sign in to comment.