Skip to content

Commit 640d4b7

Browse files
committed
Some refactoring
1 parent fdbe572 commit 640d4b7

5 files changed

Lines changed: 36 additions & 17 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ private void loadAutoplay() {
490490
} else if (resp.statusCode == 204) {
491491
MercuryRequests.StationsWrapper station = session.mercury().sendSync(MercuryRequests.getStationFor(context));
492492
state.loadStation(station);
493+
494+
state.setPositionMs(0);
495+
state.setPositionMeasuredAt(TimeProvider.currentTimeMillis());
493496
loadTrack(true);
494497

495498
LOGGER.debug(String.format("Loading context for autoplay (using radio-apollo), uri: %s", state.getContextUri()));

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import xyz.gianlu.librespot.player.remote.Remote3Page;
1919
import xyz.gianlu.librespot.player.remote.Remote3Track;
2020
import xyz.gianlu.librespot.player.tracks.PlayablesProvider;
21-
import xyz.gianlu.librespot.player.tracks.PlaylistProvider;
21+
import xyz.gianlu.librespot.player.tracks.ShuffleableProvider;
2222
import xyz.gianlu.librespot.player.tracks.StationProvider;
2323
import xyz.gianlu.librespot.spirc.SpotifyIrc;
2424

@@ -80,31 +80,31 @@ boolean isStatus(@NotNull Spirc.PlayStatus status) {
8080

8181
void setShuffle(boolean shuffle) {
8282
state.setShuffle(shuffle && (playablesProvider == null || playablesProvider.canShuffle()));
83-
if (state.getShuffle()) shuffleTracks(false);
84-
else unshuffleTracks();
83+
if (state.getShuffle()) shuffleContent(false);
84+
else unshuffleContent();
8585
}
8686

8787
private void loadPlayablesProvider(@NotNull String uri) throws SpotifyContext.UnsupportedContextException {
8888
SpotifyContext context = SpotifyContext.from(uri);
8989
playablesProvider = context.initProvider(session, state);
9090
}
9191

92-
private void shuffleTracks(boolean fully) {
92+
private void shuffleContent(boolean fully) {
9393
if (playablesProvider == null) return;
9494

95-
if (playablesProvider.canShuffle() && playablesProvider instanceof PlaylistProvider)
96-
((PlaylistProvider) playablesProvider).shuffleTracks(session.random(), fully);
95+
if (playablesProvider.canShuffle() && playablesProvider instanceof ShuffleableProvider)
96+
((ShuffleableProvider) playablesProvider).shuffleContent(session.random(), fully);
9797
else
98-
LOGGER.warn("Cannot shuffle TracksProvider: " + playablesProvider);
98+
LOGGER.warn("Cannot shuffle provider: " + playablesProvider);
9999
}
100100

101-
private void unshuffleTracks() {
101+
private void unshuffleContent() {
102102
if (playablesProvider == null) return;
103103

104-
if (playablesProvider.canShuffle() && playablesProvider instanceof PlaylistProvider)
105-
((PlaylistProvider) playablesProvider).unshuffleTracks();
104+
if (playablesProvider.canShuffle() && playablesProvider instanceof ShuffleableProvider)
105+
((ShuffleableProvider) playablesProvider).unshuffleContent();
106106
else
107-
LOGGER.warn("Cannot unshuffle TracksProvider: " + playablesProvider);
107+
LOGGER.warn("Cannot unshuffle provider: " + playablesProvider);
108108
}
109109

110110
void updated() {
@@ -163,9 +163,9 @@ int getTrackCount() {
163163
void loadStation(@NotNull MercuryRequests.StationsWrapper station) throws SpotifyContext.UnsupportedContextException {
164164
state.setContextUri(station.uri());
165165

166-
state.setPlayingTrackIndex(0);
167166
state.clearTrack();
168167
state.addAllTrack(station.tracks());
168+
state.setPlayingTrackIndex(0);
169169
SpotifyIrc.trimTracks(state);
170170

171171
loadPlayablesProvider(station.uri());
@@ -228,7 +228,7 @@ private void loadPage(@NotNull Remote3Page page, @Nullable TrackSelector selecto
228228
state.setPlayingTrackIndex(selector == null ? 0 : selector.playingIndex());
229229
SpotifyIrc.trimTracks(state);
230230

231-
if (state.getShuffle()) shuffleTracks(selector == null || !selector.findMatch());
231+
if (state.getShuffle()) shuffleContent(selector == null || !selector.findMatch());
232232
}
233233

234234
void load(@NotNull Remote3Frame frame) throws IOException, MercuryClient.MercuryException, SpotifyContext.UnsupportedContextException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public interface PlayablesProvider {
1515

16-
static int getPrevTrackIndex(Spirc.State.Builder state) {
16+
static int getPrevTrackIndex(@NotNull Spirc.State.Builder state) {
1717
List<Spirc.TrackRef> queueTracks = new ArrayList<>();
1818
Iterator<Spirc.TrackRef> iter = state.getTrackList().iterator();
1919
while (iter.hasNext()) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* @author Gianlu
2222
*/
23-
public class PlaylistProvider implements PlayablesProvider {
23+
public class PlaylistProvider implements PlayablesProvider, ShuffleableProvider {
2424
private static final Logger LOGGER = Logger.getLogger(PlaylistProvider.class);
2525
private final Spirc.State.Builder state;
2626
private final MercuryClient mercury;
@@ -41,7 +41,8 @@ private static int[] getShuffleExchanges(int size, long seed) {
4141
return exchanges;
4242
}
4343

44-
public void shuffleTracks(@NotNull Random random, boolean fully) {
44+
@Override
45+
public void shuffleContent(@NotNull Random random, boolean fully) {
4546
if (state.getTrackCount() <= 1)
4647
return;
4748

@@ -71,7 +72,8 @@ public void shuffleTracks(@NotNull Random random, boolean fully) {
7172
LOGGER.trace("Shuffled, seed: " + shuffleSeed);
7273
}
7374

74-
public void unshuffleTracks() {
75+
@Override
76+
public void unshuffleContent() {
7577
if (state.getTrackCount() <= 1)
7678
return;
7779

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package xyz.gianlu.librespot.player.tracks;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.Random;
6+
7+
/**
8+
* @author Gianlu
9+
*/
10+
public interface ShuffleableProvider {
11+
void shuffleContent(@NotNull Random random, boolean fully);
12+
13+
void unshuffleContent();
14+
}

0 commit comments

Comments
 (0)