diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2010-08-25 08:03:05 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2010-08-25 08:03:05 +0200 |
commit | bf24af1ed9808dc918dc81f0383103b6c51600a6 (patch) | |
tree | d899dae5a08c94b5184a558c16c9f743aee0c3c7 /src/de/animux/android/andmal/api/anime | |
parent | 8a8dc0a2ac4f3552568d9e14bf5ae121394b055c (diff) | |
download | AndMAL-master.tar.gz AndMAL-master.tar.xz AndMAL-master.zip |
refactoring, mangalist, settings (current problem: refresh other list after changing the settings)HEADmaster
Diffstat (limited to 'src/de/animux/android/andmal/api/anime')
4 files changed, 122 insertions, 90 deletions
diff --git a/src/de/animux/android/andmal/api/anime/Anime.java b/src/de/animux/android/andmal/api/anime/Anime.java index d4f542f..dc4b34e 100644 --- a/src/de/animux/android/andmal/api/anime/Anime.java +++ b/src/de/animux/android/andmal/api/anime/Anime.java @@ -1,6 +1,8 @@ package de.animux.android.andmal.api.anime; -public class Anime implements Comparable<Anime> { +import de.animux.android.andmal.api.MalObject; + +public class Anime extends MalObject { private int id; private String title; @@ -180,18 +182,28 @@ public class Anime implements Comparable<Anime> { } @Override - public int compareTo(Anime arg0) { - int compareTitle = arg0.getTitle().compareTo(getTitle()); + public int compareTo(MalObject arg0) { + if ( !(arg0 instanceof Anime)) { + throw new IllegalArgumentException(); + } + + Anime a = (Anime)arg0; + int compareTitle = a.getTitle().compareTo(getTitle()); if (compareTitle != 0) { return compareTitle; } else { - if (arg0.getId() != getId()) { - return getId() - arg0.getId(); + if (a.getId() != getId()) { + return getId() - a.getId(); } } return 0; } + @Override + public String getProgress() { + return getWatchedEpisodes() + "/" + getEpisodes(); + } + } diff --git a/src/de/animux/android/andmal/api/anime/AnimeList.java b/src/de/animux/android/andmal/api/anime/AnimeList.java index 78a1e09..0fbb4ea 100644 --- a/src/de/animux/android/andmal/api/anime/AnimeList.java +++ b/src/de/animux/android/andmal/api/anime/AnimeList.java @@ -1,117 +1,90 @@ package de.animux.android.andmal.api.anime; -import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Collection; import java.util.HashMap; -import java.util.List; import java.util.Map; -import javax.xml.parsers.FactoryConfigurationError; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - +import de.animux.android.andmal.api.MalList; +import de.animux.android.andmal.api.MalObject; +import de.animux.android.andmal.api.MalObjectStore; +import de.animux.android.andmal.api.MalState; import de.animux.android.andmal.api.anime.State; import de.animux.android.andmal.util.SortedLinkedList; -public class AnimeList extends DefaultHandler { +public class AnimeList extends MalList implements MalObjectStore<Anime> { - private Anime currentAnime; - private StringBuffer currentValue; + // cache last instance of this class to restore if a new one is created with + // the same username and password + private static AnimeList cache; - private HashMap<State,List<Anime>> animes; + private Map<MalState, Collection<MalObject>> animes; - public AnimeList(String user) throws IOException, - ParserConfigurationException, SAXException, - FactoryConfigurationError { - currentAnime = null; - currentValue = new StringBuffer(); - - animes = new HashMap<State, List<Anime>>(); - animes.put(State.WATCHING, new SortedLinkedList<Anime>()); - animes.put(State.COMPLETED, new SortedLinkedList<Anime>()); - animes.put(State.ONHOLD, new SortedLinkedList<Anime>()); - animes.put(State.DROPPED, new SortedLinkedList<Anime>()); - animes.put(State.PLANTOWATCH, new SortedLinkedList<Anime>()); - - URL url = new URL("http://myanimelist.net/malappinfo.php?status=all&type=anime&u=" + user); - InputStream stream = url.openStream(); - - SAXParser sax = SAXParserFactory.newInstance().newSAXParser(); - sax.parse(stream, this); - } + public AnimeList(String username, String password) { + super(username, password); - public Anime getCurrentAnime() { - return currentAnime; + if (((cache != null) && cache.getUsername().equals(username)) + && cache.getPassword().equals(username)) { + animes = cache.getObjects(); + } else { + cache = this; + needsRefresh = true; + } } - public StringBuffer getCurrentValue() { - return currentValue; - } + public AnimeList() { + super(); - public Map<State, List<Anime>> getAnimes() { - return animes; + if (cache != null && cache.getUsername() != null + && cache.getUsername().equals(this.getUsername())) { + animes = cache.getObjects(); + } + else { + cache = this; + needsRefresh = true; + } } @Override - public void characters(char[] ch, int start, int length) - throws SAXException { - currentValue.append(new String(ch).substring(start, length - start) - .replaceAll("&", "&")); + public void refresh() { + if (this.getUsername() == null) + return; + + animes = new HashMap<MalState, Collection<MalObject>>(); + animes.put(State.WATCHING, new SortedLinkedList<MalObject>()); + animes.put(State.COMPLETED, new SortedLinkedList<MalObject>()); + animes.put(State.ONHOLD, new SortedLinkedList<MalObject>()); + animes.put(State.DROPPED, new SortedLinkedList<MalObject>()); + animes.put(State.PLANTOWATCH, new SortedLinkedList<MalObject>()); + + try { + URL url = new URL( + "http://myanimelist.net/malappinfo.php?status=all&type=anime&u=" + + this.getUsername()); + InputStream stream = url.openStream(); + + SAXParser sax = SAXParserFactory.newInstance().newSAXParser(); + sax.parse(stream, new AnimeListParser(this)); + needsRefresh = false; + } catch (Exception e) { + e.printStackTrace(); + } } @Override - public void endElement(String uri, String localName, String qName) - throws SAXException { - - if (localName.equals("anime")) { - if (animes.containsKey(State.valueOf(currentAnime.getMyStatus()))) { - animes.get(State.valueOf(currentAnime.getMyStatus())).add(currentAnime); - } - - } else if (localName.equals("series_animedb_id")) { - currentAnime.setId(Integer.valueOf(currentValue.toString())); - } else if (localName.equals("series_title")) { - currentAnime.setTitle(currentValue.toString()); - } else if (localName.equals("series_synonyms")) { - currentAnime.setSynonyms(currentValue.toString()); - } else if (localName.equals("series_type")) { - currentAnime.setType(Integer.valueOf(currentValue.toString())); - } else if (localName.equals("series_episodes")) { - currentAnime.setEpisodes(Integer.valueOf(currentValue.toString())); - } else if (localName.equals("series_status")) { - currentAnime.setStatus(Integer.valueOf(currentValue.toString())); - } else if (localName.equals("series_start")) { - currentAnime.setStart(currentValue.toString()); - } else if (localName.equals("series_end")) { - currentAnime.setEnd(currentValue.toString()); - } else if (localName.equals("series_image")) { - currentAnime.setImage(currentValue.toString()); - } else if (localName.equals("my_id")) { - currentAnime.setMyId(Integer.valueOf(currentValue.toString())); - } else if (localName.equals("my_watched_episodes")) { - currentAnime.setWatchedEpisodes(Integer.valueOf(currentValue - .toString())); - } else if (localName.equals("my_status")) { - currentAnime.setMyStatus(Integer.valueOf(currentValue.toString())); + public void add(Anime object) { + if (animes.containsKey(State.valueOf(object.getMyStatus()))) { + animes.get(State.valueOf(object.getMyStatus())).add(object); } - - currentValue.setLength(0); } @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - - currentValue.setLength(0); - if (localName.equals("anime")) { - currentAnime = new Anime(); - } + public Map<MalState, Collection<MalObject>> getObjects() { + return animes; } } diff --git a/src/de/animux/android/andmal/api/anime/AnimeListParser.java b/src/de/animux/android/andmal/api/anime/AnimeListParser.java new file mode 100644 index 0000000..e7545dd --- /dev/null +++ b/src/de/animux/android/andmal/api/anime/AnimeListParser.java @@ -0,0 +1,45 @@ +package de.animux.android.andmal.api.anime; + +import de.animux.android.andmal.api.MalListParser; +import de.animux.android.andmal.api.MalObjectStore; + +public class AnimeListParser extends MalListParser<Anime> { + + private Anime currentAnime; + + public AnimeListParser(MalObjectStore<Anime> objectStore) { + super(objectStore); + currentAnime = new Anime(); + } + + public void addElement(String name, String value) { + if (name.equals("anime")) { + objectStore.add(currentAnime); + currentAnime = new Anime(); + } else if (name.equals("series_animedb_id")) { + currentAnime.setId(Integer.valueOf(value)); + } else if (name.equals("series_title")) { + currentAnime.setTitle(value); + } else if (name.equals("series_synonyms")) { + currentAnime.setSynonyms(value); + } else if (name.equals("series_type")) { + currentAnime.setType(Integer.valueOf(value)); + } else if (name.equals("series_episodes")) { + currentAnime.setEpisodes(Integer.valueOf(value)); + } else if (name.equals("series_status")) { + currentAnime.setStatus(Integer.valueOf(value)); + } else if (name.equals("series_start")) { + currentAnime.setStart(value); + } else if (name.equals("series_end")) { + currentAnime.setEnd(value); + } else if (name.equals("series_image")) { + currentAnime.setImage(value); + } else if (name.equals("my_id")) { + currentAnime.setMyId(Integer.valueOf(value)); + } else if (name.equals("my_watched_episodes")) { + currentAnime.setWatchedEpisodes(Integer.valueOf(value)); + } else if (name.equals("my_status")) { + currentAnime.setMyStatus(Integer.valueOf(value)); + } + } +} diff --git a/src/de/animux/android/andmal/api/anime/State.java b/src/de/animux/android/andmal/api/anime/State.java index 639b761..9ba4769 100644 --- a/src/de/animux/android/andmal/api/anime/State.java +++ b/src/de/animux/android/andmal/api/anime/State.java @@ -1,6 +1,8 @@ package de.animux.android.andmal.api.anime; -public enum State { +import de.animux.android.andmal.api.MalState; + +public enum State implements MalState { WATCHING (1, "Watching"), COMPLETED (2, "Completed"), ONHOLD (3, "On Hold"), |