Skip to content

Commit

Permalink
toggle feature for wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
sidarthkrishna committed Apr 21, 2024
1 parent d815c80 commit 0da2299
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.example.spotifywrapped2340;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -19,10 +23,12 @@
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.example.spotifywrapped2340.ObjectStructures.Artist;
import com.example.spotifywrapped2340.SpotifyDataManagers.SpotifyManager;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
Expand All @@ -37,6 +43,10 @@ public class ArtistWrapped extends AppCompatActivity implements StoriesProgressV
private TextView trackName;
private TextView artistName;

private Spinner timeRangeSpinner;

private ArrayList<Artist> topArtists = SpotifyManager.topArtistsShort;

private int currentIndex = 0;

private ImageView imageView;
Expand Down Expand Up @@ -69,9 +79,52 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
imageView = (ImageView) findViewById(R.id.mainImage);
Button backButton = (Button) findViewById(R.id.wrapped_return_button);

//Spinner in ArtistWrapped
timeRangeSpinner = findViewById(R.id.timeRangeSpinner);
String[] timeRanges = {"Short Term", "Medium Term", "Long Term"};

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, timeRanges);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeRangeSpinner.setAdapter(adapter);


SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
int selectedItemPosition = preferences.getInt("selectedItemPosition", 0);
timeRangeSpinner.setSelection(selectedItemPosition);
timeRangeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Handle selection
String selectedTimeRange = timeRanges[position];
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("selectedItemPosition", position);
editor.apply();
if (selectedTimeRange.equals("Short Term")) {
topArtists = SpotifyManager.topArtistsShort;
trackName.setText(topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
} else if (selectedTimeRange.equals("Medium Term")) {
topArtists = SpotifyManager.topArtistsMedium;
trackName.setText(topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
} else if (selectedTimeRange.equals("Long Term")) {
topArtists = SpotifyManager.topArtistsLong;
trackName.setText(topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
}
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
String selectedTimeRange = timeRanges[0];
}
});

artistName.setText("#" + (currentIndex + 1));
trackName.setText(SpotifyManager.topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(SpotifyManager.topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
trackName.setText(topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -129,8 +182,8 @@ public void onClick(View v) {
public void onNext() {
currentIndex++;
artistName.setText("#" + (currentIndex + 1));
trackName.setText(SpotifyManager.topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(SpotifyManager.topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);
trackName.setText(topArtists.get(currentIndex).getName());
Glide.with(ArtistWrapped.this).load(topArtists.get(currentIndex).getArtistImageUrl()).into(imageView);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ public class SpotifyManager {
private static Context context;

public static ArrayList<Artist> topArtists = new ArrayList<>();
public static ArrayList<Artist> topArtistsShort = new ArrayList<>();
public static ArrayList<Artist> topArtistsMedium = new ArrayList<>();
public static ArrayList<Artist> topArtistsLong = new ArrayList<>();

public static ArrayList<Track> topTracks = new ArrayList<>();

public static ArrayList<Track> topTracksShort = new ArrayList<>();
public static ArrayList<Track> topTracksMedium = new ArrayList<>();
public static ArrayList<Track> topTracksLong = new ArrayList<>();


public String artistString = "";

public String trackString = "";
Expand Down Expand Up @@ -118,19 +126,19 @@ public ArrayList<Artist> fetchTopTracks(TopItemType type, String time_range, int

final Request request;

if (time_range.equals("Short Term")) {
if (time_range.equals("short_term")) {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString())
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=short_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
} else if (time_range.equals("Medium Term")) {
} else if (time_range.equals("medium_term")) {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString())
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=medium_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
} else {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString())
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=long_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
}
Expand Down Expand Up @@ -173,7 +181,18 @@ public void onResponse(Call call, Response response) throws IOException {
JSONArray images = album.getJSONArray("images");
String albumCoverImageURL = images.getJSONObject(0).getString("url");
Log.d("Album Data", albumType + " " + totalTracks + " " + name + " " + artistId + " " + albumCoverImageURL);
topTracks.add(new Track(artistId, name, artistName, albumType, albumCoverImageURL));

if (time_range.equals("short_term")) {
topTracksShort.add(new Track(artistId, name, artistName, albumType, albumCoverImageURL));
topTracks = topTracksShort;
} else if (time_range.equals("medium_term")) {
topTracksMedium.add(new Track(artistId, name, artistName, albumType, albumCoverImageURL));
topTracks = topTracksMedium;
} else if (time_range.equals("long_term")) {
topTracksLong.add(new Track(artistId, name, artistName, albumType, albumCoverImageURL));
topTracks = topTracksLong;
}
//topTracks.add(new Track(artistId, name, artistName, albumType, albumCoverImageURL));
}
} catch (Exception e) {
Log.d("JSON", "Failed to parse data: " + e);
Expand Down Expand Up @@ -233,10 +252,24 @@ public void fetchTopArtists(String json) throws JSONException {
}
public void fetchTopArtists(TopItemType type, String time_range, int limit, CompletionListener listener) {
Log.d("HELLO", "WORLD");
final Request request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString())
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
final Request request;

if (time_range.equals("short_term")) {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=short_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
} else if (time_range.equals("medium_term")) {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=medium_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
} else {
request = new Request.Builder()
.url("https://api.spotify.com/v1/me/top/" + type.toString() + "?time_range=long_term")
.addHeader("Authorization", "Bearer " + mAccessToken)
.build();
}
// cancelCall();
mCall = mOkHttpClient.newCall(request);

Expand Down Expand Up @@ -273,7 +306,17 @@ public void onResponse(Call call, Response response) throws IOException {
Artist artist = new Artist("", "", "", 0, new ArrayList<>());
artist.setName((String)names.get(i));
artist.setArtistId((String) ids.get(i));
topArtists.add(artist);
if (time_range.equals("short_term")) {
topArtistsShort.add(artist);
topArtists = topArtistsShort;
} else if (time_range.equals("medium_term")) {
topArtistsMedium.add(artist);
topArtists = topArtistsMedium;
} else if (time_range.equals("long_term")) {
topArtistsLong.add(artist);
topArtists = topArtistsLong;
}
//topArtists.add(artist);
Log.d("Name, Id", (String) names.get(i) + ", " + (String) ids.get(i));
}
}
Expand All @@ -284,6 +327,24 @@ public void onResponse(Call call, Response response) throws IOException {
for (int i = 0; i < itemsJsonArray.length(); i++) {
JSONObject itemData = itemsJsonArray.getJSONObject(i);
JSONArray imagesArray = itemData.getJSONArray("images");

if (imagesArray.length() > 0) {
String url = imagesArray.getJSONObject(0).getString("url");
if (time_range.equals("short_term")) {
topArtistsShort.get(i).setArtistImageUrl(url);
topArtists = topArtistsShort;
} else if (time_range.equals("medium_term")) {
topArtistsMedium.get(i).setArtistImageUrl(url);
topArtists = topArtistsMedium;
} else if (time_range.equals("long_term")) {
topArtistsLong.get(i).setArtistImageUrl(url);
topArtists = topArtistsLong;
}
Log.d("URL", url);
} else {
Log.d("URL", "No image available for artist at index " + i);
}

JSONArray genres = (JSONArray) itemData.get("genres");

ArrayList<String> artistGenres = new ArrayList<>();
Expand All @@ -294,9 +355,9 @@ public void onResponse(Call call, Response response) throws IOException {
genreMap.put(genre, genreMap.getOrDefault(genre, 0) + 1);
}

String url = imagesArray.getJSONObject(0).getString("url");
topArtists.get(i).setArtistImageUrl(url);
Log.d("URL", url);
// String url = imagesArray.getJSONObject(0).getString("url");
// topArtists.get(i).setArtistImageUrl(url);
// Log.d("URL", url);

}
listener.onComplete("Task completed successfully!");
Expand Down Expand Up @@ -411,12 +472,12 @@ public void onResponse(Call call, Response response) throws IOException {
user.populateUserData(responseString, mAuth.getUid());
// FirebaseManager.getInstance(context).populateUserSpotifyData(user);

fetchTopArtists(SpotifyManager.TopItemType.artists, "", 20, new CompletionListener() {
fetchTopArtists(SpotifyManager.TopItemType.artists, "short_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", topArtists.get(0).getName());
String name = topArtists.get(0).getName();
String url = topArtists.get(0).getArtistImageUrl();
Log.d("Size!!", topArtistsShort.get(0).getName());
String name = topArtistsShort.get(0).getName();
String url = topArtistsShort.get(0).getArtistImageUrl();
Log.d("URL!!", url);

// Intent intent = new Intent(context, ProfileActivity.class);
Expand All @@ -433,10 +494,88 @@ public void onError(Exception e) {
}
});

SpotifyManager.getInstance(context).fetchTopTracks(SpotifyManager.TopItemType.tracks, "", 20, new CompletionListener() {
fetchTopArtists(SpotifyManager.TopItemType.artists, "medium_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", topArtistsMedium.get(0).getName());
String name = topArtistsMedium.get(0).getName();
String url = topArtistsMedium.get(0).getArtistImageUrl();
Log.d("URL!!", url);

// Intent intent = new Intent(context, ProfileActivity.class);
// startActivity(intent, activity);

/*Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);*/
/*startActivity(intent);*/
/*finish();*/
}

@Override
public void onError(Exception e) {

}
});

fetchTopArtists(SpotifyManager.TopItemType.artists, "long_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", topArtistsLong.get(0).getName());
String name = topArtistsLong.get(0).getName();
String url = topArtistsLong.get(0).getArtistImageUrl();
Log.d("URL!!", url);

// Intent intent = new Intent(context, ProfileActivity.class);
// startActivity(intent, activity);

/*Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);*/
/*startActivity(intent);*/
/*finish();*/
}

@Override
public void onError(Exception e) {

}
});

SpotifyManager.getInstance(context).fetchTopTracks(SpotifyManager.TopItemType.tracks, "short_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", SpotifyManager.getInstance(context).topTracksShort.size() + "");
// String name = SpotifyManager.getInstance(getApplicationContext()).topTracks.get(0).;
// String url = SpotifyManager.getInstance(getApplicationContext()).topArtists.get(0).getArtistImageUrl();
// Log.d("URL!!", url);
/*Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);*/
/*startActivity(intent);*/
/*finish();*/
}

@Override
public void onError(Exception e) {

}});

SpotifyManager.getInstance(context).fetchTopTracks(SpotifyManager.TopItemType.tracks, "medium_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", SpotifyManager.getInstance(context).topTracksMedium.size() + "");
// String name = SpotifyManager.getInstance(getApplicationContext()).topTracks.get(0).;
// String url = SpotifyManager.getInstance(getApplicationContext()).topArtists.get(0).getArtistImageUrl();
// Log.d("URL!!", url);
/*Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);*/
/*startActivity(intent);*/
/*finish();*/
}

@Override
public void onError(Exception e) {

}});

SpotifyManager.getInstance(context).fetchTopTracks(SpotifyManager.TopItemType.tracks, "long_term", 20, new CompletionListener() {
@Override
public void onComplete(String result) throws IOException {
Log.d("Size!!", SpotifyManager.getInstance(context).topTracks.size() + "");
Log.d("Size!!", SpotifyManager.getInstance(context).topTracksLong.size() + "");
// String name = SpotifyManager.getInstance(getApplicationContext()).topTracks.get(0).;
// String url = SpotifyManager.getInstance(getApplicationContext()).topArtists.get(0).getArtistImageUrl();
// Log.d("URL!!", url);
Expand Down
Loading

0 comments on commit 0da2299

Please sign in to comment.