diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index 88fd5dc..7c1010f 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -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");