ctbrec-5.3.2-experimental/client/src/main/java/ctbrec/ui/action/TipAction.java

57 lines
1.8 KiB
Java

package ctbrec.ui.action;
import lombok.extern.slf4j.Slf4j;
import static ctbrec.ui.controls.Dialogs.*;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import ctbrec.Model;
import ctbrec.event.EventBusHolder;
import ctbrec.ui.SiteUiFactory;
import ctbrec.ui.TipDialog;
import javafx.scene.Cursor;
import javafx.scene.Node;
@Slf4j
public class TipAction {
private Model model;
private Node source;
public TipAction(Model model, Node source) {
this.model = model;
this.source = source;
}
public void execute() {
source.setCursor(Cursor.WAIT);
try {
var site = model.getSite();
var tipDialog = new TipDialog(source.getScene(), site);
tipDialog.showAndWait();
String tipText = tipDialog.getResult();
if (tipText != null) {
var df = new DecimalFormat("0.##");
try {
Number tokens = df.parse(tipText);
SiteUiFactory.getUi(site).login();
model.receiveTip(tokens.doubleValue());
Map<String, Object> event = new HashMap<>();
event.put("event", "tokens.sent");
event.put("amount", tokens.doubleValue());
EventBusHolder.BUS.post(event);
} catch (IOException ex) {
log.error("An error occurred while sending tip", ex);
showError(source.getScene(), "Couldn't send tip", "An error occurred while sending tip:", ex);
} catch (Exception ex) {
showError(source.getScene(), "Couldn't send tip", "You entered an invalid amount of tokens", ex);
}
}
} finally {
source.setCursor(Cursor.DEFAULT);
}
}
}