summaryrefslogblamecommitdiffstats
path: root/src/de/animux/android/andmal/api/anime/AnimeList.java
blob: 0fbb4eaa1ab045b1c8118f38e2bc5df0c02fbac3 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                           


                           
                            
                         

                     


                                          



                                                   


                                                      
                                                                         
 


                                                                                    
 
                                                            
 

                                                            
 






                                                                             

         

                            
 







                                                                                    


                 






















                                                                                                        


                 


                                                                                    
                 


                 

                                                                  

         
package de.animux.android.andmal.api.anime;

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.api.anime.State;
import de.animux.android.andmal.util.SortedLinkedList;

public class AnimeList extends MalList implements MalObjectStore<Anime> {

	// 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 Map<MalState, Collection<MalObject>> animes;

	public AnimeList(String username, String password) {
		super(username, password);

		if (((cache != null) && cache.getUsername().equals(username))
				&& cache.getPassword().equals(username)) {
			animes = cache.getObjects();
		} else {
			cache = this;
			needsRefresh = true;
		}
	}

	public AnimeList() {
		super();

		if (cache != null && cache.getUsername() != null
				&& cache.getUsername().equals(this.getUsername())) {
			animes = cache.getObjects();
		}
		else {
			cache = this;
			needsRefresh = true;
		}
	}

	@Override
	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 add(Anime object) {
		if (animes.containsKey(State.valueOf(object.getMyStatus()))) {
			animes.get(State.valueOf(object.getMyStatus())).add(object);
		}
	}

	@Override
	public Map<MalState, Collection<MalObject>> getObjects() {
		return animes;
	}
}