Skip to content

Commit

Permalink
Profile View User Details Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rehan35 committed Apr 21, 2024
1 parent c6f11c1 commit 0a5ca4c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import android.util.Log;

import com.example.spotifywrapped2340.SpotifyDataManagers.JsonReader;
import com.example.spotifywrapped2340.util.CompletionListener;

import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

Expand All @@ -18,11 +20,11 @@ public class SpotifyUser extends User {

public SpotifyUser() {
super();
this.setSpotifyId(null);
this.setRefreshToken(null);
this.setProfileImageUrl(null);
this.setFollowers(null);
this.setEmail(null);
this.setSpotifyId("");
this.setRefreshToken("");
this.setProfileImageUrl("");
this.setFollowers(0);
this.setEmail("");
}

public SpotifyUser(String spotifyId, String refreshToken, String profileImageUrl, Integer followers, String email) {
Expand All @@ -33,7 +35,6 @@ public SpotifyUser(String spotifyId, String refreshToken, String profileImageUrl
this.setEmail(email);
}


public String getSpotifyId() {
return spotifyId;
}
Expand Down Expand Up @@ -66,13 +67,14 @@ public void setFollowers(Integer followers) {
this.followers = followers;
}

public void populateUserData(String jsonString, String googleId) {
public void populateUserData(String jsonString, String googleId, CompletionListener completionListener) throws IOException {
try {
Log.d("User JSON", jsonString);
JsonReader reader = new JsonReader(jsonString);

String name = reader.getStringValue("display_name");
String id = reader.getStringValue("id");
setName(name);
this.setName(name);
this.setSpotifyId((String) id);


Expand All @@ -99,6 +101,7 @@ public void populateUserData(String jsonString, String googleId) {
this.setUserId(googleId);

Log.d("Spotify User", this.toString());
completionListener.onComplete("Loaded User Successfully");

} catch (JSONException e) {

Expand Down
100 changes: 60 additions & 40 deletions app/src/main/java/com/example/spotifywrapped2340/ProfileActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class ProfileActivity extends AppCompatActivity {
private GridLayout gridLayout;
String apiKey = "AIzaSyAFav3XHHlyWER68mm9GuQ-C7WL5z7aQWI";

TextView displayNameTextView;
TextView followersTextView;
ImageView profileImageView;

@Override
protected void onStart() {
super.onStart();
Expand All @@ -65,6 +69,21 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
getToken();
}

displayNameTextView = (TextView) findViewById(R.id.display_name_text);
followersTextView = (TextView) findViewById(R.id.followers_text);
profileImageView = (ImageView) findViewById(R.id.profile_image_view);

if (SpotifyManager.user != null) {
runOnUiThread(() -> {
displayNameTextView.setText(SpotifyManager.user.getName());
followersTextView.setText(SpotifyManager.user.getFollowers() + " Followers");

if (SpotifyManager.user.getProfileImageUrl() != null) {
Glide.with(ProfileActivity.this).load(SpotifyManager.user.getProfileImageUrl()).into(profileImageView);
}
});
}

ImageButton settingsButton = findViewById(R.id.settings_button);
settingsButton.setOnClickListener(v -> {
Intent intent = new Intent(ProfileActivity.this, SettingsActivity.class);
Expand Down Expand Up @@ -176,18 +195,19 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SpotifyManager.getInstance(getApplicationContext()).getUserProfile(this, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
TextView displayNameTextView = findViewById(R.id.display_name_text);
TextView followersTextView = findViewById(R.id.followers_text);
ImageView profileImageView = findViewById(R.id.profile_image_view);

displayNameTextView.setText(SpotifyManager.user.getName());
followersTextView.setText(SpotifyManager.user.getFollowers() + " Followers");

if (SpotifyManager.user.getProfileImageUrl() != null) {
Glide.with(ProfileActivity.this).load(SpotifyManager.user.getProfileImageUrl()).into(profileImageView);
} else {
Glide.with(ProfileActivity.this).load(R.drawable.default_profile).into(profileImageView);
}

SpotifyUser user = SpotifyManager.user;

Log.d("REACHED COMPLETION", "Completion reached");

runOnUiThread(() -> {
displayNameTextView.setText(SpotifyManager.user.getName());
followersTextView.setText(SpotifyManager.user.getFollowers() + " Followers");

if (SpotifyManager.user.getProfileImageUrl() != null) {
Glide.with(ProfileActivity.this).load(SpotifyManager.user.getProfileImageUrl()).into(profileImageView);
}
});
}

@Override
Expand All @@ -197,34 +217,34 @@ public void onError(Exception e) {
});
}
}
public void updateProfileViews(String jsonData) {
try {
JSONObject jsonObject = new JSONObject(jsonData);
String displayName = jsonObject.getString("display_name");
int followersCount = jsonObject.getJSONObject("followers").getInt("total");
String profileImageUrl = null;

JSONArray imagesArray = jsonObject.getJSONArray("images");
if (imagesArray.length() > 0) {
profileImageUrl = imagesArray.getJSONObject(0).getString("url");
}

TextView displayNameTextView = findViewById(R.id.display_name_text);
TextView followersTextView = findViewById(R.id.followers_text);
ImageView profileImageView = findViewById(R.id.profile_image_view);

displayNameTextView.setText(displayName);
followersTextView.setText(followersCount + " Followers");

if (profileImageUrl != null) {
Glide.with(this).load(profileImageUrl).into(profileImageView);
} else {
Glide.with(this).load(R.drawable.default_profile).into(profileImageView);
}
} catch (JSONException e) {
Toast.makeText(this, "Failed to parse user data", Toast.LENGTH_LONG).show();
}
}
// public void updateProfileViews(String jsonData) {
// try {
// JSONObject jsonObject = new JSONObject(jsonData);
// String displayName = jsonObject.getString("display_name");
// int followersCount = jsonObject.getJSONObject("followers").getInt("total");
// String profileImageUrl = null;
//
// JSONArray imagesArray = jsonObject.getJSONArray("images");
// if (imagesArray.length() > 0) {
// profileImageUrl = imagesArray.getJSONObject(0).getString("url");
// }
//
// TextView displayNameTextView = findViewById(R.id.display_name_text);
// TextView followersTextView = findViewById(R.id.followers_text);
// ImageView profileImageView = findViewById(R.id.profile_image_view);
//
// displayNameTextView.setText(displayName);
// followersTextView.setText(followersCount + " Followers");
//
// if (profileImageUrl != null) {
// Glide.with(this).load(profileImageUrl).into(profileImageView);
// } else {
// Glide.with(this).load(R.drawable.default_profile).into(profileImageView);
// }
// } catch (JSONException e) {
// Toast.makeText(this, "Failed to parse user data", Toast.LENGTH_LONG).show();
// }
// }



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,11 @@ public void onResponse(Call call, Response response) throws IOException {
}
String responseString = response.body().string();

if (activity instanceof ProfileActivity) {
activity.runOnUiThread(() -> {
((ProfileActivity) activity).updateProfileViews(responseString);
});
}
// if (activity instanceof ProfileActivity) {
// activity.runOnUiThread(() -> {
// ((ProfileActivity) activity).updateProfileViews(responseString);
// });
// }
try {

/*String responseString = response.body().string();*/
Expand All @@ -559,9 +559,7 @@ public void onResponse(Call call, Response response) throws IOException {

FirebaseAuth mAuth = FirebaseAuth.getInstance();
//
user.populateUserData(responseString, mAuth.getUid());

loadedUserCompletion.onComplete("Loaded User Successfully");
user.populateUserData(responseString, mAuth.getUid(), loadedUserCompletion);

String[] timeRanges = new String[]{"short_term", "medium_term", "long_term"};

Expand Down

0 comments on commit 0a5ca4c

Please sign in to comment.