Added firebase cart repo
This commit is contained in:
parent
37e1147615
commit
9dddd4713c
64
app/src/main/java/me/gilo/wc/common/QueryLiveData.java
Normal file
64
app/src/main/java/me/gilo/wc/common/QueryLiveData.java
Normal file
@ -0,0 +1,64 @@
|
||||
package me.gilo.wc.common;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.support.annotation.NonNull;
|
||||
import com.google.firebase.firestore.*;
|
||||
import me.gilo.wc.models.Model;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public final class QueryLiveData<T extends Model>
|
||||
extends LiveData<Resource<List<T>>> implements EventListener<QuerySnapshot> {
|
||||
|
||||
private final Query query;
|
||||
private final Class<T> type;
|
||||
private ListenerRegistration registration;
|
||||
|
||||
public QueryLiveData(Query query, Class<T> type) {
|
||||
this.query = query;
|
||||
this.type = type;
|
||||
|
||||
setValue(new Resource<>(Status.LOADING));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
|
||||
if (e != null) {
|
||||
setValue(new Resource<>(e));
|
||||
return;
|
||||
}
|
||||
setValue(new Resource<>(documentToList(snapshots)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActive() {
|
||||
super.onActive();
|
||||
registration = query.addSnapshotListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInactive() {
|
||||
super.onInactive();
|
||||
if (registration != null) {
|
||||
registration.remove();
|
||||
registration = null;
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<T> documentToList(QuerySnapshot snapshots) {
|
||||
final List<T> retList = new ArrayList<>();
|
||||
if (snapshots.isEmpty()) {
|
||||
return retList;
|
||||
}
|
||||
|
||||
for (DocumentSnapshot document : snapshots.getDocuments()) {
|
||||
retList.add(document.toObject(type).withId(document.getId()));
|
||||
}
|
||||
|
||||
return retList;
|
||||
}
|
||||
}
|
||||
48
app/src/main/java/me/gilo/wc/models/CartLineItem.java
Normal file
48
app/src/main/java/me/gilo/wc/models/CartLineItem.java
Normal file
@ -0,0 +1,48 @@
|
||||
package me.gilo.wc.models;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import me.gilo.woodroid.models.Metum;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class CartLineItem extends Model{
|
||||
|
||||
String id;
|
||||
public float price;
|
||||
public int quantity;
|
||||
public int productId;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public int getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(int productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
}
|
||||
37
app/src/main/java/me/gilo/wc/models/Model.java
Normal file
37
app/src/main/java/me/gilo/wc/models/Model.java
Normal file
@ -0,0 +1,37 @@
|
||||
package me.gilo.wc.models;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import com.google.firebase.firestore.IgnoreExtraProperties;
|
||||
import com.google.firebase.firestore.ServerTimestamp;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* A Base Model to be extended by other models to add ids.
|
||||
*/
|
||||
|
||||
@IgnoreExtraProperties
|
||||
public class Model implements Serializable {
|
||||
|
||||
public String id;
|
||||
|
||||
@ServerTimestamp
|
||||
private Date date_created = null;
|
||||
|
||||
|
||||
public <T extends Model> T withId(@NonNull final String id) {
|
||||
this.id = id;
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,13 +1,31 @@
|
||||
package me.gilo.wc.repo;
|
||||
|
||||
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.firestore.*;
|
||||
import com.google.firebase.storage.StorageReference;
|
||||
import com.google.firebase.storage.UploadTask;
|
||||
import me.gilo.wc.common.CompletionGenericLiveData;
|
||||
import me.gilo.wc.common.QueryLiveData;
|
||||
import me.gilo.wc.common.WooLiveData;
|
||||
import me.gilo.wc.models.CartLineItem;
|
||||
import me.gilo.wc.utils.AppUtils;
|
||||
import me.gilo.woodroid.Woocommerce;
|
||||
import me.gilo.woodroid.models.LineItem;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CartRepository {
|
||||
@ -15,21 +33,73 @@ public class CartRepository {
|
||||
@Inject
|
||||
Woocommerce woocommerce;
|
||||
|
||||
private final CollectionReference cart;
|
||||
|
||||
@Inject
|
||||
public CartRepository() {
|
||||
this.cart = FirebaseFirestore.getInstance()
|
||||
.collection("users")
|
||||
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
|
||||
.collection("cart");
|
||||
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(Context context, int productId) {
|
||||
final WooLiveData<Map<String, LineItem>> callBack = new WooLiveData();
|
||||
public QueryLiveData<CartLineItem> cart() {
|
||||
return new QueryLiveData<>(cart, CartLineItem.class);
|
||||
}
|
||||
|
||||
LineItem lineItem = new LineItem();
|
||||
public CompletionGenericLiveData<Void> deleteItem(CartLineItem cartLineItem) {
|
||||
final CompletionGenericLiveData<Void> completion = new CompletionGenericLiveData();
|
||||
cart.document(cartLineItem.getId()).delete().addOnCompleteListener(completion);
|
||||
|
||||
return completion;
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<Void> setQuantity(CartLineItem cartLineItem, int quantity) {
|
||||
final CompletionGenericLiveData<Void> completion = new CompletionGenericLiveData();
|
||||
cartLineItem.setQuantity(quantity);
|
||||
|
||||
cart.document(cartLineItem.getId()).set(cartLineItem).addOnCompleteListener(completion);
|
||||
|
||||
return completion;
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<Void> deleteItems() {
|
||||
final CompletionGenericLiveData<Void> completion = new CompletionGenericLiveData();
|
||||
deleteCartItems().addOnCompleteListener(completion);
|
||||
return completion;
|
||||
}
|
||||
|
||||
|
||||
private Task<Void> deleteCartItems() {
|
||||
return cart.getFirestore().runTransaction(transaction -> {
|
||||
cart.get().addOnCompleteListener(task -> {
|
||||
if (task.isSuccessful()) {
|
||||
for (QueryDocumentSnapshot document : task.getResult()) {
|
||||
cart.document(document.getId()).delete();
|
||||
}
|
||||
|
||||
} else {
|
||||
}
|
||||
});
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public CompletionGenericLiveData<DocumentReference> addToCart(int productId, float price) {
|
||||
final CompletionGenericLiveData<DocumentReference> completion = new CompletionGenericLiveData();
|
||||
|
||||
CartLineItem lineItem = new CartLineItem();
|
||||
lineItem.setProductId(productId);
|
||||
lineItem.setPrice(price);
|
||||
lineItem.setQuantity(1);
|
||||
|
||||
woocommerce.CartRepository(context).addToCart(lineItem).enqueue(callBack);
|
||||
return callBack;
|
||||
cart.add(lineItem).addOnCompleteListener(completion);
|
||||
|
||||
return completion;
|
||||
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
@ -39,11 +109,4 @@ public class CartRepository {
|
||||
return callBack;
|
||||
}
|
||||
|
||||
public void saveSession(Context context, String session, String expiry) {
|
||||
AppUtils appUtils = new AppUtils(context);
|
||||
appUtils.saveCartSession(session, expiry);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -44,20 +44,19 @@ class ProductActivity : BaseActivity() {
|
||||
product(productId)
|
||||
}
|
||||
|
||||
fab.setOnClickListener{addToCart(productId)}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private fun addToCart(productId: Int) {
|
||||
viewModel.addToCart(baseContext, productId).observe(this, android.arch.lifecycle.Observer { response ->
|
||||
private fun addToCart(productId: Int, price : Float) {
|
||||
viewModel.addToCart(productId, price).observe(this, android.arch.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
}
|
||||
|
||||
Status.SUCCESS -> {
|
||||
val order = response.data()
|
||||
|
||||
|
||||
}
|
||||
@ -87,6 +86,8 @@ class ProductActivity : BaseActivity() {
|
||||
setUpPage(product)
|
||||
//similarProducts(product)
|
||||
|
||||
fab.setOnClickListener{addToCart(productId, product.price.toFloat())}
|
||||
|
||||
EventBus.getDefault().post(ProductEvent(product))
|
||||
|
||||
}
|
||||
|
||||
@ -2,7 +2,11 @@ package me.gilo.wc.viewmodels;
|
||||
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.content.Context;
|
||||
import com.google.firebase.firestore.DocumentReference;
|
||||
import me.gilo.wc.common.CompletionGenericLiveData;
|
||||
import me.gilo.wc.common.QueryLiveData;
|
||||
import me.gilo.wc.common.WooLiveData;
|
||||
import me.gilo.wc.models.CartLineItem;
|
||||
import me.gilo.wc.repo.CartRepository;
|
||||
import me.gilo.wc.repo.OrderRepository;
|
||||
import me.gilo.wc.repo.ProductRepository;
|
||||
@ -35,8 +39,24 @@ public final class ProductViewModel extends ViewModel {
|
||||
return productRepository.products();
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(Context context, int productId) {
|
||||
return cartRepository.addToCart(context, productId);
|
||||
public CompletionGenericLiveData<DocumentReference> addToCart(int productId, float price) {
|
||||
return cartRepository.addToCart(productId, price);
|
||||
}
|
||||
|
||||
public QueryLiveData<CartLineItem> cart() {
|
||||
return cartRepository.cart();
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<Void> deleteItem(CartLineItem cartLineItem) {
|
||||
return cartRepository.deleteItem(cartLineItem);
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<Void> deleteAllCartItems() {
|
||||
return cartRepository.deleteItems();
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<Void> setQuantity(CartLineItem cartLineItem, int quantity) {
|
||||
return cartRepository.setQuantity(cartLineItem, quantity);
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
|
||||
10
app/src/main/res/drawable/circlular_menu.xml
Normal file
10
app/src/main/res/drawable/circlular_menu.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
|
||||
<solid android:color="#00bfa5"/>
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
android:color="#ffffff"/>
|
||||
|
||||
</shape>
|
||||
@ -23,6 +23,7 @@
|
||||
|
||||
<include layout="@layout/content_product"/>
|
||||
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
@ -34,4 +35,6 @@
|
||||
|
||||
/>
|
||||
|
||||
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
31
app/src/main/res/layout/menu_cart_icon.xml
Normal file
31
app/src/main/res/layout/menu_cart_icon.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/baseline_add_shopping_cart_24"
|
||||
android:tint="#ffffff"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="16dp"
|
||||
android:layout_height="16dp"
|
||||
android:text="1"
|
||||
android:id="@+id/tvCart_counter"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/circlular_menu"
|
||||
android:textColor="#ffffff"
|
||||
android:textSize="10sp"
|
||||
android:paddingTop="0dp"
|
||||
android:paddingLeft="0dp"
|
||||
android:layout_gravity="bottom|right"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:textStyle="bold"
|
||||
/>
|
||||
|
||||
</FrameLayout>
|
||||
13
app/src/main/res/menu/home.xml
Normal file
13
app/src/main/res/menu/home.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_cart"
|
||||
android:menuCategory="system"
|
||||
android:orderInCategory="12"
|
||||
android:title="My Cart"
|
||||
app:actionLayout="@layout/menu_cart_icon"
|
||||
app:showAsAction="always" />
|
||||
|
||||
</menu>
|
||||
@ -1,10 +1,13 @@
|
||||
package me.gilo.woodroid;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import me.gilo.woodroid.repo.*;
|
||||
import me.gilo.woodroid.repo.order.OrderNoteRepository;
|
||||
import me.gilo.woodroid.repo.order.RefundRepository;
|
||||
import me.gilo.woodroid.repo.product.*;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Woocommerce {
|
||||
|
||||
|
||||
@ -1,42 +0,0 @@
|
||||
package me.gilo.woodroid.models;
|
||||
|
||||
public class Line_item {
|
||||
private int product_id;
|
||||
private int quantity;
|
||||
private String variations;
|
||||
|
||||
public Line_item(int product_id, int quantity, String variations) {
|
||||
this.product_id = product_id;
|
||||
this.quantity = quantity;
|
||||
this.variations = variations;
|
||||
}
|
||||
|
||||
public Line_item(int product_id, int quantity) {
|
||||
this.product_id = product_id;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public int getProduct_id() {
|
||||
return product_id;
|
||||
}
|
||||
|
||||
public void setProduct_id(int product_id) {
|
||||
this.product_id = product_id;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getVariations() {
|
||||
return variations;
|
||||
}
|
||||
|
||||
public void setVariations(String variations) {
|
||||
this.variations = variations;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user