Skip to content

Commit 6adaace

Browse files
committed
Trim down state to 80 songs (fixes #93)
1 parent fbdab16 commit 6adaace

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ void loadStation(@NotNull MercuryRequests.StationsWrapper station) {
787787
state.setPlayingTrackIndex(0);
788788
state.clearTrack();
789789
state.addAllTrack(station.tracks());
790+
SpotifyIrc.trimTracks(state);
790791
}
791792

792793
int lastQueuedSongIndex() {
@@ -844,6 +845,7 @@ private void loadPage(@NotNull Remote3Page page, @Nullable TrackSelector selecto
844845
}
845846

846847
state.setPlayingTrackIndex(selector == null ? 0 : selector.playingIndex());
848+
SpotifyIrc.trimTracks(state);
847849

848850
if (state.getShuffle()) shuffleTracks(selector == null || !selector.findMatch());
849851
}
@@ -919,6 +921,7 @@ void updateContext(@NotNull Remote3Frame.Context context) throws SpotifyContext.
919921
}
920922

921923
state.setPlayingTrackIndex(selector.playingIndex());
924+
SpotifyIrc.trimTracks(state);
922925

923926
if (page.nextPageUrl != null && playablesProvider instanceof StationProvider)
924927
((StationProvider) playablesProvider).knowsNextPageUrl(page.nextPageUrl);
@@ -947,6 +950,7 @@ void setQueue(@NotNull Remote3Frame frame) {
947950
}
948951

949952
state.setPlayingTrackIndex(index);
953+
SpotifyIrc.trimTracks(state);
950954
}
951955

952956
void addToQueue(@NotNull Remote3Frame frame) {

core/src/main/java/xyz/gianlu/librespot/player/tracks/PlaylistProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import xyz.gianlu.librespot.mercury.model.PlayableId;
1010
import xyz.gianlu.librespot.mercury.model.TrackId;
1111
import xyz.gianlu.librespot.player.remote.Remote3Track;
12+
import xyz.gianlu.librespot.spirc.SpotifyIrc;
1213

1314
import java.io.IOException;
1415
import java.util.ArrayList;
@@ -65,6 +66,8 @@ public void shuffleTracks(@NotNull Random random, boolean fully) {
6566

6667
state.clearTrack();
6768
state.addAllTrack(tracks);
69+
SpotifyIrc.trimTracks(state);
70+
6871
LOGGER.trace("Shuffled, seed: " + shuffleSeed);
6972
}
7073

@@ -108,9 +111,9 @@ public void unshuffleTracks() {
108111
Spirc.TrackRef current = state.getTrack(state.getPlayingTrackIndex());
109112
String currentTrackUri = TrackId.fromTrackRef(current).toSpotifyUri();
110113

111-
List<Spirc.TrackRef> rebuildState = new ArrayList<>(80);
114+
List<Spirc.TrackRef> rebuildState = new ArrayList<>(SpotifyIrc.MAX_TRACKS);
112115
boolean add = false;
113-
int count = 80;
116+
int count = SpotifyIrc.MAX_TRACKS;
114117
for (Remote3Track track : tracks) {
115118
if (add || track.uri.equals(currentTrackUri)) {
116119
rebuildState.add(track.toTrackRef());
@@ -127,6 +130,7 @@ public void unshuffleTracks() {
127130
state.clearTrack();
128131
state.addAllTrack(rebuildState);
129132
state.setPlayingTrackIndex(0);
133+
SpotifyIrc.trimTracks(state);
130134

131135
LOGGER.trace("Unshuffled using context-resolve.");
132136
} else {

core/src/main/java/xyz/gianlu/librespot/player/tracks/StationProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import xyz.gianlu.librespot.mercury.MercuryRequests;
1414
import xyz.gianlu.librespot.mercury.RawMercuryRequest;
1515
import xyz.gianlu.librespot.mercury.model.TrackId;
16+
import xyz.gianlu.librespot.spirc.SpotifyIrc;
1617

1718
import java.io.IOException;
1819
import java.io.InputStreamReader;
@@ -72,6 +73,8 @@ private void getNextPage() throws IOException {
7273
.setGid(ByteString.copyFrom(TrackId.fromUri(uri).getGid()))
7374
.build());
7475
}
76+
77+
SpotifyIrc.trimTracks(state);
7578
}
7679

7780
private void resolveContext() throws IOException, MercuryClient.MercuryException {

core/src/main/java/xyz/gianlu/librespot/spirc/SpotifyIrc.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public class SpotifyIrc implements Closeable {
3131
private final Session session;
3232
private final Spirc.DeviceState.Builder deviceState;
3333
private SpircListener internalListener;
34+
public static final int MAX_TRACKS = 80;
35+
36+
public static void trimTracks(@NotNull Spirc.State.Builder state) {
37+
if (state.getTrackCount() <= MAX_TRACKS) return;
38+
39+
for (int i = state.getTrackCount() - 1; i >= MAX_TRACKS; i--)
40+
state.removeTrack(i);
41+
}
3442

3543
public SpotifyIrc(@NotNull Session session) {
3644
this.session = session;

0 commit comments

Comments
 (0)