Search in sources :

Example 11 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project MadMax by deviz92.

the class ChooseGroupActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_group);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(this).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(getResources().getColor(R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    groupsViewAdapter = new GroupsViewAdapter(getBaseContext(), this, groups, ChooseGroupActivity.TAG);
    recyclerView.setAdapter(groupsViewAdapter);
    //Ascolto i gruppi dello user
    databaseReference.child("users").child(MainActivity.getCurrentUser().getID()).child("groups").addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Per ogni gruppo dello user
            for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
                //Se il gruppo è true, ossia è ancora tra quelli dello user
                if (groupSnapshot.getValue(Boolean.class))
                    FirebaseUtils.getInstance().getGroup(groupSnapshot.getKey(), groups, groupsViewAdapter);
                else {
                    //tolgo il gruppo da quelli che verranno stampati, così lo vedo sparire realtime
                    groups.remove(groupSnapshot.getKey());
                    groupsViewAdapter.update(groups);
                    groupsViewAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.toException());
        }
    });
}
Also used : GroupsViewAdapter(com.polito.mad17.madmax.activities.groups.GroupsViewAdapter) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 12 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project MadMax by deviz92.

the class ExpenseDetailFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.i(TAG, "onCreateView");
    setInterface((OnItemClickInterface) getActivity());
    //Read expenseID from ExpenseDetailPagerAdapter
    Bundle b = this.getArguments();
    expenseID = b.getString("expenseID");
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(getContext(), R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    participantsViewAdapter = new ParticipantsViewAdapter(this.getContext(), this, participants);
    recyclerView.setAdapter(participantsViewAdapter);
    //Ascolto i participants alla spesa
    databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Per ogni participant
            for (DataSnapshot participantSnap : dataSnapshot.child("participants").getChildren()) {
                Double alreadyPaid = participantSnap.child("alreadyPaid").getValue(Double.class);
                Double dueImport = alreadyPaid - participantSnap.child("fraction").getValue(Double.class) * dataSnapshot.child("amount").getValue(Double.class);
                String currency = dataSnapshot.child("currency").getValue(String.class);
                User u = new User();
                u.setAlreadyPaid(alreadyPaid);
                u.setDueImport(dueImport);
                u.setExpenseCurrency(currency);
                String participantID = participantSnap.getKey();
                FirebaseUtils.getInstance().getParticipantName(participantID, participants, participantsViewAdapter, u);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.toException());
        }
    });
    return view;
}
Also used : User(com.polito.mad17.madmax.entities.User) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 13 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project MadMax by deviz92.

the class EventsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    String groupID;
    View view = inflater.inflate(R.layout.skeleton_list, container, false);
    Bundle fragmentArguments = getArguments();
    groupID = fragmentArguments.getString("groupID");
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    recyclerView.setHasFixedSize(true);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(getResources().getColor(R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    eventsViewAdapter = new EventsViewAdapter(this.getContext(), eventMap);
    recyclerView.setAdapter(eventsViewAdapter);
    DatabaseReference groupRef = databaseReference.child("groups");
    Log.d(TAG, "groupID: " + groupID);
    // retrieving events for current group
    groupRef.child(groupID).child("events").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot eventSnapshot) {
            for (DataSnapshot event : eventSnapshot.getChildren()) {
                FirebaseUtils.getInstance().getEvent(event.getKey(), eventMap, eventsViewAdapter);
                Log.d(TAG, event.getKey());
            }
            eventsViewAdapter.update(eventMap);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    return view;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 14 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project MadMax by deviz92.

the class MainActivity method onStart.

@Override
protected void onStart() {
    super.onStart();
    Log.i(TAG, "onStart");
    startingIntent = getIntent();
    currentFragment = startingIntent.getIntExtra("currentFragment", 1);
    // start declaration of a listener on all the current user data -> attached in onStart()
    currentUserListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "onDataChange currentUserref");
            if (currentUser == null) {
                //makeText(MainActivity.this, "Ricreato user", Toast.LENGTH_SHORT).show(); // todo: di debug, da rimuovere
                currentUser = new User();
            }
            currentUser.setID(currentUID);
            currentUser.setName(dataSnapshot.child("name").getValue(String.class));
            currentUser.setSurname(dataSnapshot.child("surname").getValue(String.class));
            currentUser.setProfileImage(dataSnapshot.child("image").getValue().toString());
            currentUser.setEmail(dataSnapshot.child("email").getValue(String.class));
            Log.d(TAG, "taken basic data of currentUser " + currentUser.toString());
            // get user friends's IDs
            for (DataSnapshot friend : dataSnapshot.child("friends").getChildren()) {
                currentUser.getUserFriends().put(friend.getKey(), null);
            }
            // get user groups's IDs
            for (DataSnapshot group : dataSnapshot.child("groups").getChildren()) {
                currentUser.getUserGroups().put(group.getKey(), null);
            }
            //todo mettere altri dati in myself?
            Log.d(TAG, "Taken friends and groups, now creating the adapter");
            adapter = new MainActivityPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
            viewPager.setAdapter(adapter);
            if (currentFragment != null) {
                viewPager.setCurrentItem(currentFragment);
                updateFab(currentFragment);
            } else {
                viewPager.setCurrentItem(1);
                updateFab(1);
            }
            // load nav menu header data for the current user
            loadNavHeader();
            Log.d(TAG, "logged user name: " + currentUser.getName());
            Log.d(TAG, "logged user surname: " + currentUser.getSurname());
            Uri data = startingIntent.getData();
            if (data != null) {
                inviterID = data.getQueryParameter("inviterID");
                groupToBeAddedID = data.getQueryParameter("groupToBeAddedID");
            } else {
                // retrieving data from the intent inviterID & groupToBeAddedID as the group ID where to add the current user
                if (startingIntent.hasExtra("inviterID")) {
                    // to be used to set the current user as friend of the inviter
                    Log.d(TAG, "there is an invite");
                    inviterID = startingIntent.getStringExtra("inviterID");
                    startingIntent.removeExtra("inviterID");
                }
                if (startingIntent.hasExtra("groupToBeAddedID")) {
                    groupToBeAddedID = startingIntent.getStringExtra("groupToBeAddedID");
                    startingIntent.removeExtra("groupToBeAddedID");
                }
            }
            // control if user that requires the friendship is already a friend
            if (inviterID != null) {
                if (!currentUser.getUserFriends().containsKey(inviterID)) {
                    FirebaseUtils.getInstance().addFriend(inviterID);
                    inviterID = null;
                    makeText(MainActivity.this, "Now you have a new friend!", Toast.LENGTH_LONG).show();
                } else
                    makeText(MainActivity.this, "You and inviter are already friends!", Toast.LENGTH_LONG).show();
            }
            // control if user is already part of requested group
            if (groupToBeAddedID != null) {
                if (!currentUser.getUserGroups().containsKey(groupToBeAddedID)) {
                    //                        currentUser.joinGroup(groupToBeAddedID); //todo usare questa? non aggiorna il numero dei membri
                    currentUser.getUserGroups().put(groupToBeAddedID, null);
                    FirebaseUtils.getInstance().joinGroupFirebase(currentUID, groupToBeAddedID);
                    groupToBeAddedID = null;
                    makeText(MainActivity.this, "Now you are part of the group!", Toast.LENGTH_LONG).show();
                } else
                    makeText(MainActivity.this, "You are already part of " + currentUser.getUserGroups().get(groupToBeAddedID).getName(), Toast.LENGTH_LONG).show();
            }
            if (startingIntent.hasExtra("notificationTitle")) {
                Intent notificationIntent = null;
                switch(startingIntent.getStringExtra("notificationTitle")) {
                    case "notification_invite":
                        if (startingIntent.hasExtra("groupID")) {
                            notificationIntent = new Intent(getApplicationContext(), GroupDetailActivity.class);
                            notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
                        }
                        break;
                    case "notification_expense_added":
                        if (startingIntent.hasExtra("groupID")) {
                            notificationIntent = new Intent(getApplicationContext(), ExpenseDetailActivity.class);
                            notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
                            if (startingIntent.hasExtra("expenseID")) {
                                notificationIntent.putExtra("expenseID", startingIntent.getStringExtra("expenseID"));
                            }
                        }
                        break;
                    case "notification_expense_removed":
                        if (startingIntent.hasExtra("groupID")) {
                            notificationIntent = new Intent(getApplicationContext(), GroupDetailActivity.class);
                            notificationIntent.putExtra("groupID", startingIntent.getStringExtra("groupID"));
                        }
                        break;
                    case "notification_proposalExpense_added":
                        if (startingIntent.hasExtra("expenseID")) {
                            notificationIntent = new Intent(getApplicationContext(), PendingExpenseDetailActivity.class);
                            notificationIntent.putExtra("expenseID", startingIntent.getStringExtra("expenseID"));
                        }
                        break;
                }
                if (notificationIntent != null) {
                    notificationIntent.putExtra("userID", currentUID);
                    startingIntent.removeExtra("notificationTitle");
                    startActivityForResult(notificationIntent, REQUEST_NOTIFICATION);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // TODO: come gestire?
            Log.d(TAG, "getting current user failed");
        }
    };
    // end of listener declaration on all the current user data
    authListener = new FirebaseAuth.AuthStateListener() {

        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            Log.d(TAG, "onAuthStateChanged");
            currentFirebaseUser = firebaseAuth.getCurrentUser();
            if (currentFirebaseUser != null) {
                // getting reference to the user from db
                currentUID = currentFirebaseUser.getUid();
                currentUserRef = usersRef.child(currentUID);
                //take refreshed toked and save it to use FCM
                currentUserRef.child("token").setValue(FirebaseInstanceId.getInstance().getToken());
                Log.d(TAG, "device token: " + FirebaseInstanceId.getInstance().getToken());
                // attach a listener on all the current user data
                currentUserRef.addValueEventListener(currentUserListener);
            } else {
                Log.d(TAG, "current user is null, so go to login activity");
                Intent goToLogin = new Intent(getApplicationContext(), LoginSignUpActivity.class);
                // currentUser = null;
                startActivity(goToLogin);
                auth.removeAuthStateListener(authListener);
                finish();
            }
        }
    };
    // attach the listener to the FirebaseAuth instance
    auth.addAuthStateListener(authListener);
}
Also used : User(com.polito.mad17.madmax.entities.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) DatabaseError(com.google.firebase.database.DatabaseError) LoginSignUpActivity(com.polito.mad17.madmax.activities.login.LoginSignUpActivity) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) Uri(android.net.Uri) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Example 15 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project iNGAGE by davis123123.

the class ChatActivity method updateChatConversation.

private void updateChatConversation(DataSnapshot dataSnapshot) {
    Iterator i = dataSnapshot.getChildren().iterator();
    while (i.hasNext()) {
        chat_id = dataSnapshot.getKey();
        Log.d("STATE", "result : " + chat_id);
        chat_msg = (String) ((DataSnapshot) i.next()).getValue();
        chat_side = (String) ((DataSnapshot) i.next()).getValue();
        chat_timestamp = (String) ((DataSnapshot) i.next()).getValue();
        chat_username = (String) ((DataSnapshot) i.next()).getValue();
        chat_downvote = (Long) ((DataSnapshot) i.next()).getValue();
        chat_upvote = (Long) ((DataSnapshot) i.next()).getValue();
        ChatMessageHelper msg = new ChatMessageHelper(chat_id, chat_side, chat_msg, chat_username, chat_upvote, chat_downvote, chat_timestamp);
        chatAdapter.update(msg, chat_id);
        chatAdapter.notifyDataSetChanged();
    }
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) Iterator(java.util.Iterator) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)47 DatabaseError (com.google.firebase.database.DatabaseError)40 ValueEventListener (com.google.firebase.database.ValueEventListener)34 DatabaseReference (com.google.firebase.database.DatabaseReference)14 View (android.view.View)13 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)11 RecyclerView (android.support.v7.widget.RecyclerView)11 Intent (android.content.Intent)10 Bundle (android.os.Bundle)10 User (com.polito.mad17.madmax.entities.User)10 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Event (com.polito.mad17.madmax.entities.Event)5 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)5 HashMap (java.util.HashMap)5 TextView (android.widget.TextView)4 ArrayList (java.util.ArrayList)4 ActionBar (android.support.v7.app.ActionBar)3 MenuItem (android.view.MenuItem)3