summaryrefslogtreecommitdiffstats
path: root/src/de/animux/android/andmal/api/manga/MangaList.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/animux/android/andmal/api/manga/MangaList.java')
-rw-r--r--src/de/animux/android/andmal/api/manga/MangaList.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/de/animux/android/andmal/api/manga/MangaList.java b/src/de/animux/android/andmal/api/manga/MangaList.java
new file mode 100644
index 0000000..00a89e4
--- /dev/null
+++ b/src/de/animux/android/andmal/api/manga/MangaList.java
@@ -0,0 +1,86 @@
+package de.animux.android.andmal.api.manga;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+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.util.SortedLinkedList;
+
+public class MangaList extends MalList implements MalObjectStore<Manga> {
+
+ // cache last instance of this class to restore if a new one is created with
+ // the same username and password
+ private static MangaList cache;
+ private Map<MalState, Collection<MalObject>> mangas;
+
+ public MangaList(String username, String password) {
+ super(username, password);
+
+ if (((cache != null) && cache.getUsername().equals(username))
+ && cache.getPassword().equals(username)) {
+ mangas = cache.getObjects();
+ } else {
+ cache = this;
+ needsRefresh = true;
+ }
+ }
+
+ public MangaList() {
+ super();
+
+ if (cache != null && cache.getUsername() != null
+ && cache.getUsername().equals(this.getUsername())) {
+ mangas = cache.getObjects();
+ } else {
+ cache = this;
+ needsRefresh = true;
+ }
+ }
+
+ @Override
+ public void refresh() {
+ if (this.getUsername() == null)
+ return;
+
+ mangas = new HashMap<MalState, Collection<MalObject>>();
+ mangas.put(State.READING, new SortedLinkedList<MalObject>());
+ mangas.put(State.COMPLETED, new SortedLinkedList<MalObject>());
+ mangas.put(State.ONHOLD, new SortedLinkedList<MalObject>());
+ mangas.put(State.DROPPED, new SortedLinkedList<MalObject>());
+ mangas.put(State.PLANTOREAD, new SortedLinkedList<MalObject>());
+
+ try {
+ URL url = new URL(
+ "http://myanimelist.net/malappinfo.php?status=all&type=manga&u="
+ + this.getUsername());
+ InputStream stream = url.openStream();
+
+ SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
+ sax.parse(stream, new MangaListParser(this));
+ needsRefresh = false;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void add(Manga object) {
+ if (mangas.containsKey(State.valueOf(object.getPersonalStatus()))) {
+ mangas.get(State.valueOf(object.getPersonalStatus())).add(object);
+ }
+ }
+
+ @Override
+ public Map<MalState, Collection<MalObject>> getObjects() {
+ return mangas;
+ }
+}