-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProductRepository.java
101 lines (82 loc) · 3.53 KB
/
ProductRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package br.com.siecola.firestorearchcomp.repository;
import android.arch.lifecycle.MutableLiveData;
import android.util.Log;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import java.util.ArrayList;
import java.util.List;
import br.com.siecola.firestorearchcomp.model.Product;
public class ProductRepository {
private static String TAG = "ProductRepository";
private static FirebaseAuth mFirebaseAuth;
private FirebaseFirestore mFirestore;
private static ProductRepository instance = null;
private ProductRepository(){
mFirestore = FirebaseFirestore.getInstance();
}
public static synchronized ProductRepository getInstance() {
if (instance == null) {
instance = new ProductRepository();
mFirebaseAuth = FirebaseAuth.getInstance();
}
return instance;
}
public MutableLiveData<List<Product>> getProducts() {
MutableLiveData<List<Product>> liveProducts = new MutableLiveData<List<Product>>();
mFirestore.collection(Product.COLLECTION)
.whereEqualTo(Product.FIELD_userId, mFirebaseAuth.getUid())
.orderBy(Product.FIELD_name, Query.Direction.ASCENDING)
.addSnapshotListener((snapshot, e) -> {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
List<Product> products = new ArrayList<>();
if (snapshot != null && !snapshot.isEmpty()) {
for (DocumentSnapshot documentSnapshot : snapshot.getDocuments()) {
Product product = documentSnapshot.toObject(Product.class);
product.setId(documentSnapshot.getId());
products.add(product);
}
}
liveProducts.postValue(products);
});
return liveProducts;
}
public MutableLiveData<Product> getProductById(String productId) {
MutableLiveData<Product> liveProject = new MutableLiveData<>();
final DocumentReference docRef = mFirestore.collection(Product.COLLECTION).document(productId);
docRef.addSnapshotListener((snapshot, e) -> {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
if (snapshot != null && snapshot.exists()) {
Product product = snapshot.toObject(Product.class);
product.setId(snapshot.getId());
liveProject.postValue(product);
} else {
Log.d(TAG, "Current data: null");
}
});
return liveProject;
}
public String saveProduct(Product product) {
DocumentReference document;
if (product.getId() != null) {
document = mFirestore.collection(Product.COLLECTION).document(product.getId());
} else {
product.setUserId(mFirebaseAuth.getUid());
document = mFirestore.collection(Product.COLLECTION).document();
}
document.set(product);
return document.getId();
}
public void deleteProduct(String productId) {
final DocumentReference docRef = mFirestore.collection(Product.COLLECTION).document(productId);
docRef.delete();
}
}