summaryrefslogtreecommitdiffstats
path: root/src/de/animux/android/andmal/AnimeListActivity.java
blob: 566fa1d492b8b3ceccbdcd27b2203442ec5a0370 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package de.animux.android.andmal;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import de.animux.android.andmal.api.anime.Anime;
import de.animux.android.andmal.api.anime.AnimeList;
import de.animux.android.andmal.api.anime.State;
import de.animux.android.andmal.util.SeparatedListAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AnimeListActivity extends Activity {

	public final static String ITEM_TITLE = "title";
	public final static String ITEM_CAPTION = "caption";

	public final static int INITIAL_SYNC = 0;

	private AnimeList animeList;

	private UpdateUIHandler updateUIHandler = new UpdateUIHandler();

	public Map<String, ?> createItem(String title, String caption) {
		Map<String, String> item = new HashMap<String, String>();
		item.put(ITEM_TITLE, title);
		item.put(ITEM_CAPTION, caption);
		return item;
	}

	private class UpdateUIHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			SeparatedListAdapter adapter = new SeparatedListAdapter(
					AnimeListActivity.this);
			Map<de.animux.android.andmal.api.anime.State, List<Anime>> animes = animeList
					.getAnimes();

			for (State s : State.values())
				addAnimes(adapter, animes.get(s), s.toString());

			ListView list = new ListView(AnimeListActivity.this);
			list.setAdapter(adapter);

			dismissDialog(INITIAL_SYNC);

			AnimeListActivity.this.setContentView(list);
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		showDialog(INITIAL_SYNC);

		new Thread() {
			public void run() {
				try {
					animeList = new AnimeList("AlexanderS");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				updateUIHandler.sendEmptyMessage(0);
			}
		}.start();
	}

	private void addAnimes(SeparatedListAdapter adapter,
			Collection<Anime> animes, String sectionName) {
		if (animes != null && animes.size() > 0) {
			java.util.List<Map<String, ?>> listSection = new LinkedList<Map<String, ?>>();

			for (Anime a : animes) {
				listSection.add(createItem(a.getTitle(), a.getWatchedEpisodes()
						+ "/" + a.getEpisodes()));
			}

			adapter.addSection(sectionName, new SimpleAdapter(this,
					listSection, R.layout.list_item, new String[] { ITEM_TITLE,
							ITEM_CAPTION }, new int[] { R.id.list_item_title,
							R.id.list_item_other }));
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case INITIAL_SYNC:
			ProgressDialog dialog = ProgressDialog.show(this, "",
					"Loading list. Please wait...", true);
			dialog.show();
			return dialog;
		default:
			return super.onCreateDialog(id);
		}
	}

}