Skip to content

Commit 3000e69

Browse files
Friends Relationship Done
1 parent 7752984 commit 3000e69

File tree

6 files changed

+133
-26
lines changed

6 files changed

+133
-26
lines changed

app/build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ dependencies {
3939

4040
compile 'com.mikepenz:actionitembadge:3.2.0@aar'
4141

42+
compile('com.github.afollestad.material-dialogs:core:0.8.5.3@aar') {
43+
transitive = true
44+
}
45+
46+
compile('com.github.afollestad.material-dialogs:commons:0.8.5.3@aar') {
47+
transitive = true
48+
}
49+
4250
//SUB-DEPENDENCIES
4351
//Android-Iconics - used to provide an easy API for icons
4452
compile 'com.mikepenz:iconics-core:2.5.0@aar'

app/src/main/java/com/skuares/studio/quest/MainActivity.java

+38-23
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ public void onAuthStateChanged(AuthData authData) {
130130
headerImage = (ImageView) headerView.findViewById(R.id.headerImage);
131131
headerText = (TextView) headerView.findViewById(R.id.headerText);
132132

133-
133+
/*
134+
Friend Request list of users pointers
135+
*/
136+
usersPointers = new ArrayList<String>();
137+
Log.e("checker", "users initialized");
134138
/**
135139
* Lets inflate the very first fragment
136140
* Here , we are inflating the TabFragment as the first Fragment
@@ -299,13 +303,17 @@ protected void onResume() {
299303
}
300304

301305

306+
public void fetchNotificationsFriendRequest(){
307+
308+
}
309+
302310
public void retrieveUser() {
303311
if (mAuthData != null) {
304312

305313
// listener for user key
306314
userRef = ref.child("users").child(mAuthData.getUid());
307315
uid = mAuthData.getUid();
308-
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
316+
userRef.addValueEventListener(new ValueEventListener() {
309317
@Override
310318
public void onDataChange(DataSnapshot dataSnapshot) {
311319
// convert snapshot to user class
@@ -394,23 +402,39 @@ public void done(ParseException e) {
394402
get the users from pointers
395403
activate icon
396404
*/
405+
// in a method otherwise we would have many listeners
406+
397407
// initialize the reference
398-
usersPointers = new ArrayList<String>();
408+
399409
friendsReqestReference = new Firebase("https://quest1.firebaseio.com/users/"+uid+"/friends");
400410
Query query = friendsReqestReference.orderByValue().equalTo(0);
401411
query.addChildEventListener(new ChildEventListener() {
402412
@Override
403413
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
404414
//Log.e("logmeThis",""+dataSnapshot.getKey());
405-
// save this pointer in usersPointers
406-
usersPointers.add(dataSnapshot.getKey());
415+
Log.e("RequestMainActivity", "onChildAdded");
416+
417+
418+
if(usersPointers.contains(dataSnapshot.getKey())){
419+
// it is already here
420+
// do nothing
421+
Log.e("checker","fail data is here");
422+
}else{
423+
// save this pointer in usersPointers
424+
usersPointers.add(dataSnapshot.getKey());
425+
Log.e("checker", "success data is nt here");
426+
for(String st: usersPointers){
427+
Log.e("checkerData",""+st);
428+
}
407429

430+
// check the value
431+
// only increment when it is 0
432+
if(dataSnapshot.getValue(Integer.class) == 0){
408433

409-
// check the value
410-
// only increment when it is 0
411-
if(dataSnapshot.getValue(Integer.class) == 0){
412-
badgeCount++;
434+
badgeCount++;
435+
}
413436
}
437+
414438
// notify user by notification icon
415439

416440

@@ -440,7 +464,7 @@ public void onChildAdded(DataSnapshot dataSnapshot, String s) {
440464
@Override
441465
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
442466
// the user accepts
443-
Log.e("MainActivityRequest","onChildChanged");
467+
Log.e("RequestMainActivity","onChildChanged");
444468
// decrease the badgeCount and update the notification icon
445469
// add the notification icon programmatically
446470
//badgeCount--;
@@ -466,7 +490,7 @@ public void onChildRemoved(DataSnapshot dataSnapshot) {
466490
usersPointers.remove(dataSnapshot.getKey());
467491

468492

469-
//Log.e("MainActivityRequest","onChildRemoved");
493+
Log.e("RequestMainActivity", "onChildRemoved");
470494
// triggered when the user rejects or ignores the request
471495
//badgeCount--;
472496
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
@@ -584,6 +608,7 @@ public void onCancelled(FirebaseError firebaseError) {
584608

585609
}
586610

611+
587612
public List<String> getKeysFromValues(Map<String, Object> map, int value) {
588613
List<String> list = new ArrayList<String>();
589614

@@ -600,19 +625,8 @@ public List<String> getKeysFromValues(Map<String, Object> map, int value) {
600625
return list;
601626
}
602627

603-
// called after the List<User> is filled with data
604-
public void notificationStuff(List<User> users){
605-
// adjust the badgeCount
606-
badgeCount = users.size();
607-
// show the notification icon with appropriate number based on users.size()
608-
/*
609-
//If you want to add your ActionItem programmatically you can do this too. You do the following:
610-
new ActionItemBadgeAdder().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).add(bigStyle, 1);
611-
return true;
612-
*/
613-
//
614-
}
615628

629+
/*
616630
617631
private class CheckFriendRequests extends AsyncTask<Map<String, Object>, Void, Void> {
618632
@@ -676,6 +690,7 @@ public void onCancelled(FirebaseError firebaseError) {
676690
677691
678692
}
693+
*/
679694

680695

681696

app/src/main/java/com/skuares/studio/quest/Request/FriendRequestAdapter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void onClick(View v) {
135135
// reciver (this user)
136136

137137
ParseQuery<ParseObject> queryReceiver = ParseQuery.getQuery("Users");
138-
queryReceiver.whereEqualTo("authorId", MainActivity.uid); // find the sender ,, we should pass the sender id
138+
queryReceiver.whereEqualTo("authorId", MainActivity.uid); // find this user
139139
queryReceiver.findInBackground(new FindCallback<ParseObject>() {
140140
@Override
141141
public void done(List<ParseObject> objects, com.parse.ParseException e) {
@@ -173,8 +173,6 @@ public void done(List<ParseObject> objects, com.parse.ParseException e) {
173173
});
174174

175175

176-
177-
178176
}
179177
});
180178

app/src/main/java/com/skuares/studio/quest/User.java

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void updateFriendState(int state,Firebase userRef){
127127
*/
128128

129129
public Map<String, Object> getFriends() {
130+
130131
return friends;
131132
}
132133

app/src/main/java/com/skuares/studio/quest/ViewProfile.java

+84
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
1717
import android.view.View;
1818
import android.widget.ImageView;
1919

20+
import com.afollestad.materialdialogs.DialogAction;
21+
import com.afollestad.materialdialogs.MaterialDialog;
22+
import com.afollestad.materialdialogs.Theme;
2023
import com.firebase.client.DataSnapshot;
2124
import com.firebase.client.Firebase;
2225
import com.firebase.client.FirebaseError;
2326
import com.firebase.client.ValueEventListener;
27+
import com.parse.FindCallback;
2428
import com.parse.ParseInstallation;
29+
import com.parse.ParseObject;
2530
import com.parse.ParsePush;
2631
import com.parse.ParseQuery;
2732

33+
import java.util.ArrayList;
2834
import java.util.HashMap;
35+
import java.util.List;
2936
import java.util.Map;
3037

3138

@@ -245,6 +252,83 @@ public void onClick(View v) {
245252
// delete this user(author) from the hashmap
246253
// and delete this user (current) from author's hashmap
247254

255+
new MaterialDialog.Builder(ViewProfile.this)
256+
.title("Unfriend")
257+
.content("Do you want to unfriend " + mUser.getUsername() + " ?")
258+
.positiveText("Unfriend")
259+
.negativeText("Cancel")
260+
.theme(Theme.DARK)
261+
.onPositive(new MaterialDialog.SingleButtonCallback() {
262+
@Override
263+
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
264+
// unfriend the user
265+
// currentUser has the other user id
266+
Map<String, Object> currentUserMap = new HashMap<String, Object>();
267+
currentUserMap.put(author, null);
268+
269+
Map<String, Object> unfriendedMap = new HashMap<String, Object>();
270+
unfriendedMap.put(MainActivity.uid, null);
271+
272+
// get references to sender/receiver
273+
Firebase firebaseUnfriendedRef = MainActivity.ref.child("users").child(author).child("friends");
274+
Firebase firebaseThisUserRef = MainActivity.userRef.child("friends");
275+
276+
firebaseUnfriendedRef.updateChildren(unfriendedMap);
277+
firebaseThisUserRef.updateChildren(currentUserMap);
278+
279+
// change the icon
280+
floatingActionButton.setImageResource(R.drawable.ic_action_add_person);
281+
// change the state
282+
userState = null;
283+
284+
// parse stuff
285+
// delete from user array of friends
286+
287+
288+
// reciver (this user)
289+
290+
ParseQuery<ParseObject> queryThisUser = ParseQuery.getQuery("Users");
291+
queryThisUser.whereEqualTo("authorId", MainActivity.uid); // find this user
292+
queryThisUser.findInBackground(new FindCallback<ParseObject>() {
293+
@Override
294+
public void done(List<ParseObject> objects, com.parse.ParseException e) {
295+
if (e == null) {
296+
297+
ParseObject object = objects.get(0); // only one element. the id is unique
298+
List<String> strings = new ArrayList<String>();
299+
strings.add(author);
300+
object.removeAll("friends", strings);
301+
object.saveInBackground();
302+
} else {
303+
Log.d("score", "Error: " + e.getMessage());
304+
}
305+
}
306+
307+
});
308+
309+
ParseQuery<ParseObject> queryOtherUser = ParseQuery.getQuery("Users");
310+
queryOtherUser.whereEqualTo("authorId", author); // find the other user, the one we are viewing
311+
queryOtherUser.findInBackground(new FindCallback<ParseObject>() {
312+
@Override
313+
public void done(List<ParseObject> objects, com.parse.ParseException e) {
314+
if (e == null) {
315+
316+
ParseObject object = objects.get(0); // only one element. the id is unique
317+
List<String> strings = new ArrayList<String>();
318+
strings.add(MainActivity.uid);
319+
object.removeAll("friends", strings);
320+
object.saveInBackground();
321+
} else {
322+
Log.d("score", "Error: " + e.getMessage());
323+
}
324+
}
325+
326+
});
327+
328+
329+
}
330+
})
331+
.show();
248332

249333

250334
}else if(userState == request){

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ buildscript {
1717
allprojects {
1818
repositories {
1919
jcenter()
20+
maven { url "https://jitpack.io" }
2021
}
2122
}
2223

0 commit comments

Comments
 (0)