Skip to content

Commit

Permalink
Migration of profile to firebase.
Browse files Browse the repository at this point in the history
Now creates a new entry in firebase for user once logged in.
  • Loading branch information
ms01640 committed Apr 7, 2019
1 parent de43b04 commit 1eef549
Show file tree
Hide file tree
Showing 23 changed files with 417 additions and 322 deletions.
Binary file modified .gradle/4.10.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/4.10.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/4.10.1/taskHistory/taskHistory.bin
Binary file not shown.
Binary file modified .gradle/4.10.1/taskHistory/taskHistory.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

376 changes: 167 additions & 209 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/validate_signing_config" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,7 +35,10 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
User currentUser = userList.get(position);

ImageView image = (ImageView) listItem.findViewById(R.id.iv_profile_pic);
image.setImageResource((int) currentUser.getmProfilePicture());
//image.setImageResource((int) currentUser.getmProfilePicture());
if (!currentUser.getmProfilePicture().isEmpty()) {
Picasso.get().load(currentUser.getmProfilePicture()).placeholder(R.drawable.ic_person_blue).into(image);
}

TextView name = (TextView) listItem.findViewById(R.id.tv_username);
name.setText(currentUser.getmUsername());
Expand Down
37 changes: 28 additions & 9 deletions app/src/main/java/com/example/offlinemaps/FriendsUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand All @@ -28,9 +31,11 @@ public class FriendsUI extends AppCompatActivity {
private DrawerLayout drawerLayout;
private boolean doubleBackToExitPressedOnce = false;
private static boolean calledAlready;
private String mCurrentUser;

//Firebase fields
private DatabaseReference mDatabase;
private DatabaseReference userRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -49,6 +54,8 @@ protected void onCreate(Bundle savedInstanceState) {
// calledAlready = true;
// }

mCurrentUser = FirebaseAuth.getInstance().getUid();
userRef = FirebaseDatabase.getInstance().getReference().child("users").child(mCurrentUser);
mDatabase = FirebaseDatabase.getInstance().getReference();

ListView friends = (ListView) findViewById(R.id.lv_friends_list);
Expand All @@ -64,7 +71,6 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
friendsAdapter.add(user);
friendsAdapter.notifyDataSetChanged();
}
//Log.d("JOY", "" + R.drawable.joy);
}
}

Expand All @@ -74,16 +80,10 @@ public void onCancelled(@NonNull DatabaseError databaseError) {
}
});

//Test data for list view
// userList.add(new User(R.drawable.joy, "Mike", "Bournemouth, UK"));
// userList.add(new User(R.drawable.common_google_signin_btn_text_dark_normal_background, "Ross", "London, UK"));
// userList.add(new User(R.drawable.fui_ic_check_circle_black_128dp, "Femi", "Guildford, UK"));
// userList.add(new User(R.drawable.googleg_standard_color_18, "Kai", "London, UK"));
// userList.add(new User(R.drawable.common_google_signin_btn_icon_dark, "Vytenis", "Guildford, UK"));
// userList.add(new User(R.drawable.common_google_signin_btn_icon_light, "Rayan", "Guildford, UK"));

//Set the friends list adapter.
friends.setAdapter(friendsAdapter);

//Inflate the navigation drawer.
drawerLayout = findViewById(R.id.drawer_layout);

NavigationView navigationView = findViewById(R.id.nv_friends_list);
Expand All @@ -93,6 +93,7 @@ public void onCancelled(@NonNull DatabaseError databaseError) {
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);

// close drawer when item is tapped
drawerLayout.closeDrawers();

Expand All @@ -112,6 +113,7 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
case R.id.nav_home:
//Go to main activity.
break;
//Got to profile activity.
case R.id.nav_profile:
Intent profile = new Intent(FriendsUI.this, ProfileUI.class);
startActivity(profile);
Expand All @@ -121,6 +123,18 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
}
});


/**
Test feature
*/
// Button friend = (Button) findViewById(R.id.add_friend);
// friend.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// User user = new User("", "Dan", "Bournemouth, UK", 0, 0, 0, 0,0);
// userRef.setValue(user);
// }
// });
}

@Override
Expand All @@ -143,6 +157,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

/*
* Method used to invoke the user whether they would
* like to quit the app with a confirmation before doing so.
*/

@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/example/offlinemaps/Leaderboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
ArrayList<User> userList = new ArrayList<>();
final LeaderboardAdapterClass leaderboardAdapter = new LeaderboardAdapterClass(this, userList);

userList.add(new User(R.drawable.common_google_signin_btn_icon_dark, "Vytenis", "Guildford, UK"));
userList.add(new User(R.drawable.common_google_signin_btn_icon_light, "Rayan", "Guildford, UK"));
userList.add(new User("https://firebasestorage.googleapis.com/v0/b/fitpic-2f0fd.appspot.com/o/Profile%20Pictures%2FlryFgJBnDAhlEECsXwGuF5zMUqO2.jpg?alt=media&token=3b1172ad-27bf-41a9-b8e8-faf10c27f946", "Mike", "Bournemouth, UK",5476,296.7,0,0,0));
userList.add(new User("https://firebasestorage.googleapis.com/v0/b/fitpic-2f0fd.appspot.com/o/Profile%20Pictures%2FPGeWAHVVV7U9yrEa4MrFc4y7cQQ2.jpg?alt=media&token=b6312e7b-80e3-466c-b099-809ebcdb20d5", "Vytenis", "Guildford, UK",894,40.8,0,0,0));

leaderboard.setAdapter(leaderboardAdapter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -32,16 +35,22 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
User currentUser = userList.get(position);

ImageView image = (ImageView) listItem.findViewById(R.id.iv_profile_pic);
image.setImageResource((int) currentUser.getmProfilePicture());
//image.setImageResource((String) currentUser.getmProfilePicture());
if (!currentUser.getmProfilePicture().isEmpty()) {
Picasso.get().load(currentUser.getmProfilePicture()).placeholder(R.drawable.ic_person_blue).into(image);
}

TextView name = (TextView) listItem.findViewById(R.id.tv_username);
name.setText(currentUser.getmUsername());

TextView release = (TextView) listItem.findViewById(R.id.tv_location);
release.setText(currentUser.getmLocation());
TextView location = (TextView) listItem.findViewById(R.id.tv_location);
location.setText(currentUser.getmLocation());

ImageView runningMan = (ImageView) listItem.findViewById(R.id.iv_step_counter);
TextView steps = (TextView) listItem.findViewById(R.id.tv_step_counter);
steps.setText("" + currentUser.getmSteps());

TextView calories = (TextView) listItem.findViewById(R.id.tv_calories_burnt);
calories.setText(currentUser.getmCaloriesBurned() + "");


return listItem;
Expand Down
65 changes: 43 additions & 22 deletions app/src/main/java/com/example/offlinemaps/ProfileUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
Expand Down Expand Up @@ -51,7 +52,6 @@ public class ProfileUI extends AppCompatActivity {
private String mCurrentUser;

//Final fields
private static final int GALLERY_PICK = 2;
private static final int RC_SIGN_IN = 1;

//Firebase fields
Expand Down Expand Up @@ -111,6 +111,8 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
break;
case R.id.nav_home:
//Go to main activity.
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.signOut();
break;
case R.id.nav_friends:
Intent profile = new Intent(ProfileUI.this, FriendsUI.class);
Expand All @@ -127,11 +129,6 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
mProfilePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent gallery = new Intent();
// gallery.setAction(Intent.ACTION_GET_CONTENT);
// gallery.setType("image/*");
// startActivityForResult(gallery, GALLERY_PICK);

CropImage.activity().setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(ProfileUI.this);
Expand All @@ -155,19 +152,55 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
//user signed in.
mCurrentUser = FirebaseAuth.getInstance().getUid();
userRef = FirebaseDatabase.getInstance().getReference().child("users").child(mCurrentUser);
Log.d("TAG", userRef.toString());

userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String image = dataSnapshot.child("profilePicture").getValue().toString();
String image = dataSnapshot.child("mProfilePicture").getValue().toString();
Log.d("IMAGE", image);
Picasso.get().load(image).placeholder(R.drawable.ic_person_blue).into(mProfilePicture);
if (!image.isEmpty()) {
Picasso.get().load(image).placeholder(R.drawable.ic_person_white_24dp).into(mProfilePicture);
}

//User
TextView user = (TextView) findViewById(R.id.tv_profile_id);
user.setText(dataSnapshot.child("mUsername").getValue().toString());

//Username
TextView id = (TextView) findViewById(R.id.tv_profile_user);
id.setText(dataSnapshot.child("mUsername").getValue().toString());

//Steps
TextView steps = (TextView) findViewById(R.id.tv_profile_steps);
steps.setText(dataSnapshot.child("mSteps").getValue().toString());

//Calories
TextView calories = (TextView) findViewById(R.id.tv_profile_calories);
calories.setText(dataSnapshot.child("mCaloriesBurned").getValue().toString());

//Photos
TextView photos = (TextView) findViewById(R.id.tv_profile_photos);
photos.setText(dataSnapshot.child("mPhotos").getValue().toString());

//Followers
TextView followers = (TextView) findViewById(R.id.tv_profile_followers);
followers.setText(dataSnapshot.child("mFollowers").getValue().toString());

//Following
TextView following = (TextView) findViewById(R.id.tv_profile_following);
following.setText(dataSnapshot.child("mFollowing").getValue().toString());


} else {
User signedIn = new User("", "", "", 0, 0, 0, 0, 0);
userRef.setValue(signedIn);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
public void onCancelled(@NonNull DatabaseError dataaseError) {

}
});
Expand Down Expand Up @@ -250,17 +283,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri cropped = result.getUri();

final StorageReference filePath = userProfilePicturesRef.child(mCurrentUser + ".jpg");
// filePath.putFile(cropped).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
// @Override
// public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
// if (task.isSuccessful()) {
// Toast.makeText(ProfileUI.this, "Profile pictured uploaded successfully!", Toast.LENGTH_SHORT).show();
//
// final String downloadURL = filePath.getDownloadUrl().toString();
// userRef.child("profilePicture").setValue(downloadURL);
// }
// }
// });

filePath.putFile(cropped).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
Expand All @@ -278,10 +300,9 @@ public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.d("IMAGE", downloadUri + "");
userRef.child("profilePicture").setValue(downloadUri.toString());
userRef.child("mProfilePicture").setValue(downloadUri.toString());
} else {
// Handle failures
// ...
}
}
});
Expand Down
Loading

0 comments on commit 1eef549

Please sign in to comment.