-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_java_com_example_harsh_freechat_ProfileActivity.java
280 lines (221 loc) · 12.4 KB
/
main_java_com_example_harsh_freechat_ProfileActivity.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.example.harsh.freechat;
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ProfileActivity extends AppCompatActivity {
private ImageView mProfileImage;
private TextView mProfileName,mProfileStatus;
private Button mProfileSendReqBtn, mDeclineBtn;
private DatabaseReference mUsersDatabase;
private ProgressDialog mProgressDialog;
private DatabaseReference mFriendReqDatabase;
private DatabaseReference mFriendDatabase;
private DatabaseReference mNotificationDatabase;
private DatabaseReference mRootRef;
private FirebaseUser mCurrent_user;
private String mcurrent_state;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final String user_id=getIntent().getStringExtra("user_id");
mRootRef=FirebaseDatabase.getInstance().getReference();
mUsersDatabase= FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);
mFriendReqDatabase = FirebaseDatabase.getInstance().getReference().child("Friend_req");
mFriendDatabase=FirebaseDatabase.getInstance().getReference().child("Friends");
mNotificationDatabase=FirebaseDatabase.getInstance().getReference().child("notifications");
mCurrent_user=FirebaseAuth.getInstance().getCurrentUser();
mProfileImage=findViewById(R.id.profile_picture);
mProfileName=findViewById(R.id.profile_displayName);
mProfileStatus=findViewById(R.id.profile_status);
mProfileSendReqBtn=findViewById(R.id.profile_send_req_btn);
mDeclineBtn=findViewById(R.id.profile_decline_btn);
mcurrent_state="not_friends";
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
mProgressDialog=new ProgressDialog(this);
mProgressDialog.setTitle("Loading Users Data");
mProgressDialog.setMessage("Please wait while we load user data");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
mUsersDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String display_name=dataSnapshot.child("name").getValue().toString();
String status=dataSnapshot.child("status").getValue().toString();
String image=dataSnapshot.child("image").getValue().toString();
mProfileName.setText(display_name);
mProfileStatus.setText(status);
Picasso.with(ProfileActivity.this).load(image).placeholder(R.drawable.default_avatar).into(mProfileImage);
//---------------FRIENDS LIST/REQUEST FEATURE--------------
mFriendReqDatabase.child(mCurrent_user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(user_id)){
String req_type=dataSnapshot.child(user_id).child("request_type").getValue().toString();
if(req_type.equals("received")){
mcurrent_state="req_received";
mProfileSendReqBtn.setText("Accept Friend Request");
mDeclineBtn.setVisibility(View.VISIBLE);
mDeclineBtn.setEnabled(true);
} else if(req_type.equals("sent")){
mcurrent_state="req_sent";
mProfileSendReqBtn.setText("Cancel Friend Request");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
mProgressDialog.dismiss();
}
else{
mFriendDatabase.child(mCurrent_user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(user_id)){
mcurrent_state="friends";
mProfileSendReqBtn.setText("Unfriend this person");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
mProgressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mProfileSendReqBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProfileSendReqBtn.setEnabled(false);
//--------------NOT FRIENDS STATE---------------
if(mcurrent_state.equals("not_friends"))
{
DatabaseReference newNotificationref=mRootRef.child("notifications").child(user_id).push();
String newNotificationId=newNotificationref.getKey();
HashMap<String,String> notificationData=new HashMap<>();
notificationData.put("from",mCurrent_user.getUid());
notificationData.put("type","request");
Map requestMap=new HashMap();
requestMap.put("Friend_req/" + mCurrent_user.getUid()+ "/" + user_id + "/request_type","sent");
requestMap.put("Friend_req/" + user_id + "/" + mCurrent_user.getUid() + "/request_type","received");
requestMap.put("notifications/"+ user_id + "/" + newNotificationId,notificationData);
mRootRef.updateChildren(requestMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError !=null)
{
Toast.makeText(ProfileActivity.this,"There was some error in sending request",Toast.LENGTH_SHORT).show();
}
mProfileSendReqBtn.setEnabled(true);
mcurrent_state="req_sent";
mProfileSendReqBtn.setText("Cancel Friend Request");
}
});
}
//-----------CANCEL REQUEST STATE--------------
if(mcurrent_state.equals("req_sent"))
{
mFriendReqDatabase.child(mCurrent_user.getUid()).child(user_id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mFriendReqDatabase.child(user_id).child(mCurrent_user.getUid()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mProfileSendReqBtn.setEnabled(true);
mcurrent_state="not_friends";
mProfileSendReqBtn.setText("Send Friend Request");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
});
}
});
}
//----------REQUEST RECEIVED STATE--------------
if(mcurrent_state.equals("req_received")){
final String currentDate= DateFormat.getDateTimeInstance().format(new Date());
Map friendsMap=new HashMap();
friendsMap.put("Friends/" + mCurrent_user.getUid() + "/" + user_id + "/date", currentDate);
friendsMap.put("Friends/" + user_id + "/" + mCurrent_user.getUid() + "/date",currentDate);
friendsMap.put("Friend_req/" + mCurrent_user.getUid() + "/" + user_id,null);
friendsMap.put("Friend_req/" + user_id + "/" + mCurrent_user.getUid(),null);
mRootRef.updateChildren(friendsMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError==null)
{
mProfileSendReqBtn.setEnabled(true);
mcurrent_state="friends";
mProfileSendReqBtn.setText("Unfriend this person");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
else
{
String error=databaseError.getMessage();
Toast.makeText(ProfileActivity.this,error,Toast.LENGTH_SHORT).show();
}
}
});
}
//---------UNFRIEND------------
if(mcurrent_state.equals("friends"))
{
Map unfriendMap=new HashMap();
unfriendMap.put("Friends/" + mCurrent_user.getUid() + "/" + user_id,null);
unfriendMap.put("Friends/" + user_id + "/" + mCurrent_user.getUid(),null);
mRootRef.updateChildren(unfriendMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError==null)
{
mcurrent_state="not_friends";
mProfileSendReqBtn.setText("Send Friend Request");
mDeclineBtn.setVisibility(View.INVISIBLE);
mDeclineBtn.setEnabled(false);
}
else
{
String error=databaseError.getMessage();
Toast.makeText(ProfileActivity.this,error,Toast.LENGTH_SHORT).show();
}
mProfileSendReqBtn.setEnabled(true);
}
});
}
}
});
}
}