Profile pics working!

This commit is contained in:
Marijn Jansen
2017-06-23 12:44:25 +02:00
parent 0f98bdb5ea
commit d8eea0bb70
6 changed files with 65 additions and 54 deletions

View File

@@ -1,22 +1,20 @@
package nl.myhyvesbookplus.tagram;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
@@ -25,14 +23,10 @@ import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.net.URI;
import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG;
/**
* A simple {@link Fragment} subclass.
@@ -43,27 +37,24 @@ import static android.content.ContentValues.TAG;
* create an instance of this fragment.
*/
public class ProfileFragment extends Fragment implements View.OnClickListener {
static final int REQUEST_IMAGE_CAPTURE = 1;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
static final int REQUEST_IMAGE_CAPTURE = 1;
private static Uri downloadUrl;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/// Views ///
protected Button changePwdButton;
protected ImageButton profilePicButton;
/// Views ///
protected StorageReference httpsReference;
protected TextView profileName;
protected ImageView profilePicture;
private OnFragmentInteractionListener mListener;
protected FirebaseUser user;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/// Required empty public constructor ///
@@ -193,27 +184,28 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
UploadClass uploadClass = new UploadClass();
uploadClass.uploadPicture(imageBitmap);
downloadUrl = uploadClass.getDownloadUrl();
updateUserProfilePic(user);
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!");
}
}
});
}
// 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.

View File

@@ -29,12 +29,10 @@ import nl.myhyvesbookplus.tagram.model.UriPost;
public class UploadClass {
private static final String TAG = "UploadClass";
private StorageReference mStorageRef;
private DatabaseReference mDataRef;
private static final String TAG = "UploadClass";
private static Uri downloadUrl;
public UploadClass() {
mStorageRef = FirebaseStorage.getInstance().getReference();
mDataRef = FirebaseDatabase.getInstance().getReference();
@@ -53,7 +51,7 @@ public class UploadClass {
public void uploadPicture(final BitmapPost post) {
UploadTask uploadTask = mStorageRef.child("posts").child("UniquePostName").putBytes(bitmapToBytes(post.getBitmap()));
UploadTask uploadTask = mStorageRef.child("posts").child("UniquePostName" + ".jpg").putBytes(bitmapToBytes(post.getBitmap()));
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
@@ -66,13 +64,13 @@ 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));
}
});
}
private void putPostInDatabase(UriPost post) {
DatabaseReference ref = mDataRef.child("posts").child("UniquePostName" + ".jpg"); // TODO: Naam voor post.
DatabaseReference ref = mDataRef.child("posts").child("UniquePostName"); // TODO: Naam voor post.
ref.setValue(post) // FIXME: Grote boos veroorzaker
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
@@ -82,15 +80,10 @@ public class UploadClass {
} else {
Log.d(TAG, "onComplete: " + task.getException().getLocalizedMessage());
}
downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
}
});
}
public Uri getDownloadUrl() {
return downloadUrl;
}
private String getUserUid() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {

View File

@@ -25,4 +25,8 @@ public class BitmapPost extends Post {
public UriPost getUriPost(Uri url) {
return new UriPost(url, getComment(), getDate(), getNietSlechts(), getPoster());
}
public void setPhoto(Bitmap photo) {
this.photo = photo;
}
}

View File

@@ -15,6 +15,10 @@ abstract class Post {
private int nietSlechts;
private String poster;
Post() {
// Default constructor required for calls to Post.getValue(User.class)
}
Post(String comment, Date date, int nietSlechts, String poster) {
this.date = date;
this.comment = comment;
@@ -30,15 +34,31 @@ abstract class Post {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public int getNietSlechts() {
return nietSlechts;
}
public void setNietSlechts(int nietSlechts) {
this.nietSlechts = nietSlechts;
}
public String getPoster() {
return poster;
}
public void setPoster(String poster) {
this.poster = poster;
}
}

View File

@@ -5,23 +5,28 @@ import android.net.Uri;
import java.util.Date;
public class UriPost extends Post {
private Uri photo;
private String photo;
public UriPost() {
// Default constructor required for calls to DataSnapshot.getValue(UriPost.class)
}
public UriPost(Uri photo, String comment, Date date, int nietSlechts, String poster) {
super(comment, date, nietSlechts, poster);
this.photo = photo;
this.photo = photo.toString();
}
public UriPost(Uri photo, String comment) {
public UriPost(String photo, String comment) {
super(comment);
this.photo = photo;
}
public Uri getUri() {
public String getUri() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}

View File

@@ -24,7 +24,4 @@
<string name="profile_picture_description">profile picture</string>
<string name="change_psw_button">Change Password</string>
<string name="please_wait">Please Wait</string>
<string name="login_error">Please fill in email and password</string>
<string name="register_error">Please fill in all the fields.</string>
<string name="password_match_error">Passwords do not match</string>
</resources>