Search in sources :

Example 11 with StorageReference

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);
}
Also used : Group(com.polito.mad17.madmax.entities.Group) User(com.polito.mad17.madmax.entities.User) StorageReference(com.google.firebase.storage.StorageReference) Intent(android.content.Intent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) Event(com.polito.mad17.madmax.entities.Event) MotionEvent(android.view.MotionEvent) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with StorageReference

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);
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

StorageReference (com.google.firebase.storage.StorageReference)12 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)8 UploadTask (com.google.firebase.storage.UploadTask)6 Bitmap (android.graphics.Bitmap)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 NonNull (android.support.annotation.NonNull)4 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)4 File (java.io.File)4 HashMap (java.util.HashMap)3 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 StorageMetadata (com.google.firebase.storage.StorageMetadata)2 User (com.polito.mad17.madmax.entities.User)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 MotionEvent (android.view.MotionEvent)1 FirebaseMetadata (com.bedrock.padder.model.FirebaseMetadata)1