threaded initial api calls

This commit is contained in:
aj 2018-04-19 14:01:01 -07:00
parent 31146c5452
commit d3413d15eb

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.fx.controller;
import java.io.IOException; import java.io.IOException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -61,67 +62,71 @@ public class ControllerMain {
public void initialize() { public void initialize() {
Reference.setUserName("sarsoo"); Reference.setUserName("sarsoo");
labelStatsScrobblesToday.setText(Integer.toString(Getter.getScrobblesToday(Reference.getUserName()))); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
labelStatsUsername.setText(Reference.getUserName()); Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
// updateGenrePieChart();
labelStatsScrobblesTotal String scrobblesToday = numberFormat.format(Getter.getScrobblesToday(Reference.getUserName()));
.setText(NumberFormat.getInstance().format(Getter.getScrobbles(Reference.getUserName()))); String scrobbles = numberFormat.format(Getter.getScrobbles(Reference.getUserName()));
tags = Getter.getUserTags(Reference.getUserName());
tags = Getter.getUserTags(Reference.getUserName()); final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try{
int counter; labelStatsScrobblesToday.setText(scrobblesToday);
for (counter = 0; counter < tags.size(); counter++) { labelStatsUsername.setText(Reference.getUserName());
labelStatsScrobblesTotal.setText(scrobbles);
String name = tags.get(counter).getName().toLowerCase(); refreshPieCharts();
// System.out.println(name); int counter;
for (counter = 0; counter < tags.size(); counter++) {
MenuItem item = new MenuItem(name); String name = tags.get(counter).getName().toLowerCase();
item.setOnAction(new EventHandler<ActionEvent>() { // System.out.println(name);
@Override
public void handle(ActionEvent e) {
try {
FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name));
tabPane.getTabs().add(tab); MenuItem item = new MenuItem(name);
// System.out.println("tab added");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
menuTag.getItems().add(item); item.setOnAction(new EventHandler<ActionEvent>() {
} @Override
public void handle(ActionEvent e) {
try {
FMObjListTab tab = new FMObjListTab(Getter.getUserTag(Reference.getUserName(), name));
// FMObjList rap = Getter.getUserTag(Reference.getUserName(), "rap"); tabPane.getTabs().add(tab);
// FMObjList classicRap = Getter.getUserTag(Reference.getUserName(), "classic // System.out.println("tab added");
// rap"); } catch (IOException e1) {
// FMObjList grime = Getter.getUserTag(Reference.getUserName(), "grime"); e1.printStackTrace();
// }
// int rapTotal = rap.getTotalUserScrobbles() - }
// classicRap.getTotalUserScrobbles() - grime.getTotalUserScrobbles(); });
//
// ObservableList<PieChart.Data> rapData = FXCollections.observableArrayList( menuTag.getItems().add(item);
// new PieChart.Data("rap", rapTotal), }
// new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()),
// new PieChart.Data("grime", grime.getTotalUserScrobbles())); }finally{
// pieChartRap.setData(rapData); latch.countDown();
// }
// int other = Getter.getScrobbles(Reference.getUserName()) - }
// rap.getTotalUserScrobbles(); });
// latch.await();
// ObservableList<PieChart.Data> rapTotalData = //Keep with the background work
// FXCollections.observableArrayList( return null;
// new PieChart.Data("rap", rapTotal), }
// new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), };
// new PieChart.Data("grime", grime.getTotalUserScrobbles()), }
// new PieChart.Data("other", other)); };
// pieChartRapTotal.setData(rapTotalData); service.start();
} }
@ -283,53 +288,55 @@ public class ControllerMain {
public void refreshPieCharts() { public void refreshPieCharts() {
Service<Void> service = new Service<Void>() { Service<Void> service = new Service<Void>() {
@Override @Override
protected Task<Void> createTask() { protected Task<Void> createTask() {
return new Task<Void>() { return new Task<Void>() {
@Override @Override
protected Void call() throws Exception { protected Void call() throws Exception {
FMObjList rap = Getter.getUserTag(Reference.getUserName(), "rap"); FMObjList rap = Getter.getUserTag(Reference.getUserName(), "rap");
FMObjList classicRap = Getter.getUserTag(Reference.getUserName(), "classic rap"); FMObjList classicRap = Getter.getUserTag(Reference.getUserName(), "classic rap");
FMObjList grime = Getter.getUserTag(Reference.getUserName(), "grime"); FMObjList grime = Getter.getUserTag(Reference.getUserName(), "grime");
int rapTotal = rap.getTotalUserScrobbles() - classicRap.getTotalUserScrobbles() - grime.getTotalUserScrobbles(); int rapTotal = rap.getTotalUserScrobbles() - classicRap.getTotalUserScrobbles()
- grime.getTotalUserScrobbles();
ObservableList<PieChart.Data> rapData = FXCollections.observableArrayList(new PieChart.Data("rap", rapTotal), ObservableList<PieChart.Data> rapData = FXCollections.observableArrayList(
new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), new PieChart.Data("rap", rapTotal),
new PieChart.Data("grime", grime.getTotalUserScrobbles())); new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()),
new PieChart.Data("grime", grime.getTotalUserScrobbles()));
int other = Getter.getScrobbles(Reference.getUserName()) - rap.getTotalUserScrobbles(); int other = Getter.getScrobbles(Reference.getUserName()) - rap.getTotalUserScrobbles();
ObservableList<PieChart.Data> rapTotalData = FXCollections.observableArrayList( ObservableList<PieChart.Data> rapTotalData = FXCollections.observableArrayList(
new PieChart.Data("rap", rapTotal), new PieChart.Data("rap", rapTotal),
new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()), new PieChart.Data("classic rap", classicRap.getTotalUserScrobbles()),
new PieChart.Data("grime", grime.getTotalUserScrobbles()), new PieChart.Data("other", other)); new PieChart.Data("grime", grime.getTotalUserScrobbles()),
new PieChart.Data("other", other));
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try{ try {
pieChartRap.setData(rapData); pieChartRap.setData(rapData);
pieChartRapTotal.setData(rapTotalData);
pieChartRapTotal.setData(rapTotalData); accordionCharts.setExpandedPane(titledPaneRap);
} finally {
accordionCharts.setExpandedPane(titledPaneRap); latch.countDown();
}finally{ }
latch.countDown(); }
} });
} latch.await();
}); // Keep with the background work
latch.await(); return null;
//Keep with the background work }
return null; };
} }
}; };
} service.start();
};
service.start();
} }
} }