Merge branch 'marijn-appje' into 'master'
Marijn appje See merge request !15
This commit was merged in pull request #15.
This commit is contained in:
@@ -3,7 +3,6 @@ package nl.myhyvesbookplus.tagram;
|
|||||||
import android.app.FragmentManager;
|
import android.app.FragmentManager;
|
||||||
import android.app.FragmentTransaction;
|
import android.app.FragmentTransaction;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
@@ -15,10 +14,10 @@ import android.view.View;
|
|||||||
|
|
||||||
import com.google.firebase.auth.FirebaseAuth;
|
import com.google.firebase.auth.FirebaseAuth;
|
||||||
|
|
||||||
import nl.myhyvesbookplus.tagram.controller.UploadClass;
|
import nl.myhyvesbookplus.tagram.controller.DownloadClass;
|
||||||
import nl.myhyvesbookplus.tagram.model.BitmapPost;
|
import nl.myhyvesbookplus.tagram.controller.ProfilePictureUploader;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener, UploadClass.ProfilePictureUpdatedListener {
|
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener, ProfilePictureUploader.ProfilePictureUpdatedListener {
|
||||||
final static private String TAG = "MainScreen";
|
final static private String TAG = "MainScreen";
|
||||||
|
|
||||||
FirebaseAuth mAuth;
|
FirebaseAuth mAuth;
|
||||||
@@ -102,10 +101,8 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testCreatePost(View view) {
|
public void testCreatePost(View view) {
|
||||||
UploadClass uploadClass = new UploadClass(this);
|
DownloadClass downloadClass = new DownloadClass();
|
||||||
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
|
downloadClass.getPosts();
|
||||||
BitmapPost bitmapPost = new BitmapPost(bitmap, "Dit is een Test!");
|
|
||||||
uploadClass.uploadPicture(bitmapPost);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -114,6 +111,7 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
|
|||||||
FragmentManager man = getFragmentManager();
|
FragmentManager man = getFragmentManager();
|
||||||
ProfileFragment frag = (ProfileFragment) man.findFragmentById(R.id.content);
|
ProfileFragment frag = (ProfileFragment) man.findFragmentById(R.id.content);
|
||||||
FragmentTransaction transaction = man.beginTransaction();
|
FragmentTransaction transaction = man.beginTransaction();
|
||||||
|
frag.progressDialog.dismiss();
|
||||||
transaction.detach(frag)
|
transaction.detach(frag)
|
||||||
.attach(frag)
|
.attach(frag)
|
||||||
.commit();
|
.commit();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package nl.myhyvesbookplus.tagram;
|
package nl.myhyvesbookplus.tagram;
|
||||||
|
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
@@ -26,7 +27,7 @@ import com.google.firebase.auth.FirebaseUser;
|
|||||||
import com.google.firebase.storage.FirebaseStorage;
|
import com.google.firebase.storage.FirebaseStorage;
|
||||||
import com.google.firebase.storage.StorageReference;
|
import com.google.firebase.storage.StorageReference;
|
||||||
|
|
||||||
import nl.myhyvesbookplus.tagram.controller.UploadClass;
|
import nl.myhyvesbookplus.tagram.controller.ProfilePictureUploader;
|
||||||
|
|
||||||
import static android.app.Activity.RESULT_OK;
|
import static android.app.Activity.RESULT_OK;
|
||||||
|
|
||||||
@@ -54,6 +55,7 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
|
|||||||
protected TextView profileName;
|
protected TextView profileName;
|
||||||
protected ImageView profilePicture;
|
protected ImageView profilePicture;
|
||||||
protected FirebaseUser user;
|
protected FirebaseUser user;
|
||||||
|
ProgressDialog progressDialog;
|
||||||
// TODO: Rename and change types of parameters
|
// TODO: Rename and change types of parameters
|
||||||
private String mParam1;
|
private String mParam1;
|
||||||
private String mParam2;
|
private String mParam2;
|
||||||
@@ -189,8 +191,9 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
|
|||||||
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
|
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
|
||||||
Bundle extras = data.getExtras();
|
Bundle extras = data.getExtras();
|
||||||
Bitmap imageBitmap = (Bitmap) extras.get("data");
|
Bitmap imageBitmap = (Bitmap) extras.get("data");
|
||||||
UploadClass uploadClass = new UploadClass(getActivity());
|
progressDialog = ProgressDialog.show(getActivity(), getString(R.string.please_wait), "bezig met uploaden", false, false);
|
||||||
uploadClass.uploadProfilePicture(imageBitmap);
|
ProfilePictureUploader profilePictureUploader = new ProfilePictureUploader(getActivity());
|
||||||
|
profilePictureUploader.uploadProfilePicture(imageBitmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package nl.myhyvesbookplus.tagram.controller;
|
package nl.myhyvesbookplus.tagram.controller;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.google.firebase.database.DataSnapshot;
|
||||||
|
import com.google.firebase.database.DatabaseError;
|
||||||
import com.google.firebase.database.DatabaseReference;
|
import com.google.firebase.database.DatabaseReference;
|
||||||
import com.google.firebase.database.FirebaseDatabase;
|
import com.google.firebase.database.FirebaseDatabase;
|
||||||
import com.google.firebase.storage.FirebaseStorage;
|
import com.google.firebase.database.ValueEventListener;
|
||||||
import com.google.firebase.storage.StorageReference;
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import nl.myhyvesbookplus.tagram.model.UriPost;
|
import nl.myhyvesbookplus.tagram.model.UriPost;
|
||||||
|
|
||||||
@@ -13,16 +18,37 @@ import nl.myhyvesbookplus.tagram.model.UriPost;
|
|||||||
|
|
||||||
public class DownloadClass {
|
public class DownloadClass {
|
||||||
private static final String TAG = "DownloadClass";
|
private static final String TAG = "DownloadClass";
|
||||||
private StorageReference mStorageRef;
|
// private StorageReference mStorageRef;
|
||||||
private DatabaseReference mDataRef;
|
private DatabaseReference mDataRef;
|
||||||
|
|
||||||
public DownloadClass() {
|
public DownloadClass() {
|
||||||
mStorageRef = FirebaseStorage.getInstance().getReference();
|
// mStorageRef = FirebaseStorage.getInstance().getReference();
|
||||||
mDataRef = FirebaseDatabase.getInstance().getReference();
|
mDataRef = FirebaseDatabase.getInstance().getReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UriPost[] getPosts() {
|
public UriPost[] getPosts() {
|
||||||
UriPost[] posts = new UriPost[10];
|
UriPost[] posts = new UriPost[10];
|
||||||
|
getPostsFromServer().toArray(posts);
|
||||||
|
|
||||||
return posts;
|
return posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ArrayList<UriPost> getPostsFromServer() {
|
||||||
|
Log.d(TAG, "getPostsFromServer: Begin of function");
|
||||||
|
final ArrayList<UriPost> list = new ArrayList<>();
|
||||||
|
mDataRef.child("posts").addListenerForSingleValueEvent(new ValueEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||||
|
for (DataSnapshot data : dataSnapshot.getChildren()) {
|
||||||
|
list.add(data.getValue(UriPost.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(DatabaseError databaseError) {
|
||||||
|
Log.d(TAG, "onCancelled: " + databaseError.getDetails() + databaseError.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package nl.myhyvesbookplus.tagram.controller;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.google.android.gms.tasks.OnCompleteListener;
|
||||||
|
import com.google.android.gms.tasks.OnFailureListener;
|
||||||
|
import com.google.android.gms.tasks.OnSuccessListener;
|
||||||
|
import com.google.android.gms.tasks.Task;
|
||||||
|
import com.google.firebase.database.DatabaseReference;
|
||||||
|
import com.google.firebase.storage.UploadTask;
|
||||||
|
|
||||||
|
import nl.myhyvesbookplus.tagram.model.BitmapPost;
|
||||||
|
import nl.myhyvesbookplus.tagram.model.UriPost;
|
||||||
|
|
||||||
|
import static java.lang.System.currentTimeMillis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by marijnjansen on 25/06/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class PostUploader extends UploadClass {
|
||||||
|
|
||||||
|
final private static String TAG = "PostUploader";
|
||||||
|
|
||||||
|
private PostUploadListener mListener;
|
||||||
|
|
||||||
|
public PostUploader(Context context) {
|
||||||
|
super();
|
||||||
|
if (context instanceof PostUploadListener) {
|
||||||
|
mListener = (PostUploadListener) context;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException(context.toString()
|
||||||
|
+ " must implement PostUploadListener");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uploadPicture(final BitmapPost post) {
|
||||||
|
final String name = getUserUid() + currentTimeMillis();
|
||||||
|
|
||||||
|
UploadTask uploadTask = mStorageRef.child("posts").child(name + ".jpg").putBytes(bitmapToBytes(post.getBitmap()));
|
||||||
|
uploadTask.addOnFailureListener(new OnFailureListener() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Exception e) {
|
||||||
|
Log.d(TAG, "onFailure: Upload Failed");
|
||||||
|
mListener.PostUploadComplete(false);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
||||||
|
// Handle successful uploads on complete
|
||||||
|
Log.d(TAG, "onSuccess: Upload Success!");
|
||||||
|
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
|
||||||
|
putPostInDatabase(post.getUriPost(downloadUrl), name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (task.isSuccessful()) {
|
||||||
|
Log.d(TAG, "onComplete: Added post to database");
|
||||||
|
mListener.PostUploadComplete(true);
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "onComplete: " + task.getException().getLocalizedMessage());
|
||||||
|
mListener.PostUploadComplete(false);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface PostUploadListener {
|
||||||
|
void PostUploadComplete(Boolean success);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package nl.myhyvesbookplus.tagram.controller;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.google.android.gms.tasks.OnCompleteListener;
|
||||||
|
import com.google.android.gms.tasks.OnFailureListener;
|
||||||
|
import com.google.android.gms.tasks.OnSuccessListener;
|
||||||
|
import com.google.android.gms.tasks.Task;
|
||||||
|
import com.google.firebase.auth.UserProfileChangeRequest;
|
||||||
|
import com.google.firebase.storage.FirebaseStorage;
|
||||||
|
import com.google.firebase.storage.StorageReference;
|
||||||
|
import com.google.firebase.storage.UploadTask;
|
||||||
|
|
||||||
|
import static java.lang.System.currentTimeMillis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by marijnjansen on 25/06/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ProfilePictureUploader extends UploadClass {
|
||||||
|
|
||||||
|
final static private String TAG = "PPUploader";
|
||||||
|
|
||||||
|
private Uri oldPicture;
|
||||||
|
private ProfilePictureUpdatedListener mListener;
|
||||||
|
|
||||||
|
public ProfilePictureUploader(Context context) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
if (context instanceof ProfilePictureUpdatedListener) {
|
||||||
|
mListener = (ProfilePictureUpdatedListener) context;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException(context.toString()
|
||||||
|
+ " must implement ProfilePictureUpdatedListener");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uploadProfilePicture(Bitmap picture) {
|
||||||
|
byte[] uploadPhoto = bitmapToBytes(picture);
|
||||||
|
oldPicture = mUser.getPhotoUrl();
|
||||||
|
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid() + "_" + currentTimeMillis()).putBytes(uploadPhoto);
|
||||||
|
photoUpload.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
||||||
|
Uri downloadUrl = taskSnapshot.getDownloadUrl();
|
||||||
|
updateProfilePictureInUser(downloadUrl);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.addOnFailureListener(new OnFailureListener() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Exception e) {
|
||||||
|
mListener.ProfilePictureUpdated(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateProfilePictureInUser(Uri url) {
|
||||||
|
UserProfileChangeRequest request = new UserProfileChangeRequest.Builder()
|
||||||
|
.setPhotoUri(url)
|
||||||
|
.build();
|
||||||
|
mUser.updateProfile(request)
|
||||||
|
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onComplete(@NonNull Task<Void> task) {
|
||||||
|
Log.d(TAG, "onComplete: Updated profile picture");
|
||||||
|
mListener.ProfilePictureUpdated(true);
|
||||||
|
removeOldPicture();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeOldPicture() {
|
||||||
|
StorageReference ref = FirebaseStorage.getInstance().getReferenceFromUrl(oldPicture.toString());
|
||||||
|
ref.delete()
|
||||||
|
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onComplete(@NonNull Task<Void> task) {
|
||||||
|
if (task.isSuccessful()) {
|
||||||
|
Log.d(TAG, "onComplete: Delete successfull");
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "onComplete: " + task.getException().getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ProfilePictureUpdatedListener {
|
||||||
|
void ProfilePictureUpdated(Boolean success);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,132 +1,44 @@
|
|||||||
package nl.myhyvesbookplus.tagram.controller;
|
package nl.myhyvesbookplus.tagram.controller;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.google.android.gms.tasks.OnCompleteListener;
|
|
||||||
import com.google.android.gms.tasks.OnFailureListener;
|
|
||||||
import com.google.android.gms.tasks.OnSuccessListener;
|
|
||||||
import com.google.android.gms.tasks.Task;
|
|
||||||
import com.google.firebase.auth.FirebaseAuth;
|
import com.google.firebase.auth.FirebaseAuth;
|
||||||
import com.google.firebase.auth.FirebaseUser;
|
import com.google.firebase.auth.FirebaseUser;
|
||||||
import com.google.firebase.auth.UserProfileChangeRequest;
|
|
||||||
import com.google.firebase.database.DatabaseReference;
|
import com.google.firebase.database.DatabaseReference;
|
||||||
import com.google.firebase.database.FirebaseDatabase;
|
import com.google.firebase.database.FirebaseDatabase;
|
||||||
import com.google.firebase.storage.FirebaseStorage;
|
import com.google.firebase.storage.FirebaseStorage;
|
||||||
import com.google.firebase.storage.StorageReference;
|
import com.google.firebase.storage.StorageReference;
|
||||||
import com.google.firebase.storage.UploadTask;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
import nl.myhyvesbookplus.tagram.model.BitmapPost;
|
|
||||||
import nl.myhyvesbookplus.tagram.model.UriPost;
|
|
||||||
|
|
||||||
import static java.lang.System.currentTimeMillis;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that does all the photo uploading things.
|
* Class that does all the photo uploading things.
|
||||||
*/
|
*/
|
||||||
public class UploadClass {
|
public abstract class UploadClass {
|
||||||
|
|
||||||
private static final String TAG = "UploadClass";
|
private static final String TAG = "UploadClass";
|
||||||
private StorageReference mStorageRef;
|
StorageReference mStorageRef;
|
||||||
private DatabaseReference mDataRef;
|
DatabaseReference mDataRef;
|
||||||
|
FirebaseUser mUser;
|
||||||
|
|
||||||
private ProfilePictureUpdatedListener mListener;
|
UploadClass() {
|
||||||
|
|
||||||
public UploadClass(Context context) {
|
|
||||||
mStorageRef = FirebaseStorage.getInstance().getReference();
|
mStorageRef = FirebaseStorage.getInstance().getReference();
|
||||||
mDataRef = FirebaseDatabase.getInstance().getReference();
|
mDataRef = FirebaseDatabase.getInstance().getReference();
|
||||||
mListener = (ProfilePictureUpdatedListener) context;
|
mUser = FirebaseAuth.getInstance().getCurrentUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helpers ///
|
/// Helpers ///
|
||||||
|
|
||||||
private byte[] bitmapToBytes(Bitmap bitmap) {
|
byte[] bitmapToBytes(Bitmap bitmap) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||||
return baos.toByteArray();
|
return baos.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getUserUid() {
|
String getUserUid() {
|
||||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
if (mUser != null) {
|
||||||
if (user != null) {
|
return mUser.getUid();
|
||||||
return user.getUid();
|
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Post Uploads ///
|
|
||||||
|
|
||||||
public void uploadPicture(final BitmapPost post) {
|
|
||||||
final String name = getUserUid() + currentTimeMillis();
|
|
||||||
|
|
||||||
UploadTask uploadTask = mStorageRef.child("posts").child(name + ".jpg").putBytes(bitmapToBytes(post.getBitmap()));
|
|
||||||
uploadTask.addOnFailureListener(new OnFailureListener() {
|
|
||||||
@Override
|
|
||||||
public void onFailure(@NonNull Exception e) {
|
|
||||||
Log.d(TAG, "onFailure: Upload Failed");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
|
||||||
// Handle successful uploads on complete
|
|
||||||
Log.d(TAG, "onSuccess: Upload Success!");
|
|
||||||
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
|
|
||||||
putPostInDatabase(post.getUriPost(downloadUrl), name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
if (task.isSuccessful()) {
|
|
||||||
Log.d(TAG, "onComplete: Added post to database");
|
|
||||||
} else {
|
|
||||||
Log.d(TAG, "onComplete: " + task.getException().getLocalizedMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Profile picture ///
|
|
||||||
|
|
||||||
public void uploadProfilePicture(Bitmap picture) {
|
|
||||||
byte[] uploadPhoto = bitmapToBytes(picture);
|
|
||||||
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid() + "_" + currentTimeMillis()).putBytes(uploadPhoto);
|
|
||||||
photoUpload.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
|
||||||
Uri downloadUrl = taskSnapshot.getDownloadUrl();
|
|
||||||
updateProfilePictureInUser(downloadUrl);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateProfilePictureInUser(Uri url) {
|
|
||||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
|
||||||
UserProfileChangeRequest request = new UserProfileChangeRequest.Builder()
|
|
||||||
.setPhotoUri(url)
|
|
||||||
.build();
|
|
||||||
user.updateProfile(request)
|
|
||||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,28 +8,29 @@ import java.util.Date;
|
|||||||
* UriPost is a Class for a Post with a Uri as an image.
|
* UriPost is a Class for a Post with a Uri as an image.
|
||||||
*/
|
*/
|
||||||
public class UriPost extends Post {
|
public class UriPost extends Post {
|
||||||
private String photo;
|
private String uri;
|
||||||
|
|
||||||
public UriPost() {
|
public UriPost() {
|
||||||
// Default constructor required for calls to DataSnapshot.getValue(UriPost.class)
|
// Default constructor required for calls to DataSnapshot.getValue(UriPost.class)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public UriPost(Uri photo, String comment, Date date, int nietSlechts, String poster) {
|
public UriPost(Uri uri, String comment, Date date, int nietSlechts, String poster) {
|
||||||
super(comment, date, nietSlechts, poster);
|
super(comment, date, nietSlechts, poster);
|
||||||
this.photo = photo.toString();
|
this.uri = uri.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UriPost(String photo, String comment) {
|
public UriPost(String uri, String comment) {
|
||||||
super(comment);
|
super(comment);
|
||||||
this.photo = photo;
|
this.uri = uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUri() {
|
public String getUri() {
|
||||||
return photo;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPhoto(String photo) {
|
|
||||||
this.photo = photo;
|
public void setUri(String uri) {
|
||||||
|
this.uri = uri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user