separating services and working on threading

This commit is contained in:
aj 2019-05-03 23:19:55 +01:00
parent 994b8e2e6b
commit b56bfec0b7
6 changed files with 287 additions and 163 deletions

View File

@ -21,6 +21,11 @@ import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.fx.TextAreaConsole;
import sarsoo.fmframework.fx.chart.GenrePieChartTitledPane; import sarsoo.fmframework.fx.chart.GenrePieChartTitledPane;
import sarsoo.fmframework.fx.chart.PieChartTitledPane; import sarsoo.fmframework.fx.chart.PieChartTitledPane;
import sarsoo.fmframework.fx.service.GetLastTrackService;
import sarsoo.fmframework.fx.service.GetScrobbleCountService;
import sarsoo.fmframework.fx.service.GetTagMenuItemsService;
import sarsoo.fmframework.fx.service.GetTagsService;
import sarsoo.fmframework.fx.service.ScrobbleCount;
import sarsoo.fmframework.fx.tab.AlbumTab; import sarsoo.fmframework.fx.tab.AlbumTab;
import sarsoo.fmframework.fx.tab.ArtistTab; import sarsoo.fmframework.fx.tab.ArtistTab;
import sarsoo.fmframework.fx.tab.ConsoleTab; import sarsoo.fmframework.fx.tab.ConsoleTab;
@ -55,116 +60,100 @@ public class RootController {
@FXML @FXML
public void initialize() { public void initialize() {
// Reference.setUserName("sarsoo");
Logger.setLog(new Log(TextAreaConsole.getInstance(), false)); Logger.setLog(new Log(TextAreaConsole.getInstance(), false));
// ConsoleHandler.setVerbose(TextAreaConsole.getInstance());
refresh(); refresh();
} }
public void refresh() { public void refresh() {
labelStatsUsername.setText(Reference.getUserName());
refreshScrobbleCounts();
addLastTrackTab();
refreshTagMenu();
}
public void refreshScrobbleCounts() {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK);
GetScrobbleCountService getScrobbles = new GetScrobbleCountService();
getScrobbles.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
Service<Void> service = new Service<Void>() {
@Override @Override
protected Task<Void> createTask() { public void handle(WorkerStateEvent t) {
return new Task<Void>() { Platform.runLater(new Runnable() {
@Override @Override
protected Void call() throws Exception { public void run() {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); labelStatsScrobblesToday.setText(
numberFormat.format(((ScrobbleCount) t.getSource().getValue()).getDailyCount()));
labelStatsScrobblesTotal.setText(
numberFormat.format(((ScrobbleCount) t.getSource().getValue()).getTotalCount()));
}
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); });
}
});
getScrobbles.start();
}
String scrobblesToday = numberFormat.format(net.getScrobblesToday()); public void addLastTrackTab() {
String scrobbles = numberFormat.format(net.getUserScrobbleCount()); GetLastTrackService getLastTrack = new GetLastTrackService();
getLastTrack.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
try {
TrackTab tab = new TrackTab(((Track) t.getSource().getValue()));
TrackTab tab = new TrackTab(net.getLastTrack());
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
labelStatsScrobblesToday.setText(scrobblesToday);
labelStatsUsername.setText(Reference.getUserName());
labelStatsScrobblesTotal.setText(scrobbles);
addTab(tab); addTab(tab);
}
// refreshPieCharts(); });
refreshTagMenu(); } catch (IOException e) {
refreshPieChartMenu(); e.printStackTrace();
} finally {
latch.countDown();
} }
} }
}); });
latch.await(); getLastTrack.start();
// Keep with the background work
return null;
}
};
}
};
service.start();
} }
public void refreshTagMenu() { public void refreshTagMenu() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
tags = net.getTags(); GetTagsService getTags = new GetTagsService();
getTags.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
tags = (ArrayList<Tag>) t.getSource().getValue();
Collections.sort(tags); Collections.sort(tags);
int counter; GetTagMenuItemsService getTagMenuItems = new GetTagMenuItemsService(tags);
for (counter = 0; counter < tags.size(); counter++) {
String name = tags.get(counter).getName().toLowerCase(); getTagMenuItems.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
// System.out.println(name);
MenuItem item = new MenuItem(name);
item.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent e) { public void handle(WorkerStateEvent t) {
// TAG ITEM HANDLER SERVICE
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
FMObjListTab tab = new FMObjListTab(TagPool.getPool().getTag(name));
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try { menuTag.getItems().setAll((ArrayList<MenuItem>) t.getSource().getValue());
tabPane.getTabs().add(tab);
} finally {
latch.countDown();
}
} }
}); });
latch.await();
// Keep with the background work
return null;
}
};
}
};
service.start();
} }
}); });
menuTag.getItems().add(item); getTagMenuItems.start();
} }
});
getTags.start();
} }
public void refreshPieChartMenu() { public void refreshPieChartMenu() {
@ -229,18 +218,18 @@ public class RootController {
GenrePieChartTitledPane pane = new GenrePieChartTitledPane(hierarchyName, GenrePieChartTitledPane pane = new GenrePieChartTitledPane(hierarchyName,
hierarchyTagNameList); hierarchyTagNameList);
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 {
accordionCharts.getPanes().add(pane); accordionCharts.getPanes().add(pane);
} finally { } finally {
latch.countDown(); // latch.countDown();
} }
} }
}); });
latch.await(); // latch.await();
// Keep with the background work // Keep with the background work
return null; return null;
} }
@ -325,7 +314,7 @@ public class RootController {
} }
paneList.add(new PieChartTitledPane("total", allTags)); paneList.add(new PieChartTitledPane("total", allTags));
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() {
@ -336,11 +325,11 @@ public class RootController {
accordionCharts.getPanes().add(paneList.get(i)); accordionCharts.getPanes().add(paneList.get(i));
} }
} finally { } finally {
latch.countDown(); // latch.countDown();
} }
} }
}); });
latch.await(); // latch.await();
return null; return null;
} }
}; };
@ -366,13 +355,6 @@ public class RootController {
@FXML @FXML
protected void handleChangeUsername(ActionEvent event) throws IOException { protected void handleChangeUsername(ActionEvent event) throws IOException {
// System.out.println("USERNAME");
// String username = JOptionPane.showInputDialog("enter username:");
// if(username != null) {
// Reference.setUserName(username);
// }
// refresh();
Service<Void> service = new Service<Void>() { Service<Void> service = new Service<Void>() {
@Override @Override
protected Task<Void> createTask() { protected Task<Void> createTask() {
@ -380,25 +362,17 @@ public class RootController {
@Override @Override
protected Void call() throws Exception { protected Void call() throws Exception {
System.out.println("USERNAME");
String username = JOptionPane.showInputDialog("enter username:"); String username = JOptionPane.showInputDialog("enter username:");
if (username != null) { if (username != null) {
Reference.setUserName(username); Reference.setUserName(username);
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
refresh(); refresh();
} finally {
latch.countDown();
}
} }
}); });
latch.await();
} }
// Keep with the background work
return null; return null;
} }
}; };
@ -421,18 +395,18 @@ public class RootController {
if (album != null) { if (album != null) {
AlbumTab tab = new AlbumTab(album); AlbumTab tab = new AlbumTab(album);
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 {
tabPane.getTabs().add(tab); tabPane.getTabs().add(tab);
} finally { } finally {
latch.countDown(); // latch.countDown();
} }
} }
}); });
latch.await(); // latch.await();
} }
// Keep with the background work // Keep with the background work
return null; return null;
@ -458,20 +432,19 @@ public class RootController {
if (artist != null) { if (artist != null) {
ArtistTab tab = new ArtistTab(artist); ArtistTab tab = new ArtistTab(artist);
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 {
tabPane.getTabs().add(tab); tabPane.getTabs().add(tab);
} finally { } finally {
latch.countDown(); // latch.countDown();
} }
} }
}); });
latch.await(); // latch.await();
} }
// Keep with the background work
return null; return null;
} }
}; };
@ -495,18 +468,18 @@ public class RootController {
if (track != null) { if (track != null) {
TrackTab tab = new TrackTab(track); TrackTab tab = new TrackTab(track);
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 {
tabPane.getTabs().add(tab); tabPane.getTabs().add(tab);
} finally { } finally {
latch.countDown(); // latch.countDown();
} }
} }
}); });
latch.await(); // latch.await();
} }
// Keep with the background work // Keep with the background work
return null; return null;
@ -519,39 +492,7 @@ public class RootController {
@FXML @FXML
protected void handleCurrentTrack(ActionEvent event) throws IOException { protected void handleCurrentTrack(ActionEvent event) throws IOException {
Service<Void> service = new Service<Void>() { addLastTrackTab();
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
Track track = new FmUserNetwork(Key.getKey(), Reference.getUserName()).getLastTrack();
if (track != null) {
TrackTab tab = new TrackTab(track);
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
tabPane.getTabs().add(tab);
} finally {
latch.countDown();
}
}
});
latch.await();
}
// Keep with the background work
return null;
}
};
}
};
service.start();
} }
@FXML @FXML

View File

@ -0,0 +1,27 @@
package sarsoo.fmframework.fx.service;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.Reference;
public class GetLastTrackService extends Service<Track>{
@Override
protected Task<Track> createTask() {
return new Task<Track>() {
@Override
protected Track call() throws Exception {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
return net.getLastTrack();
}
};
}
}

View File

@ -0,0 +1,26 @@
package sarsoo.fmframework.fx.service;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.Reference;
public class GetScrobbleCountService extends Service<ScrobbleCount>{
@Override
protected Task<ScrobbleCount> createTask() {
return new Task<ScrobbleCount>() {
@Override
protected ScrobbleCount call() throws Exception {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
return new ScrobbleCount(net.getScrobblesToday(), net.getUserScrobbleCount());
}
};
}
}

View File

@ -0,0 +1,81 @@
package sarsoo.fmframework.fx.service;
import java.util.ArrayList;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.MenuItem;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.tab.FMObjListTab;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.util.tagpool.TagPool;
public class GetTagMenuItemsService extends Service<ArrayList<MenuItem>> {
private ArrayList<Tag> tags;
public GetTagMenuItemsService(ArrayList<Tag> tags) {
super();
this.tags = tags;
}
@Override
protected Task<ArrayList<MenuItem>> createTask() {
return new Task<ArrayList<MenuItem>>() {
@Override
protected ArrayList<MenuItem> call() throws Exception {
ArrayList<MenuItem> items = new ArrayList<MenuItem>();
int counter;
for (counter = 0; counter < tags.size(); counter++) {
String name = tags.get(counter).getName().toLowerCase();
// System.out.println(name);
MenuItem item = new MenuItem(name);
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// TAG ITEM HANDLER SERVICE
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
FMObjListTab tab = new FMObjListTab(TagPool.getPool().getTag(name));
Platform.runLater(new Runnable() {
@Override
public void run() {
FmFramework.getController().addTab(tab);
}
});
return null;
}
};
}
};
service.start();
}
});
items.add(item);
}
return items;
}
};
}
}

View File

@ -0,0 +1,29 @@
package sarsoo.fmframework.fx.service;
import java.util.ArrayList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.Reference;
public class GetTagsService extends Service<ArrayList<Tag>>{
@Override
protected Task<ArrayList<Tag>> createTask() {
return new Task<ArrayList<Tag>>() {
@Override
protected ArrayList<Tag> call() throws Exception {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
return net.getTags();
}
};
}
}

View File

@ -0,0 +1,20 @@
package sarsoo.fmframework.fx.service;
public class ScrobbleCount {
private int dailyCount;
private int totalCount;
public ScrobbleCount(int dailyCount, int totalCount) {
this.dailyCount = dailyCount;
this.totalCount = totalCount;
}
public int getDailyCount() {
return dailyCount;
}
public int getTotalCount() {
return totalCount;
}
}