Almost completely separates the GUI from the logic.

This will hopefully make debugging easier as well as make a CLI possible.
This commit is contained in:
Markil3
2021-09-14 17:02:18 -06:00
parent 999f3345cc
commit 55f879fedd
4 changed files with 129 additions and 95 deletions

View File

@@ -82,34 +82,6 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL
return INSTANCE;
}
public static void main(String[] args)
{
Interface inter = null;
PlayerManager.getPlayers();
try
{
/*
* Add this just in case of a crash or something. It won't work if the
* program is forcibly terminated by the OS, but it could be helpful
* otherwise.
*/
logger.info("Starting application");
inter = new Interface();
inter.setSize(700, 500);
SongProvider.INSTANCE.addUpdateListener(inter);
inter.setVisible(true);
}
catch (Throwable e)
{
logger.error("Could not open browser background", e);
JOptionPane
.showMessageDialog(inter != null && inter
.isVisible() ? inter : null, e, langs
.getString("error.generic"), JOptionPane.ERROR_MESSAGE);
}
}
/**
* Creates an interface
*/
@@ -469,7 +441,7 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL
{
if (this.isEnabled())
{
controls.previousSong();
Queue.getInstance().skipPrev();
}
}
});
@@ -482,7 +454,7 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL
{
if (this.isEnabled())
{
controls.nextSong();
Queue.getInstance().skipNext();
}
}
});

View File

@@ -37,7 +37,7 @@ import java.util.concurrent.ForkJoinPool;
* @author William Hubbard
* @version 0.1
*/
public class PlayerControls extends JPanel implements Queue.SongChangeListener, PlaybackListener
public class PlayerControls extends JPanel implements PlaybackListener
{
private static final Logger logger = LoggerFactory
.getLogger(PlayerControls.class);
@@ -175,7 +175,6 @@ public class PlayerControls extends JPanel implements Queue.SongChangeListener,
layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, this.updateProgress);
layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, this.updateProgress);
Queue.getInstance().addSongChangeListener(this);
PlayerManager.getPlayers().addPlaybackListener(this);
}
@@ -436,70 +435,18 @@ public class PlayerControls extends JPanel implements Queue.SongChangeListener,
}
}
@Override
public void onSongChange(Queue queue)
{
this.service.execute(() -> {
QueryFuture<Void> command = PlayerManager.getPlayers().stopSong();
try
{
if (command != null && !command.getConfirmation().wasSuccessful())
{
JOptionPane.showMessageDialog(this,
command.getConfirmation().getError(),
command.getConfirmation().getMessage(),
JOptionPane.ERROR_MESSAGE);
logger.error("Could not run command",
command.getConfirmation().getError());
}
else
{
command =
PlayerManager.getPlayers()
.playSong(queue.getCurrentSong());
if (!command.getConfirmation().wasSuccessful())
{
JOptionPane.showMessageDialog(this,
command.getConfirmation().getError(),
command.getConfirmation().getMessage(),
JOptionPane.ERROR_MESSAGE);
logger.error("Could not run command",
command.getConfirmation().getError());
}
else
{
SwingUtilities.invokeLater(() -> {
this.playButton.setIcon(PLAY_ICON);
this.progress.setMaximum((int) (PlayerManager
.getPlayers()
.getCurrentSong().duration / 1000));
});
}
}
}
catch (ExecutionException | InterruptedException e)
{
logger.error("Could not get current playback status", e);
JOptionPane
.showMessageDialog(this, e.getMessage(), langs
.getString(
"error.command"), JOptionPane.ERROR_MESSAGE);
}
});
}
@Override
public void onPlaybackChanged(PlaybackEvent status)
{
switch (status.getInfo().getStatus())
{
case PLAYING -> this.playButton.setIcon(PAUSE_ICON);
case FINISHED -> Queue.getInstance().skipNext();
case PAUSED, STOPPED, EMPTY -> this.playButton.setIcon(PLAY_ICON);
}
this.progress.setValue((int) status.getInfo().getPlayTime());
this.progress.setMaximum((int) (status.getInfo()
.getSong().duration / 1000));
SwingUtilities.invokeLater(() -> {
switch (status.getInfo().getStatus())
{
case PLAYING -> this.playButton.setIcon(PAUSE_ICON);
case PAUSED, STOPPED, EMPTY -> this.playButton.setIcon(PLAY_ICON);
}
this.progress.setValue((int) status.getInfo().getPlayTime());
this.progress.setMaximum((int) (status.getInfo()
.getSong().duration / 1000));
});
}
}