Skip to content
Permalink
Browse files

Saving Data to the Firebase Database

  • Loading branch information
dragosholban committed May 13, 2018
1 parent 599d4b7 commit 660a4df32538b2d085bc8f2b121f22d9997a0f75
Binary file not shown.
@@ -28,6 +28,7 @@ dependencies {
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
}

apply plugin: 'com.google.gms.google-services'
@@ -16,6 +16,8 @@
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;
@@ -26,6 +28,7 @@
public class FeedActivity extends AppCompatActivity {

FirebaseUser fbUser;
DatabaseReference database;

static final int RC_PERMISSION_READ_EXTERNAL_STORAGE = 1;
static final int RC_IMAGE_GALLERY = 2;
@@ -39,6 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {
if (fbUser == null) {
finish();
}

database = FirebaseDatabase.getInstance().getReference();
}

public void uploadImage(View view) {
@@ -86,6 +91,11 @@ public void onFailure(@NonNull Exception exception) {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Toast.makeText(FeedActivity.this, "Upload finished!", Toast.LENGTH_SHORT).show();

// save image to database
String key = database.child("images").push().getKey();
Image image = new Image(key, fbUser.getUid(), downloadUrl.toString());
database.child("images").child(key).setValue(image);
}
});
}
@@ -0,0 +1,42 @@
package com.dragosholban.myinstagramapp;

import com.google.firebase.database.Exclude;
import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class Image {
public String key;
public String userId;
public String downloadUrl;

// these properties will not be saved to the database
@Exclude
public User user;

@Exclude
public int likes = 0;

@Exclude
public boolean hasLiked = false;

@Exclude
public String userLike;

public Image() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public Image(String key, String userId, String downloadUrl) {
this.key = key;
this.userId = userId;
this.downloadUrl = downloadUrl;
}

public void addLike() {
this.likes++;
}

public void removeLike() {
this.likes--;
}
}
@@ -0,0 +1,18 @@
package com.dragosholban.myinstagramapp;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class Like {
public String imageId;
public String userId;

public Like() {
// Default constructor required for calls to DataSnapshot.getValue(Like.class)
}

public Like(String imageId, String userId) {
this.imageId = imageId;
this.userId = userId;
}
}
@@ -10,19 +10,35 @@
import com.firebase.ui.auth.IdpResponse;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;

public class MainActivity extends AppCompatActivity {

DatabaseReference database;

private static final int RC_SIGN_IN = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

database = FirebaseDatabase.getInstance().getReference();

FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();
if(fbUser != null) {
// User already signed in

// get the FCM token
String token = FirebaseInstanceId.getInstance().getToken();

// save the user info in the database to users/UID/
// we'll use the UID as part of the path
User user = new User(fbUser.getUid(), fbUser.getDisplayName(), token);
database.child("users").child(user.uid).setValue(user);

// go to feed activity
Intent intent = new Intent(this, FeedActivity.class);
startActivity(intent);
@@ -45,6 +61,18 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {
// Successfully signed in

// get the Firebase user
FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();

// get the FCM token
String token = FirebaseInstanceId.getInstance().getToken();

// save the user info in the database to users/UID/
// we'll use the UID as part of the path
User user = new User(fbUser.getUid(), fbUser.getDisplayName(), token);
database.child("users").child(user.uid).setValue(user);

// go to feed activity
Intent intent = new Intent(this, FeedActivity.class);
startActivity(intent);
@@ -0,0 +1,20 @@
package com.dragosholban.myinstagramapp;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class User {
public String uid;
public String displayName;
public String token;

public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public User(String uid, String displayName, String token) {
this.uid = uid;
this.displayName = displayName;
this.token = token;
}
}

0 comments on commit 660a4df

Please sign in to comment.
You can’t perform that action at this time.