forked from j62/ctbrec
1
0
Fork 0

Code cleanup

This commit is contained in:
0xboobface 2019-12-29 15:16:08 +01:00
parent 5d9762a8f7
commit e153a967f7
1 changed files with 39 additions and 38 deletions

View File

@ -58,7 +58,10 @@ import okhttp3.Response;
public class ThumbCell extends StackPane {
private static final transient Logger LOG = LoggerFactory.getLogger(ThumbCell.class);
private static final String COULDNT_START_STOP_RECORDING = "Couldn't start/stop recording";
private static final String COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL = "Couldn't update resolution tag for model {} - {}";
private static final String ERROR = "Error";
private static final Logger LOG = LoggerFactory.getLogger(ThumbCell.class);
private static final Duration ANIMATION_DURATION = new Duration(250);
private Model model;
@ -78,8 +81,8 @@ public class ThumbCell extends StackPane {
private PauseIndicator pausedIndicator;
private int index = 0;
ContextMenu popup;
private final Color colorNormal = Color.BLACK;
private final Color colorHighlight = Color.WHITE;
private static final Color colorNormal = Color.BLACK;
private static final Color colorHighlight = Color.WHITE;
private final Color colorRecording = new Color(0.8, 0.28, 0.28, .8);
private SimpleBooleanProperty selectionProperty = new SimpleBooleanProperty(false);
private double imgAspectRatio = 3.0 / 4.0;
@ -187,7 +190,7 @@ public class ThumbCell extends StackPane {
StackPane.setAlignment(selectionOverlay, Pos.TOP_LEFT);
getChildren().add(selectionOverlay);
setOnMouseEntered((e) -> {
setOnMouseEntered(e -> {
mouseHovering = true;
Color normal = recording ? colorRecording : colorNormal;
new ParallelTransition(changeColor(nameBackground, normal, colorHighlight), changeColor(name, colorHighlight, normal)).playFromStart();
@ -197,7 +200,7 @@ public class ThumbCell extends StackPane {
resolutionTag.setVisible(false);
}
});
setOnMouseExited((e) -> {
setOnMouseExited(e -> {
mouseHovering = false;
Color normal = recording ? colorRecording : colorNormal;
new ParallelTransition(changeColor(nameBackground, colorHighlight, normal), changeColor(name, normal, colorHighlight)).playFromStart();
@ -220,11 +223,11 @@ public class ThumbCell extends StackPane {
previewTrigger.setOpacity(.8);
previewTrigger.setMaxSize(s, s);
Polygon play = new Polygon(new double[] {
Polygon play = new Polygon(
16, 8,
26, 15,
16, 22
});
);
StackPane.setMargin(play, new Insets(0, 0, 0, 3));
play.setStyle("-fx-background-color: black;");
previewTrigger.getChildren().add(play);
@ -283,38 +286,38 @@ public class ThumbCell extends StackPane {
try {
updateResolutionTag(resolution);
} catch (Exception e) {
LOG.debug("Couldn't update resolution tag for model {} - {}", model.getName(), e.getLocalizedMessage());
LOG.debug(COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL, model.getName(), e.getLocalizedMessage());
}
});
} else {
ThumbOverviewTab.threadPool.submit(() -> {
try {
ThumbOverviewTab.resolutionProcessing.add(model);
int[] _resolution = model.getStreamResolution(false);
resolutionCache.put(model.getUrl(), _resolution);
updateResolutionTag(_resolution);
int[] res = model.getStreamResolution(false);
resolutionCache.put(model.getUrl(), res);
updateResolutionTag(res);
// the model is online, but the resolution is 0. probably something went wrong
// when we first requested the stream info, so we remove this invalid value from the "cache"
// so that it is requested again
if (model.isOnline() && _resolution[1] == 0) {
if (model.isOnline() && res[1] == 0) {
LOG.trace("Removing invalid resolution value for {}", model.getName());
model.invalidateCacheEntries();
}
Thread.sleep(100);
} catch (IOException e1) {
LOG.debug("Couldn't update resolution tag for model {} - {}", model.getName(), e1.getLocalizedMessage());
LOG.debug(COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL, model.getName(), e1.getLocalizedMessage());
} catch(InterruptedException e1) {
Thread.currentThread().interrupt();
LOG.debug("Couldn't update resolution tag for model {} - {}", model.getName(), e1.getLocalizedMessage());
LOG.debug(COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL, model.getName(), e1.getLocalizedMessage());
} catch(ExecutionException e) {
if(e.getCause() instanceof EOFException) {
LOG.debug("Couldn't update resolution tag for model {}. Playlist empty", model.getName());
} else if(e.getCause() instanceof ParseException) {
LOG.debug("Couldn't update resolution tag for model {} - {}", model.getName(), e.getMessage());
LOG.debug(COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL, model.getName(), e.getMessage());
} else {
LOG.debug("Couldn't update resolution tag for model {} - {}", model.getName(), e.getMessage());
LOG.debug(COULDNT_UPDATE_RESOLUTION_TAG_FOR_MODEL, model.getName(), e.getMessage());
}
} finally {
ThumbOverviewTab.resolutionProcessing.remove(model);
@ -331,7 +334,8 @@ public class ThumbCell extends StackPane {
LOG.trace("Model resolution {} {}x{}", model.getName(), resolution[0], resolution[1]);
LOG.trace("Resolution queue size: {}", ThumbOverviewTab.queue.size());
final int w = resolution[1];
tagText = w > 0 ? w != Integer.MAX_VALUE ? Integer.toString(w) : "HD" : state;
String width = w != Integer.MAX_VALUE ? Integer.toString(w) : "HD";
tagText = w > 0 ? width : state;
} else {
tagText = state;
resolutionBackgroundColor = resolutionOfflineColor;
@ -366,13 +370,10 @@ public class ThumbCell extends StackPane {
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
});
} else {
img.progressProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if(newValue.doubleValue() == 1.0) {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
}
img.progressProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
if(newValue.doubleValue() == 1.0) {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
}
});
}
@ -436,21 +437,21 @@ public class ThumbCell extends StackPane {
boolean selectSource = Config.getInstance().getSettings().chooseStreamQuality;
if(selectSource && start) {
Function<Model, Void> onSuccess = (model) -> {
_startStopAction(model, start);
Function<Model, Void> onSuccess = modl -> {
startStopActionAsync(modl, start);
return null;
};
Function<Throwable, Void> onFail = (throwable) -> {
Function<Throwable, Void> onFail = throwable -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setHeaderText("Couldn't start/stop recording");
alert.setTitle(ERROR);
alert.setHeaderText(COULDNT_START_STOP_RECORDING);
alert.setContentText("I/O error while starting/stopping the recording: " + throwable.getLocalizedMessage());
alert.showAndWait();
return null;
};
StreamSourceSelectionDialog.show(getScene(), model, onSuccess, onFail);
} else {
_startStopAction(model, start);
startStopActionAsync(model, start);
}
}
@ -468,7 +469,7 @@ public class ThumbCell extends StackPane {
LOG.error("Couldn't pause/resume recording", e1);
Platform.runLater(() -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setTitle(ERROR);
alert.setHeaderText("Couldn't pause/resume recording");
alert.setContentText("I/O error while pausing/resuming the recording: " + e1.getLocalizedMessage());
alert.showAndWait();
@ -479,7 +480,7 @@ public class ThumbCell extends StackPane {
}).start();
}
private void _startStopAction(Model model, boolean start) {
private void startStopActionAsync(Model model, boolean start) {
new Thread(() -> {
try {
if(start) {
@ -490,11 +491,11 @@ public class ThumbCell extends StackPane {
setRecording(false);
}
} catch (Exception e1) {
LOG.error("Couldn't start/stop recording", e1);
LOG.error(COULDNT_START_STOP_RECORDING, e1);
Platform.runLater(() -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setHeaderText("Couldn't start/stop recording");
alert.setTitle(ERROR);
alert.setHeaderText(COULDNT_START_STOP_RECORDING);
alert.setContentText("I/O error while starting/stopping the recording: " + e1.getLocalizedMessage());
alert.showAndWait();
});
@ -516,7 +517,7 @@ public class ThumbCell extends StackPane {
} else {
Platform.runLater(() -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setTitle(ERROR);
alert.setHeaderText("Couldn't follow model");
alert.setContentText("");
alert.showAndWait();
@ -532,7 +533,7 @@ public class ThumbCell extends StackPane {
} else {
Platform.runLater(() -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setTitle(ERROR);
alert.setHeaderText("Couldn't unfollow model");
alert.setContentText("");
alert.showAndWait();
@ -544,7 +545,7 @@ public class ThumbCell extends StackPane {
LOG.error("Couldn't follow/unfollow model {}", model.getName(), e1);
Platform.runLater(() -> {
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, getScene());
alert.setTitle("Error");
alert.setTitle(ERROR);
alert.setHeaderText("Couldn't follow/unfollow model");
alert.setContentText("I/O error while following/unfollowing model " + model.getName() + ": " + e1.getLocalizedMessage());
alert.showAndWait();