order posts by publishing date

This commit is contained in:
Benedek László 2024-05-10 12:41:21 +02:00
parent affdd3e4fb
commit d59c475b6b

View File

@ -19,9 +19,11 @@ import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.Filter;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -42,7 +44,7 @@ public class PostService {
FirebaseFirestore db = FirebaseFirestore.getInstance();
try {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").get())) {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("post").orderBy("date", Query.Direction.DESCENDING).get())) {
DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get());
String username = user.get("username", String.class);
String email = user.get("email", String.class);
@ -96,8 +98,10 @@ public class PostService {
FirebaseFirestore db = FirebaseFirestore.getInstance();
try {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post")
.where(Filter.equalTo("user", db.document("/user/"+username))).get())) {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("post")
.where(Filter.equalTo("user", db.document("/user/"+username)))
.orderBy("date", Query.Direction.DESCENDING)
.get())) {
DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get());
String username = user.get("username", String.class);
String email = user.get("email", String.class);
@ -157,6 +161,7 @@ public class PostService {
post.put("content", strings[0]);
post.put("image", "");
post.put("user", user.getReference());
post.put("date", new Date().getTime());
Tasks.await(db.collection("post").add(post));
Log.d(this.getClass().getName(), "after adding post");