From f9b81093db3f308a7eb309fe6662d978a2f65e1f Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 23 May 2019 15:09:07 +0100 Subject: [PATCH] added artist scrobble getting --- .../ScrobblesViewPaneController.java | 35 ++++++++++++----- .../ArtistBorderPaneController.java | 18 +++++++++ .../util/ScrobbleCountCalendar.java | 39 +++++++++++-------- .../fmframework/fx/ui/FMObjScrobblePane.fxml | 1 + 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/main/java/sarsoo/fmframework/fx/controller/ScrobblesViewPaneController.java b/src/main/java/sarsoo/fmframework/fx/controller/ScrobblesViewPaneController.java index 9c99366..e753a9e 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/ScrobblesViewPaneController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/ScrobblesViewPaneController.java @@ -4,6 +4,7 @@ import java.text.NumberFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -48,8 +49,13 @@ public class ScrobblesViewPaneController { @FXML private CheckBox checkBoxCumulative; + + @FXML + private CheckBox checkBoxTotal; private FMObj obj; + + private List scrobbles; private LocalDate firstDate; @@ -63,10 +69,19 @@ public class ScrobblesViewPaneController { } public void populate(FMObj obj) { - this.obj = obj; - - ArrayList scrobbles = obj.getScrobbles(); + this.scrobbles = obj.getScrobbles(); + populate(); + } + + public void populate(ArrayList scrobbles) { + this.scrobbles = scrobbles; + populate(); + } + + private void populate() { + + scrobbles.sort(Comparator.comparing(Scrobble::getDateTime).reversed()); if (scrobbles != null) { if (scrobbles.size() > 0) { @@ -115,7 +130,7 @@ public class ScrobblesViewPaneController { ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total"); - for (Scrobble scrobble : obj.getScrobbles()) { + for (Scrobble scrobble : scrobbles) { Boolean needNew = true; @@ -145,7 +160,7 @@ public class ScrobblesViewPaneController { List toShow = list.stream().map(FMObjCalendarWrapper::getCalendar) .collect(Collectors.toList()); - if (list.size() > 1) { + if (list.size() > 1 && checkBoxTotal.isSelected()) { toShow.add(totalCalendar); } @@ -159,7 +174,7 @@ public class ScrobblesViewPaneController { ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total"); - for (Scrobble scrobble : obj.getScrobbles()) { + for (Scrobble scrobble : scrobbles) { Boolean needNew = true; @@ -189,7 +204,7 @@ public class ScrobblesViewPaneController { List toShow = list.stream().map(FMObjCalendarWrapper::getCalendar) .collect(Collectors.toList()); - if (list.size() > 1) { + if (list.size() > 1 && checkBoxTotal.isSelected()) { toShow.add(totalCalendar); } @@ -203,7 +218,7 @@ public class ScrobblesViewPaneController { ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total"); - for (Scrobble scrobble : obj.getScrobbles()) { + for (Scrobble scrobble : scrobbles) { Boolean needNew = true; @@ -221,7 +236,7 @@ public class ScrobblesViewPaneController { if (needNew) { - ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(firstDate, scrobble.getAlbum().getName()); + ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(firstDate, scrobble.getTrack().getName() + " / " + scrobble.getAlbum().getName()); calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear()); @@ -234,7 +249,7 @@ public class ScrobblesViewPaneController { List toShow = list.stream().map(FMObjCalendarWrapper::getCalendar) .collect(Collectors.toList()); - if (list.size() > 1) { + if (list.size() > 1 && checkBoxTotal.isSelected()) { toShow.add(totalCalendar); } diff --git a/src/main/java/sarsoo/fmframework/fx/controller/borderpane/ArtistBorderPaneController.java b/src/main/java/sarsoo/fmframework/fx/controller/borderpane/ArtistBorderPaneController.java index ef0e76a..4a0c392 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/borderpane/ArtistBorderPaneController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/borderpane/ArtistBorderPaneController.java @@ -4,6 +4,7 @@ import java.awt.Desktop; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import javafx.application.Platform; import javafx.concurrent.Service; @@ -17,6 +18,9 @@ import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.controller.info.ArtistPaneController; import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Scrobble; +import sarsoo.fmframework.music.Track; +import sarsoo.fmframework.util.FMObjList; public class ArtistBorderPaneController extends FMObjBorderPaneController{ @@ -91,12 +95,26 @@ public class ArtistBorderPaneController extends FMObjBorderPaneController{ protected Void call() throws Exception { artist = FmFramework.getArtistPool().getNew(artist); + + Config config = FmFramework.getSessionConfig(); + FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); + + FMObjList topTracks = net.getArtistTopTracks(artist, 10); + + ArrayList scrobbles = new ArrayList<>(); + + topTracks.stream().forEach(t -> { + + scrobbles.addAll(net.getTrackScrobbles((Track) t)); + + }); Platform.runLater(new Runnable() { @Override public void run() { infoPaneController.refresh(artist); + scrobblePaneController.populate(scrobbles); } }); diff --git a/src/main/java/sarsoo/fmframework/util/ScrobbleCountCalendar.java b/src/main/java/sarsoo/fmframework/util/ScrobbleCountCalendar.java index 77e6c8f..371d4cb 100644 --- a/src/main/java/sarsoo/fmframework/util/ScrobbleCountCalendar.java +++ b/src/main/java/sarsoo/fmframework/util/ScrobbleCountCalendar.java @@ -9,41 +9,46 @@ import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.entry.ErrorEntry; public class ScrobbleCountCalendar { - + private ArrayList months; private String name; - + public ScrobbleCountCalendar(LocalDate date, String name) { - + months = new ArrayList<>(); this.name = name; - + LocalDate now = LocalDate.now(); long monthsDiff = ChronoUnit.MONTHS.between(date, now) + 1; - - for(int i = 0; i < monthsDiff; i++) { + + for (int i = 0; i < monthsDiff; i++) { LocalDate counter = date.plusMonths(i); - + months.add(new MonthScrobbles(counter.getMonth(), counter.getYear())); } - + } - + public void addCount(Month monthIn, int year) { - - for(MonthScrobbles month: months) { - if(month.getMonth().getValue() == monthIn.getValue() && year == month.getYear()) { + + Boolean found = false; + + for (MonthScrobbles month : months) { + if (month.getMonth().getValue() == monthIn.getValue() && year == month.getYear()) { month.addCount(); + found = true; break; } } - - Logger.getLog().logError(new ErrorEntry("ScrobbleCountCalendar").addArg("add count").addArg("month not found")); - + + if (found == false) + Logger.getLog().logError(new ErrorEntry("ScrobbleCountCalendar").addArg("add count") + .addArg("month not found").addArg(monthIn.toString()).addArg(Integer.toString(year))); + } - - public ArrayList getMonthScrobbles(){ + + public ArrayList getMonthScrobbles() { return months; } diff --git a/src/main/resources/sarsoo/fmframework/fx/ui/FMObjScrobblePane.fxml b/src/main/resources/sarsoo/fmframework/fx/ui/FMObjScrobblePane.fxml index 0f76527..95551e9 100644 --- a/src/main/resources/sarsoo/fmframework/fx/ui/FMObjScrobblePane.fxml +++ b/src/main/resources/sarsoo/fmframework/fx/ui/FMObjScrobblePane.fxml @@ -44,6 +44,7 @@