Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

song search

  • Loading branch information...
commit 1a7cb7119619db92fb7caf3bee63e48ef7cac69f 1 parent a765b13
@camillol authored
Showing with 87 additions and 3 deletions.
  1. +36 −0 ArtistDetailView.pde
  2. +51 −3 Data.pde
View
36 ArtistDetailView.pde
@@ -127,12 +127,48 @@ class ArtistDetailView extends View {
class SongDetailView extends View {
TextField searchField;
+ Button searchButton;
+ TableView searchResults;
+
+ Song song;
SongDetailView(float x_, float y_, float w_, float h_){
super(x_,y_,w_,h_);
+ song = null;
+
searchField = new TextField(20, 20, 300, 20);
subviews.add(searchField);
+
+ searchButton = new Button(320, 20, 100, 20, "Search");
+ searchButton.setAction(new Action<Button>() {
+ public void respond(Button b) {
+ searchResults.data = new AsyncTableDataSource(data.searchSongs(searchField.value));
+ }
+ });
+ subviews.add(searchButton);
+
+ searchResults = new TableView(20, 40, 400, 100, Arrays.asList(
+ new TableColumn("Name", 100), new TableColumn("Artist", 100)), new MissingTableDataSource("no data"));
+ this.subviews.add(searchResults);
+ }
+
+ String getTitle()
+ {
+ if (song == null) return "<no song>";
+ else return song.title;
+ }
+
+ void drawTitle(){
+ textAlign(TOP);
+ fill(200);
+ textSize(40);
+ text(getTitle(), 20, 180);
+ textSize(normalFontSize);
+ }
+
+ void drawContent(float lx, float ly){
+ drawTitle();
}
}
View
54 Data.pde
@@ -230,18 +230,35 @@ class Country {
class Song {
int id;
- String ref;
+ String mbid;
String title;
Artist artist;
- Song(int id, String ref, String title, Artist artist) {
+ Song(int id, String mbid, String title, Artist artist) {
this.title = title;
this.id = id;
- this.ref = ref;
+ this.mbid = mbid;
this.artist = artist;
}
}
+class SongList implements TableDataSource {
+ List<Song> songs;
+
+ SongList(List<Song> songs) {
+ this.songs = songs;
+ }
+
+ String getText(int index, int column) {
+ if (column == 0) return songs.get(index).title;
+ else return songs.get(index).artist.name;
+ }
+ Object get(int index) { return songs.get(index); }
+ int count() { return songs.size(); }
+ boolean selected(int index) { return false; }
+ PImage getImage(int index, int column){ return null; }
+}
+
class ArtistList implements TableDataSource {
List<Artist> artists;
@@ -730,6 +747,37 @@ class WebDataSource {
}
return null;
}
+
+ Future<SongList> searchSongs(final String q){
+ return loadExec.submit(new Callable<SongList>() {
+ public SongList call() {
+ List<Song> songs = new ArrayList<Song>();
+ String resultStr = null;
+ try {
+ String request = baseURL + "songs/by_title/" + java.net.URLEncoder.encode(q, "UTF-8") + ".json";
+ println(request);
+ resultStr = join(loadStrings(request), "");
+ JSONArray result = new JSONArray(resultStr);
+ for (int i = 0; i < result.length(); i++){
+ JSONObject aj = result.getJSONObject(i);
+ JSONObject artj = aj.getJSONObject("artist");
+ songs.add(new Song(aj.getInt("id"), aj.getString("mbid"), aj.getString("title"),
+ new Artist(artj.getInt("id"), artj.getString("mbid"), artj.getString("name"))));
+ }
+ }
+ catch (UnsupportedEncodingException e) {
+ println("searchSongs");
+ println (e);
+ }
+ catch (JSONException e) {
+ println("searchSongs");
+ println (e);
+ println(resultStr);
+ }
+ return new SongList(songs);
+ }
+ });
+ }
}
class mbArtist{
Please sign in to comment.
Something went wrong with that request. Please try again.