-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentActivity.java
More file actions
191 lines (156 loc) · 7.92 KB
/
Copy pathCommentActivity.java
File metadata and controls
191 lines (156 loc) · 7.92 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
package com.hossain.zakaria.firebasephotoapp.activities;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.appcompat.widget.Toolbar;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
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.DocumentReference;
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.QuerySnapshot;
import com.hossain.zakaria.firebasephotoapp.R;
import com.hossain.zakaria.firebasephotoapp.adapters.CommentRecyclerViewAdapter;
import com.hossain.zakaria.firebasephotoapp.models.Comment;
import com.hossain.zakaria.firebasephotoapp.models.User;
import com.hossain.zakaria.firebasephotoapp.utils.TranslateAnim;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import javax.annotation.Nullable;
public class CommentActivity extends AppCompatActivity {
@BindView(R.id.comment_toolbar)
Toolbar commentToolbar;
@BindView(R.id.comment_field_edit_text)
EditText commentFieldEditText;
@BindView(R.id.comment_send_button)
ImageView commentSendButton;
@BindView(R.id.comment_list_recycler_view)
RecyclerView commentListRecyclerView;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
private CommentRecyclerViewAdapter commentRecyclerViewAdapter;
private List<Comment> commentList;
private List<User> commentUserList;
private String currentUserId;
private String blogPostId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
ButterKnife.bind(this);
setSupportActionBar(commentToolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Comments");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
commentToolbar.setNavigationIcon(R.drawable.ic_navigation_back_icon);
setAnimatedTranslatedView();
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
currentUserId = Objects.requireNonNull(firebaseAuth.getCurrentUser()).getUid();
blogPostId = getIntent().getStringExtra("blog_post_id");
if (getIntent().getStringExtra("focus_comment_edit_text").equals("focusCommentEditText")) {
commentFieldEditText.requestFocus();
if (commentFieldEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
getCommentList();
}
private void setAnimatedTranslatedView() {
TranslateAnim.setAnimation(commentListRecyclerView, this, "bottom");
TranslateAnim.setAnimation(commentFieldEditText, this, "right");
TranslateAnim.setAnimation(commentSendButton, this, "left");
}
/*Activity name added to snapshot because to ensure that when close this activity then stop the snapshot listener*/
private void getCommentList() {
commentList = new ArrayList<>();
commentUserList = new ArrayList<>();
commentListRecyclerView.setHasFixedSize(true);
commentRecyclerViewAdapter = new CommentRecyclerViewAdapter(commentList, commentUserList, this);
commentListRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL));
commentListRecyclerView.setAdapter(commentRecyclerViewAdapter);
firebaseFirestore.collection("Posts/" + blogPostId + "/Comments").addSnapshotListener(CommentActivity.this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
for (final DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
if (documentChange.getType() == DocumentChange.Type.ADDED) {
final Comment comment = documentChange.getDocument().toObject(Comment.class);
String commentUserId = documentChange.getDocument().getString("commentUserId");
if (commentUserId != null) {
firebaseFirestore.collection("Users").document(commentUserId).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);
commentUserList.add(user);
commentList.add(comment);
commentRecyclerViewAdapter.notifyDataSetChanged();
} else {
Toast.makeText(CommentActivity.this, "Error: " + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
}
}
});
}
@OnClick(R.id.comment_send_button)
public void commentSendOnViewClicked() {
String commentText = commentFieldEditText.getText().toString();
if (!commentText.isEmpty()) {
Map<String, Object> commentMap = new HashMap<>();
commentMap.put("commentText", commentText);
commentMap.put("commentUserId", currentUserId);
commentMap.put("commentTimeStamp", getCurrentDateAndTime());
firebaseFirestore.collection("Posts/" + blogPostId + "/Comments").add(commentMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
commentFieldEditText.setText("");
} else {
Toast.makeText(CommentActivity.this, "Error: " + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_LONG).show();
}
}
});
} else {
Toast.makeText(this, "Please write a comment first", Toast.LENGTH_SHORT).show();
}
}
private String getCurrentDateAndTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mma", Locale.ENGLISH);
return simpleDateFormat.format(new Date()).toLowerCase();
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, 0);
}
@Override
public boolean onSupportNavigateUp() {
overridePendingTransition(0, 0);
return super.onSupportNavigateUp();
}
}