Added Post classess enz.
This commit is contained in:
@@ -26,11 +26,11 @@ dependencies {
|
||||
})
|
||||
compile 'com.android.support:appcompat-v7:25.3.1'
|
||||
compile 'com.android.support:design:25.3.1'
|
||||
compile 'com.google.firebase:firebase-database:10.0.1'
|
||||
compile 'com.google.firebase:firebase-auth:10.0.1'
|
||||
compile 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||
compile 'com.android.support:support-v4:25.3.1'
|
||||
compile 'com.google.firebase:firebase-storage:10.0.1'
|
||||
compile 'com.google.firebase:firebase-database:11.0.1'
|
||||
compile 'com.google.firebase:firebase-auth:11.0.1'
|
||||
compile 'com.google.firebase:firebase-storage:11.0.1'
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package nl.myhyvesbookplus.tagram;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
@@ -15,6 +16,8 @@ import android.view.View;
|
||||
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
|
||||
import nl.myhyvesbookplus.tagram.model.BitmapPost;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener {
|
||||
final static private String TAG = "MainScreen";
|
||||
|
||||
@@ -91,11 +94,18 @@ public class MainActivity extends AppCompatActivity implements CameraFragment.On
|
||||
public void logOutOnClick(View view) {
|
||||
FirebaseAuth.getInstance().signOut();
|
||||
goToLogin();
|
||||
this.finish();
|
||||
}
|
||||
|
||||
protected void goToLogin() {
|
||||
Intent goToLogIn = new Intent(this, LoginActivity.class);
|
||||
startActivity(goToLogIn);
|
||||
this.finish();
|
||||
}
|
||||
|
||||
public void testCreatePost(View view) {
|
||||
UploadClass uploadClass = new UploadClass();
|
||||
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
|
||||
BitmapPost bitmapPost = new BitmapPost(bitmap, "Dit is een Test!");
|
||||
uploadClass.uploadPicture(bitmapPost);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,26 @@ package nl.myhyvesbookplus.tagram;
|
||||
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.FirebaseUser;
|
||||
import com.google.firebase.auth.UserProfileChangeRequest;
|
||||
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 com.google.firebase.storage.UploadTask;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import nl.myhyvesbookplus.tagram.model.BitmapPost;
|
||||
import nl.myhyvesbookplus.tagram.model.UriPost;
|
||||
|
||||
/**
|
||||
* Created by marijnjansen on 20/06/2017.
|
||||
*/
|
||||
@@ -21,31 +30,57 @@ import java.io.ByteArrayOutputStream;
|
||||
public class UploadClass {
|
||||
|
||||
private StorageReference mStorageRef;
|
||||
private DatabaseReference mDataRef;
|
||||
|
||||
private static final String TAG = "UploadClass";
|
||||
|
||||
public UploadClass() {
|
||||
mStorageRef = FirebaseStorage.getInstance().getReference().child("images");
|
||||
mStorageRef = FirebaseStorage.getInstance().getReference();
|
||||
mDataRef = FirebaseDatabase.getInstance().getReference();
|
||||
}
|
||||
|
||||
/// Helpers ///
|
||||
|
||||
private byte[] bitmapToBytes(Bitmap bitmap) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public void uploadPicture(Bitmap bitmap) {
|
||||
UploadTask uploadTask = mStorageRef.putBytes(bitmapToBytes(bitmap));
|
||||
/// Post Uploads ///
|
||||
|
||||
public void uploadPicture(final BitmapPost post) {
|
||||
|
||||
|
||||
UploadTask uploadTask = mStorageRef.child("posts").child("UniquePostName").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));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void putPostInDatabase(UriPost post) {
|
||||
DatabaseReference ref = mDataRef.child("posts").child("UniquePostName" + ".jpg"); // TODO: Naam voor post.
|
||||
ref.setValue(post) // FIXME: Grote boos veroorzaker
|
||||
.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());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -57,4 +92,32 @@ public class UploadClass {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// Profile picture ///
|
||||
|
||||
protected void uploadProfilePicture(Bitmap picture) {
|
||||
byte[] uploadPhoto = bitmapToBytes(picture);
|
||||
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid()).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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package nl.myhyvesbookplus.tagram.model;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class BitmapPost extends Post {
|
||||
private Bitmap photo;
|
||||
|
||||
public BitmapPost(Bitmap photo, String comment, Date date, int nietSlechts, String poster) {
|
||||
super(comment, date, nietSlechts, poster);
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
public BitmapPost(Bitmap photo, String comment) {
|
||||
super(comment);
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
public Bitmap getBitmap() {
|
||||
return photo;
|
||||
}
|
||||
|
||||
public UriPost getUriPost(Uri url) {
|
||||
return new UriPost(url, getComment(), getDate(), getNietSlechts(), getPoster());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package nl.myhyvesbookplus.tagram.model;
|
||||
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by marijnjansen on 22/06/2017.
|
||||
*/
|
||||
|
||||
abstract class Post {
|
||||
|
||||
private Date date;
|
||||
private String comment;
|
||||
private int nietSlechts;
|
||||
private String poster;
|
||||
|
||||
Post(String comment, Date date, int nietSlechts, String poster) {
|
||||
this.date = date;
|
||||
this.comment = comment;
|
||||
this.nietSlechts = nietSlechts;
|
||||
this.poster = poster;
|
||||
}
|
||||
|
||||
Post(String comment) {
|
||||
this(comment, new Date(), 0, FirebaseAuth.getInstance().getCurrentUser().getUid());
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public int getNietSlechts() {
|
||||
return nietSlechts;
|
||||
}
|
||||
|
||||
public String getPoster() {
|
||||
return poster;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package nl.myhyvesbookplus.tagram.model;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class UriPost extends Post {
|
||||
private Uri photo;
|
||||
|
||||
public UriPost() {
|
||||
|
||||
}
|
||||
|
||||
public UriPost(Uri photo, String comment, Date date, int nietSlechts, String poster) {
|
||||
super(comment, date, nietSlechts, poster);
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
public UriPost(Uri photo, String comment) {
|
||||
super(comment);
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
public Uri getUri() {
|
||||
return photo;
|
||||
}
|
||||
}
|
||||
@@ -9,5 +9,10 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/hello_blank_fragment" />
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Test bitmap upload"
|
||||
android:onClick="testCreatePost"/>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
@@ -17,4 +17,7 @@
|
||||
<string name="hello_camera">Hello Camera</string>
|
||||
<string name="logo_text">MyHyvesBookPlusTagram logo</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>
|
||||
|
||||
Reference in New Issue
Block a user