Camera filter switching and uploading #12

Merged
11035064 merged 10 commits from felix into master 2017-06-26 10:51:45 +02:00
7 changed files with 103 additions and 61 deletions
Showing only changes of commit 6d7930ee03 - Show all commits

View File

@@ -4,12 +4,10 @@ import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
@@ -17,9 +15,10 @@ import android.view.View;
import com.google.firebase.auth.FirebaseAuth;
import nl.myhyvesbookplus.tagram.controller.UploadClass;
import nl.myhyvesbookplus.tagram.model.BitmapPost;
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener {
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener, UploadClass.ProfilePictureUpdatedListener {
final static private String TAG = "MainScreen";
FirebaseAuth mAuth;
@@ -35,25 +34,25 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
case nl.myhyvesbookplus.tagram.R.id.navigation_timeline:
Log.d(TAG, "onNavigationItemSelected: Timeline");
TimelineFragment timeline = new TimelineFragment();
transaction.replace(R.id.content, timeline);
transaction.addToBackStack(null);
transaction.commit();
transaction.replace(R.id.content, timeline)
.addToBackStack(null)
.commit();
return true;
case nl.myhyvesbookplus.tagram.R.id.navigation_camera:
Log.d(TAG, "onNavigationItemSelected: Camera");
CameraFragment camera = new CameraFragment();
transaction.replace(R.id.content, camera);
transaction.addToBackStack(null);
transaction.commit();
transaction.replace(R.id.content, camera)
.addToBackStack(null)
.commit();
return true;
case nl.myhyvesbookplus.tagram.R.id.navigation_profile:
Log.d(TAG, "onNavigationItemSelected: Profile");
ProfileFragment profile = new ProfileFragment();
transaction.replace(R.id.content, profile);
transaction.addToBackStack(null);
transaction.commit();
transaction.replace(R.id.content, profile)
.addToBackStack(null)
.commit();
return true;
}
return false;
@@ -74,14 +73,11 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
goToLogin();
}
Log.d(TAG, "onCreate: " + mAuth.getCurrentUser().getPhotoUrl() );
TimelineFragment fragment = new TimelineFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content, fragment);
transaction.commit();
}
@Override
@@ -106,9 +102,22 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
}
public void testCreatePost(View view) {
UploadClass uploadClass = new UploadClass();
UploadClass uploadClass = new UploadClass(this);
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
BitmapPost bitmapPost = new BitmapPost(bitmap, "Dit is een Test!");
uploadClass.uploadPicture(bitmapPost);
}
@Override
public void ProfilePictureUpdated(Boolean success) {
Log.d(TAG, "ProfilePictureUpdated: Ja ik luister naar je!");
FragmentManager man = getFragmentManager();
ProfileFragment frag = (ProfileFragment) man.findFragmentById(R.id.content);
FragmentTransaction transaction = man.beginTransaction();
transaction.detach(frag)
.attach(frag)
.commit();
Log.d(TAG, "ProfilePictureUpdated: Done reloading fragment");
}
}

View File

@@ -26,6 +26,8 @@ import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import nl.myhyvesbookplus.tagram.controller.UploadClass;
import static android.app.Activity.RESULT_OK;
/**
@@ -38,6 +40,7 @@ import static android.app.Activity.RESULT_OK;
*/
public class ProfileFragment extends Fragment implements View.OnClickListener {
static final int REQUEST_IMAGE_CAPTURE = 1;
final static private String TAG = "ProfileFragment";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
@@ -88,9 +91,6 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
}
user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null && user.getPhotoUrl() != null) {
httpsReference = FirebaseStorage.getInstance().getReferenceFromUrl(user.getPhotoUrl().toString());
}
}
/**
@@ -118,10 +118,16 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
findViews(view);
if (user != null && user.getPhotoUrl() != null) {
httpsReference = FirebaseStorage.getInstance().getReferenceFromUrl(user.getPhotoUrl().toString());
}
if (httpsReference != null) {
Glide.with(this).using(new FirebaseImageLoader()).load(httpsReference).into(profilePicture);
}
profilePicture.invalidate();
if (user != null && user.getDisplayName() != null) {
profileName.setText(user.getDisplayName());
}
@@ -183,29 +189,11 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
UploadClass uploadClass = new UploadClass();
UploadClass uploadClass = new UploadClass(getActivity());
uploadClass.uploadProfilePicture(imageBitmap);
// uploadClass.uploadPicture(new BitmapPost(imageBitmap, "Ik ben een heel mooi comment"));
// downloadUrl = uploadClass.getDownloadUrl();
// updateUserProfilePic(user);
}
}
// protected void updateUserProfilePic(final FirebaseUser user) {
// UserProfileChangeRequest request = new UserProfileChangeRequest.Builder()
// .setPhotoUri(downloadUrl)
// .build();
//
// user.updateProfile(request)
// .addOnCompleteListener(new OnCompleteListener<Void>() {
// @Override
// public void onComplete(@NonNull Task<Void> task) {
// if (task.isSuccessful()) {
// Log.d(TAG, "User profile updated!");
// }
// }
// });
// }
// TODO Make this function into its own class for modularity.
@@ -265,4 +253,6 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

View File

@@ -0,0 +1,28 @@
package nl.myhyvesbookplus.tagram.controller;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import nl.myhyvesbookplus.tagram.model.UriPost;
/**
* Created by marijnjansen on 23/06/2017.
*/
public class DownloadClass {
private static final String TAG = "DownloadClass";
private StorageReference mStorageRef;
private DatabaseReference mDataRef;
public DownloadClass() {
mStorageRef = FirebaseStorage.getInstance().getReference();
mDataRef = FirebaseDatabase.getInstance().getReference();
}
public UriPost[] getPosts() {
UriPost[] posts = new UriPost[10];
return posts;
}
}

View File

@@ -1,5 +1,6 @@
package nl.myhyvesbookplus.tagram;
package nl.myhyvesbookplus.tagram.controller;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
@@ -23,19 +24,23 @@ import java.io.ByteArrayOutputStream;
import nl.myhyvesbookplus.tagram.model.BitmapPost;
import nl.myhyvesbookplus.tagram.model.UriPost;
/**
* Created by marijnjansen on 20/06/2017.
*/
import static java.lang.System.currentTimeMillis;
/**
* Class that does all the photo uploading things.
*/
public class UploadClass {
private static final String TAG = "UploadClass";
private StorageReference mStorageRef;
private DatabaseReference mDataRef;
public UploadClass() {
private ProfilePictureUpdatedListener mListener;
public UploadClass(Context context) {
mStorageRef = FirebaseStorage.getInstance().getReference();
mDataRef = FirebaseDatabase.getInstance().getReference();
mListener = (ProfilePictureUpdatedListener) context;
}
/// Helpers ///
@@ -46,12 +51,20 @@ public class UploadClass {
return baos.toByteArray();
}
private String getUserUid() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
return user.getUid();
}
return "";
}
/// Post Uploads ///
public void uploadPicture(final BitmapPost post) {
final String name = getUserUid() + currentTimeMillis();
UploadTask uploadTask = mStorageRef.child("posts").child("UniquePostName" + ".jpg").putBytes(bitmapToBytes(post.getBitmap()));
UploadTask uploadTask = mStorageRef.child("posts").child(name + ".jpg").putBytes(bitmapToBytes(post.getBitmap()));
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
@@ -64,14 +77,14 @@ public class UploadClass {
// Handle successful uploads on complete
Log.d(TAG, "onSuccess: Upload Success!");
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
putPostInDatabase(post.getUriPost(downloadUrl));
putPostInDatabase(post.getUriPost(downloadUrl), name);
}
});
}
private void putPostInDatabase(UriPost post) {
DatabaseReference ref = mDataRef.child("posts").child("UniquePostName"); // TODO: Naam voor post.
ref.setValue(post) // FIXME: Grote boos veroorzaker
private void putPostInDatabase(UriPost post, String name) {
DatabaseReference ref = mDataRef.child("posts").child(name);
ref.setValue(post)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
@@ -84,19 +97,11 @@ public class UploadClass {
});
}
private String getUserUid() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
return user.getUid();
}
return "";
}
/// Profile picture ///
protected void uploadProfilePicture(Bitmap picture) {
public void uploadProfilePicture(Bitmap picture) {
byte[] uploadPhoto = bitmapToBytes(picture);
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid()).putBytes(uploadPhoto);
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid() + "_" + currentTimeMillis()).putBytes(uploadPhoto);
photoUpload.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@@ -116,7 +121,12 @@ public class UploadClass {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "onComplete: Updated profile picture");
mListener.ProfilePictureUpdated(true);
}
});
}
public interface ProfilePictureUpdatedListener {
void ProfilePictureUpdated(Boolean success);
}
}

View File

@@ -5,6 +5,9 @@ import android.net.Uri;
import java.util.Date;
/**
* BitmapPost is a Class for a Post with a Bitmap as an image.
*/
public class BitmapPost extends Post {
private Bitmap photo;

View File

@@ -5,9 +5,8 @@ import com.google.firebase.auth.FirebaseAuth;
import java.util.Date;
/**
* Created by marijnjansen on 22/06/2017.
* Post is a Class for a Post with a Bitmap as an image.
*/
abstract class Post {
private Date date;

View File

@@ -4,6 +4,9 @@ import android.net.Uri;
import java.util.Date;
/**
* UriPost is a Class for a Post with a Uri as an image.
*/
public class UriPost extends Post {
private String photo;