Search in sources :

Example 16 with Group

use of com.polito.mad17.madmax.entities.Group 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.getCurrentUID();
        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
        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.getCurrentUID(), 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)

Aggregations

Intent (android.content.Intent)10 Group (com.polito.mad17.madmax.entities.Group)7 DataSnapshot (com.google.firebase.database.DataSnapshot)6 DatabaseError (com.google.firebase.database.DatabaseError)6 ValueEventListener (com.google.firebase.database.ValueEventListener)6 User (com.polito.mad17.madmax.entities.User)6 View (android.view.View)5 ImageView (android.widget.ImageView)5 ViewGroup (android.view.ViewGroup)4 TextView (android.widget.TextView)4 HashMap (java.util.HashMap)4 TabLayout (android.support.design.widget.TabLayout)3 Event (com.polito.mad17.madmax.entities.Event)3 Expense (com.polito.mad17.madmax.entities.Expense)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Map (java.util.Map)3 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 ListView (android.widget.ListView)2 StorageReference (com.google.firebase.storage.StorageReference)2