Search in sources :

Example 16 with DatabaseError

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

the class ChatActivity method onDownvoteClick.

@Override
public void onDownvoteClick(int p) {
    //Log.d("vote" , "down : ");
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference downvote_count = message_root.child("downvotes");
    downvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } else {
                currentData.setValue((Long) currentData.getValue() + 1);
            }
            //we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        //TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 17 with DatabaseError

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

the class FriendsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final String activityName = getActivity().getClass().getSimpleName();
    Log.d(TAG, "Sono nella activity: " + activityName);
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    databaseReference = FirebaseDatabase.getInstance().getReference();
    friends.clear();
    setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
    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);
    friendsViewAdapter = new FriendsViewAdapter(this.getContext(), this, this, friends);
    recyclerView.setAdapter(friendsViewAdapter);
    //Se sono in MainActivity visualizzo lista degli amici
    if (activityName.equals("MainActivity")) {
        query = databaseReference.child("users").child(MainActivity.getCurrentUser().getID()).child("friends");
    } else //Se sono dentro un gruppo, visualizzo lista membri del gruppo
    if (activityName.equals("GroupDetailActivity")) {
        Bundle b = this.getArguments();
        if (b != null) {
            groupID = b.getString("groupID");
            query = databaseReference.child("groups").child(groupID).child("members");
        }
    }
    Log.d(TAG, "query: " + query);
    groupMembersListener = query.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(final DataSnapshot externalDataSnapshot) {
            for (final DataSnapshot friendSnapshot : externalDataSnapshot.getChildren()) {
                //getFriend(friendSnapshot.getKey());
                Boolean deleted = true;
                if (activityName.equals("MainActivity")) {
                    Log.d(TAG, "key: " + friendSnapshot.getKey());
                    Log.d(TAG, "value: " + friendSnapshot.getValue());
                    if (!listenedFriends)
                        listenedFriends = true;
                    Log.d(TAG, friendSnapshot.child("deleted").getValue() + " ");
                    deleted = friendSnapshot.child("deleted").getValue(Boolean.class);
                    if (deleted != null) {
                    //deleted  = friendSnapshot.child("deleted").getValue().equals(true);
                    }
                } else if (activityName.equals("GroupDetailActivity")) {
                    deleted = friendSnapshot.child("deleted").getValue().equals(true);
                    //Se sono negli amici "generali" e non nei membri di un gruppo, non c'è il campo deleted, quindi sarà null
                    if (!listenedGroups.contains(groupID))
                        listenedGroups.add(groupID);
                }
                final String id = friendSnapshot.getKey();
                final Boolean finalDeleted = deleted;
                databaseReference.child("users").child(id).addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String name = dataSnapshot.child("name").getValue(String.class);
                        String surname = dataSnapshot.child("surname").getValue(String.class);
                        String profileImage = dataSnapshot.child("image").getValue(String.class);
                        if (activityName.equals("MainActivity")) {
                            User u = new User();
                            u.setID(friendSnapshot.getKey());
                            u.setName(name);
                            u.setSurname(surname);
                            u.setProfileImage(profileImage);
                            if (!finalDeleted)
                                friends.put(id, u);
                            else
                                friends.remove(id);
                            friendsViewAdapter.update(friends);
                            friendsViewAdapter.notifyDataSetChanged();
                        } else if (activityName.equals("GroupDetailActivity")) {
                            getUserAndGroupBalance(id, name, surname, profileImage, groupID);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
            friendsViewAdapter.update(friends);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.getMessage());
        }
    });
    Log.d(TAG, "dopo setAdapter");
    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 18 with DatabaseError

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

the class FriendDetailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friend_detail);
    Log.d(TAG, "onCreate di FriendDetailActivity");
    //ID dell'amico di cui sto guardando il dettaglio
    Intent intent = getIntent();
    friendID = intent.getStringExtra("friendID");
    userID = intent.getStringExtra("userID");
    toolbar = (Toolbar) findViewById(R.id.fd_toolbar);
    setSupportActionBar(toolbar);
    toolbar.setBackgroundColor(0x0000FF00);
    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();
    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);
    //Set data of upper part of Activity
    imageView = (ImageView) findViewById(R.id.img_photo);
    nameTextView = (TextView) findViewById(R.id.tv_friend_name);
    balanceTextView = (TextView) findViewById(R.id.tv_balance);
    balanceTextTextView = (TextView) findViewById(R.id.tv_balance_text);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getApplicationContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(getApplicationContext(), R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    groupsViewAdapter = new GroupsViewAdapter(getApplicationContext(), this, this, groups, FriendDetailActivity.TAG);
    recyclerView.setAdapter(groupsViewAdapter);
    //Show data of friend
    databaseReference.child("users").child(friendID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String name = dataSnapshot.child("name").getValue(String.class);
            String surname = dataSnapshot.child("surname").getValue(String.class);
            nameTextView.setText(name + " " + surname);
            if (!dataSnapshot.child("image").getValue(String.class).equals("")) {
            //                    imageView.setImageURI(dataSnapshot.child("image").getValue(String.class));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    Log.d(TAG, "Populated all the above data");
    if (findViewById(R.id.collapsed_content) != null) {
        Bundle bundle = new Bundle();
        bundle.putString("friendID", friendID);
        Log.d(TAG, friendID);
        BarDetailFragment barDetailFragment = new BarDetailFragment();
        barDetailFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.collapsed_content, barDetailFragment).commit();
        DetailFragment detailFragment = new DetailFragment();
        detailFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.main, detailFragment).commit();
    }
    //Show shared groups
    databaseReference.child("users").child(MainActivity.getCurrentUser().getID()).child("friends").child(friendID).child("sharedGroups").addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot sharedGroupSnapshot : dataSnapshot.getChildren()) {
                FirebaseUtils.getInstance().getGroup(sharedGroupSnapshot.getKey(), groups, groupsViewAdapter);
            }
        }

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

Example 19 with DatabaseError

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

the class User method joinGroup.

public void joinGroup(String groupID, String inviterUID) {
    final DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
    final String currentUID = this.getID();
    //Aggiungo gruppo alla lista gruppi dello user
    databaseReference.child("users").child(currentUID).child("groups").push();
    databaseReference.child("users").child(currentUID).child("groups").child(groupID).setValue("true");
    //Aggiungo user (con sottocampi admin e timestamp) alla lista membri del gruppo
    databaseReference.child("groups").child(groupID).child("members").push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("admin").setValue(false);
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("timestamp").setValue(timestamp);
    // aggiunto da riky
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("deleted").setValue(false);
    // aggiungo l'invitante agli amici se non lo è già
    if (!userFriends.containsKey(inviterUID)) {
        //todo aggiungere l'invitato tra gli amici dell'invitante
        addFriend(inviterUID);
    }
    //Incremento il numero di partecipanti
    databaseReference.child("groups").child(groupID).child("numberMembers").runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            Integer numberMembers = mutableData.getValue(Integer.class);
            if (numberMembers == null) {
                return Transaction.success(mutableData);
            }
            // Set value and report transaction success
            mutableData.setValue(numberMembers + 1);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "postTransaction:onComplete:" + databaseError);
        }
    });
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) MutableData(com.google.firebase.database.MutableData) SimpleDateFormat(java.text.SimpleDateFormat)

Example 20 with DatabaseError

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

the class ChatActivity method removeDownvote.

@Override
public void removeDownvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference downvote_count = message_root.child("downvotes");
    downvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(0);
            } else {
                currentData.setValue((Long) currentData.getValue() - 1);
            }
            //we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        //TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DatabaseError (com.google.firebase.database.DatabaseError)42 DataSnapshot (com.google.firebase.database.DataSnapshot)40 ValueEventListener (com.google.firebase.database.ValueEventListener)34 View (android.view.View)14 DatabaseReference (com.google.firebase.database.DatabaseReference)14 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)12 RecyclerView (android.support.v7.widget.RecyclerView)12 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 TextView (android.widget.TextView)5 Event (com.polito.mad17.madmax.entities.Event)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ActionBar (android.support.v7.app.ActionBar)3 MenuItem (android.view.MenuItem)3 ImageView (android.widget.ImageView)3