-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeFragment.java
More file actions
211 lines (168 loc) · 9.72 KB
/
Copy pathHomeFragment.java
File metadata and controls
211 lines (168 loc) · 9.72 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.hossain.zakaria.firebasephotoapp.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import com.hossain.zakaria.firebasephotoapp.R;
import com.hossain.zakaria.firebasephotoapp.adapters.BlogPostRecyclerViewAdapter;
import com.hossain.zakaria.firebasephotoapp.models.BlogPost;
import com.hossain.zakaria.firebasephotoapp.models.User;
import com.hossain.zakaria.firebasephotoapp.utils.CircularProgressBar;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
public class HomeFragment extends Fragment {
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private RecyclerView blogPostRecyclerView;
private BlogPostRecyclerViewAdapter blogPostRecyclerViewAdapter;
private DocumentSnapshot lastVisibleSnapshot;
private List<User> userList;
private List<BlogPost> blogPostList;
private boolean isFirstPagePostAlreadyLoaded;
//private FloatingActionButton floatingActionButton;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
userList = new ArrayList<>();
blogPostList = new ArrayList<>();
blogPostRecyclerView = view.findViewById(R.id.blog_post_recycler_view);
blogPostRecyclerViewAdapter = new BlogPostRecyclerViewAdapter(blogPostList, userList, container.getContext());
blogPostRecyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
blogPostRecyclerView.setAdapter(blogPostRecyclerViewAdapter);
if (firebaseAuth.getCurrentUser() != null) {
blogPostRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
boolean reachedBottom = !recyclerView.canScrollVertically(1); //if can not possible to scroll vertically then it will return false
if (reachedBottom) {
loadMorePost();
}
}
});
loadFirstFewPost();
}
return view;
}
private void loadFirstFewPost() {
CircularProgressBar progressBar = new CircularProgressBar(getContext());
final androidx.appcompat.app.AlertDialog alertDialog = progressBar.setCircularProgressBar();
if (firebaseAuth.getCurrentUser() != null) {
Query firstDataQuery = firebaseFirestore.collection("Posts")
.orderBy("postDateAndTime", Query.Direction.DESCENDING)
.limit(5);
alertDialog.dismiss();
firstDataQuery.addSnapshotListener(Objects.requireNonNull(getActivity()), new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
if (!isFirstPagePostAlreadyLoaded) {
lastVisibleSnapshot = queryDocumentSnapshots.getDocuments()
.get(queryDocumentSnapshots.size() - 1);
userList.clear();
blogPostList.clear();
}
for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
if (documentChange.getType() == DocumentChange.Type.ADDED) {
String blogPostId = documentChange.getDocument().getId();
final BlogPost blogPost = documentChange.getDocument().toObject(BlogPost.class).withId(blogPostId);
String blogUserId = documentChange.getDocument().getString("userId");
if (blogUserId != null) {
firebaseFirestore.collection("Users").document(blogUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
User user = Objects.requireNonNull(task.getResult()).toObject(User.class);
if (!isFirstPagePostAlreadyLoaded) {
userList.add(user);
blogPostList.add(blogPost);
} else {
userList.add(0, user);
blogPostList.add(0, blogPost);
}
blogPostRecyclerViewAdapter.notifyDataSetChanged();
} else {
Toast.makeText(getContext(), "Error: " + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
isFirstPagePostAlreadyLoaded = true;
}
}
});
}
}
private void loadMorePost() {
CircularProgressBar progressBar = new CircularProgressBar(getContext());
final androidx.appcompat.app.AlertDialog alertDialog = progressBar.setCircularProgressBar();
if (firebaseAuth.getCurrentUser() != null) {
Query nextDataQuery = firebaseFirestore.collection("Posts")
.orderBy("postDateAndTime", Query.Direction.DESCENDING)
.startAfter(lastVisibleSnapshot)
.limit(5);
alertDialog.dismiss();
nextDataQuery.addSnapshotListener(Objects.requireNonNull(getActivity()), new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
lastVisibleSnapshot = queryDocumentSnapshots.getDocuments()
.get(queryDocumentSnapshots.size() - 1);
for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
if (documentChange.getType() == DocumentChange.Type.ADDED) {
String blogPostId = documentChange.getDocument().getId();
final BlogPost blogPost = documentChange.getDocument().toObject(BlogPost.class).withId(blogPostId);
String blogUserId = documentChange.getDocument().getString("userId");
if (blogUserId != null) {
firebaseFirestore.collection("Users").document(blogUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
User user = Objects.requireNonNull(task.getResult()).toObject(User.class);
userList.add(user);
blogPostList.add(blogPost);
blogPostRecyclerViewAdapter.notifyDataSetChanged();
} else {
Toast.makeText(getContext(), "Error: " + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
}
}
});
}
}
/*@Override
public void onDetach() {
// fragment remove from activity
super.onDetach();
isFirstPagePostAlreadyLoaded = false;
}*/
}