jafea7-ctbrec-v5.3.2-based/client/src/main/java/ctbrec/ui/menu/FollowUnfollowHandler.java

46 lines
1.7 KiB
Java

package ctbrec.ui.menu;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import ctbrec.Model;
import ctbrec.recorder.Recorder;
import ctbrec.ui.action.AbstractModelAction.Result;
import ctbrec.ui.action.FollowAction;
import ctbrec.ui.action.TriConsumer;
import ctbrec.ui.action.UnfollowAction;
import javafx.scene.Node;
@Slf4j
public class FollowUnfollowHandler {
private Node source;
private Recorder recorder;
private TriConsumer<Model, Boolean, Boolean> callback;
public FollowUnfollowHandler(Node source, Recorder recorder, TriConsumer<Model, Boolean, Boolean> callback) {
this.source = source;
this.recorder = recorder;
this.callback = callback;
}
protected void follow(List<Model> selectedModels) {
new FollowAction(source, selectedModels, recorder).execute().thenAccept(r -> {
r.stream().filter(rs -> rs.getThrowable() == null).map(Result::getModel).forEach(m -> callback.accept(m, true, true));
r.stream().filter(rs -> rs.getThrowable() != null).map(Result::getModel).forEach(m -> callback.accept(m, true, false));
}).exceptionally(ex -> {
log.error("Couldn't follow model", ex);
return null;
});
}
protected void unfollow(List<Model> selectedModels) {
new UnfollowAction(source, selectedModels, recorder).execute().thenAccept(r -> {
r.stream().filter(rs -> rs.getThrowable() == null).map(Result::getModel).forEach(m -> callback.accept(m, false, true));
r.stream().filter(rs -> rs.getThrowable() != null).map(Result::getModel).forEach(m -> callback.accept(m, false, false));
}).exceptionally(ex -> {
log.error("Couldn't unfollow model", ex);
return null;
});
}
}