use of com.google.firebase.storage.StorageReference in project MadMax by deviz92.
the class NewGroupActivity method onOptionsItemSelected.
//When i click SAVE
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.action_save) {
//display message if text field is empty
if (TextUtils.isEmpty(nameGroup.getText().toString())) {
nameGroup.setError(getString(R.string.required));
return false;
}
Log.d(TAG, "Second step: invite members to group");
// String deepLink = getString(R.string.invitation_deep_link) + "?groupToBeAddedID=" + groupID+ "?inviterToGroupUID=" + MainActivity.getCurrentUser().getID();
newgroup_id = databaseReference.child("groups").push().getKey();
String name = nameGroup.getText().toString();
String description = descriptionGroup.getText().toString();
//id is useless
final Group newGroup = new Group("0", name, "noImage", description, 1);
// for saving image
if (imageSetted) {
StorageReference uProfileImageFilenameRef = storageReference.child("groups").child(newgroup_id).child(newgroup_id + "_profileImage.jpg");
// Get the data from an ImageView as bytes
imageGroup.setDrawingCacheEnabled(true);
imageGroup.buildDrawingCache();
Bitmap bitmap = imageGroup.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
UploadTask uploadTask = uProfileImageFilenameRef.putBytes(imageData);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
newGroup.setImage(task.getResult().getDownloadUrl().toString());
Log.d(TAG, "group img url: " + newGroup.getImage());
databaseReference.child("groups").child(newgroup_id).setValue(newGroup);
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
databaseReference.child("groups").child(newgroup_id).child("timestamp").setValue(timeStamp);
databaseReference.child("groups").child(newgroup_id).child("numberMembers").setValue(1);
FirebaseUtils.getInstance().joinGroupFirebase(MainActivity.getCurrentUser().getID(), newgroup_id);
Log.d(TAG, "group " + newgroup_id + " created");
// add event for GROUP_ADD
User currentUser = MainActivity.getCurrentUser();
Event event = new Event(newgroup_id, Event.EventType.GROUP_ADD, currentUser.getName() + " " + currentUser.getSurname(), newGroup.getName());
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 {
databaseReference.child("groups").child(newgroup_id).setValue(newGroup);
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
databaseReference.child("groups").child(newgroup_id).child("timestamp").setValue(timeStamp);
databaseReference.child("groups").child(newgroup_id).child("numberMembers").setValue(1);
FirebaseUtils.getInstance().joinGroupFirebase(MainActivity.getCurrentUser().getID(), newgroup_id);
Log.d(TAG, "group " + newgroup_id + " created");
// add event for GROUP_ADD
User currentUser = MainActivity.getCurrentUser();
Event event = new Event(newgroup_id, Event.EventType.GROUP_ADD, currentUser.getName() + " " + currentUser.getSurname(), newGroup.getName());
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);
}
Intent intent = new Intent(getApplicationContext(), NewMemberActivity.class);
intent.putExtra("groupID", newgroup_id);
intent.putExtra("groupName", name);
startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
use of com.google.firebase.storage.StorageReference in project MadMax by deviz92.
the class FirebaseUtils method addPendingExpenseFirebase.
public void addPendingExpenseFirebase(Expense expense, ImageView expensePhoto, ImageView billPhoto) {
Log.d(TAG, "addPendingExpenseFirebase");
//Aggiungo pending expense a Firebase
final String eID = databaseReference.child("proposedExpenses").push().getKey();
databaseReference.child("proposedExpenses").child(eID).setValue(expense);
StorageReference uExpensePhotoFilenameRef = storageReference.child("proposedExpenses").child(eID).child(eID + "_expensePhoto.jpg");
// Get the data from an ImageView as bytes
expensePhoto.setDrawingCacheEnabled(true);
expensePhoto.buildDrawingCache();
Bitmap bitmap = expensePhoto.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = uExpensePhotoFilenameRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// todo Handle unsuccessful uploads
Log.e(TAG, "image upload failed");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
databaseReference.child("proposedExpenses").child(eID).child("expensePhoto").setValue(taskSnapshot.getMetadata().getDownloadUrl().toString());
}
});
Log.d(TAG, "creator expense " + expense.getCreatorID());
//e aggiungo spesa alla lista spese di ogni participant
for (Map.Entry<String, Double> participant : expense.getParticipants().entrySet()) {
Log.d(TAG, "participant " + participant.getKey());
//Setto voto nel participant a null
databaseReference.child("proposedExpenses").child(eID).child("participants").child(participant.getKey()).child("vote").setValue("null");
//Aggiungo campo deleted al participant
databaseReference.child("proposedExpenses").child(eID).child("participants").child(participant.getKey()).child("deleted").setValue(false);
//Aggiungo spesaID a elenco spese pending dello user
databaseReference.child("users").child(participant.getKey()).child("proposedExpenses").child(eID).setValue(true);
}
//Aggiungo spesa pending alla lista spese pending del gruppo
databaseReference.child("groups").child(expense.getGroupID()).child("proposedExpenses").push();
databaseReference.child("groups").child(expense.getGroupID()).child("proposedExpenses").child(eID).setValue(true);
}
Aggregations