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);
}
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;
}
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;
}
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) {
}
});
}
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) {
}
});
}
Aggregations