Search in sources :

Example 6 with DatabaseReference

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

the class NewExpenseActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    DatabaseReference groupRef;
    int itemThatWasClickedId = item.getItemId();
    if (itemThatWasClickedId == R.id.action_save) {
        if (!validateForm())
            return true;
        //display message if text field is empty
        Toast.makeText(getBaseContext(), "Saved expense", Toast.LENGTH_SHORT).show();
        final Expense newExpense = new Expense();
        newExpense.setDescription(description.getText().toString());
        newExpense.setAmount(Double.valueOf(amount.getText().toString()));
        newExpense.setCurrency(currency.getSelectedItem().toString());
        newExpense.setGroupID(groupID);
        newExpense.setCreatorID(userID);
        newExpense.setEquallyDivided(true);
        newExpense.setDeleted(false);
        Log.d(TAG, "Before first access to firebase");
        groupRef = databaseReference.child("groups");
        groupRef.child(groupID).child("members").addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot membersSnapshot) {
                int participantsCount = 0;
                //Attenzione! Non contare i membri eliminati tra i partecipanti alla spesa
                for (DataSnapshot memberSnap : membersSnapshot.getChildren()) {
                    if (!memberSnap.child("deleted").getValue(Boolean.class)) {
                        participantsCount++;
                    }
                }
                Double amountPerMember = 1 / (double) participantsCount;
                for (DataSnapshot member : membersSnapshot.getChildren()) {
                    //Aggiungo alla spesa solo i membri non eliminati dal gruppo
                    if (!member.child("deleted").getValue(Boolean.class)) {
                        newExpense.getParticipants().put(member.getKey(), amountPerMember);
                    }
                }
                String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
                newExpense.setTimestamp(timeStamp);
                //Aggiungo una pending expense
                if (callingActivity.equals("ChooseGroupActivity")) {
                    newExpense.setGroupName(groupName);
                    if (groupImage != null)
                        newExpense.setGroupImage(groupImage);
                    FirebaseUtils.getInstance().addPendingExpenseFirebase(newExpense, expensePhoto, billPhoto);
                    //todo qui
                    Intent myIntent = new Intent(NewExpenseActivity.this, MainActivity.class);
                    myIntent.putExtra("UID", MainActivity.getCurrentUser().getID());
                    myIntent.putExtra("currentFragment", 2);
                    startActivity(myIntent);
                    // add event for PENDING_EXPENSE_ADD
                    User currentUser = MainActivity.getCurrentUser();
                    Event event = new Event(groupID, Event.EventType.PENDING_EXPENSE_ADD, currentUser.getName() + " " + currentUser.getSurname(), newExpense.getDescription(), newExpense.getAmount());
                    event.setDate(new SimpleDateFormat("yyyy.MM.dd").format(new java.util.Date()));
                    event.setTime(new SimpleDateFormat("HH:mm").format(new java.util.Date()));
                    FirebaseUtils.getInstance().addEvent(event);
                } else //Aggiungo una spesa normale
                {
                    FirebaseUtils.getInstance().addExpenseFirebase(newExpense, expensePhoto, billPhoto);
                    // add event for EXPENSE_ADD
                    User currentUser = MainActivity.getCurrentUser();
                    Event event = new Event(newExpense.getGroupID(), Event.EventType.EXPENSE_ADD, currentUser.getName() + " " + currentUser.getSurname(), newExpense.getDescription(), newExpense.getAmount());
                    event.setDate(new SimpleDateFormat("yyyy.MM.dd").format(new java.util.Date()));
                    event.setTime(new SimpleDateFormat("HH:mm").format(new java.util.Date()));
                    FirebaseUtils.getInstance().addEvent(event);
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
    }
    this.finish();
    return super.onOptionsItemSelected(item);
}
Also used : User(com.polito.mad17.madmax.entities.User) DatabaseReference(com.google.firebase.database.DatabaseReference) Intent(android.content.Intent) MainActivity(com.polito.mad17.madmax.activities.MainActivity) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) Expense(com.polito.mad17.madmax.entities.Expense) Event(com.polito.mad17.madmax.entities.Event) MotionEvent(android.view.MotionEvent) ValueEventListener(com.google.firebase.database.ValueEventListener) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with DatabaseReference

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

the class ExpenseCommentsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    String expenseID;
    View view = inflater.inflate(R.layout.skeleton_list, container, false);
    Bundle fragmentArguments = getArguments();
    expenseID = fragmentArguments.getString("expenseID");
    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);
    expenseCommentsViewAdapter = new ExpenseCommentsViewAdapter(this.getContext(), this, commentsMap, getFragmentManager());
    recyclerView.setAdapter(expenseCommentsViewAdapter);
    DatabaseReference expenseRef = databaseReference.child("expenses");
    expenseRef.child(expenseID).child("comments").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot commentSnapshot) {
            for (DataSnapshot comment : commentSnapshot.getChildren()) {
                FirebaseUtils.getInstance().getComment(comment.getKey(), commentsMap, expenseCommentsViewAdapter);
                Log.d(TAG, comment.getKey());
            }
            expenseCommentsViewAdapter.update(commentsMap);
        }

        @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 8 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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 9 with DatabaseReference

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

the class ChatActivity method removeUpvote.

@Override
public void removeUpvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_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)

Example 10 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)25 DataSnapshot (com.google.firebase.database.DataSnapshot)13 DatabaseError (com.google.firebase.database.DatabaseError)13 RecyclerView (android.support.v7.widget.RecyclerView)6 View (android.view.View)6 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 ValueEventListener (com.google.firebase.database.ValueEventListener)6 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 Intent (android.content.Intent)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 Query (com.google.firebase.database.Query)4 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)4 Bundle (android.os.Bundle)2 TextView (android.widget.TextView)2 Event (com.polito.mad17.madmax.entities.Event)2 User (com.polito.mad17.madmax.entities.User)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2