summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/util/HeadlinesRequest.java
blob: 453f5f5011454eb796fc21a95768ce0d7518470d (plain)
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
package org.fox.ttrss.util;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.HeadlinesFragment;
import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed;

import java.lang.reflect.Type;
import java.util.List;

public class HeadlinesRequest extends ApiRequest {
	public static final int HEADLINES_REQUEST_SIZE = 30;
	public static final int HEADLINES_BUFFER_MAX = 1500;

	private final String TAG = this.getClass().getSimpleName();
	
	private int m_offset = 0;
	private OnlineActivity m_activity;
	private ArticleList m_articles; // = new ArticleList(); //Application.getInstance().m_loadedArticles;
	private Feed m_feed;
	
	public HeadlinesRequest(Context context, OnlineActivity activity, final Feed feed, ArticleList articles) {
		super(context);

        m_articles = articles;
		m_activity = activity;
		m_feed = feed;
	}
	
	protected void onPostExecute(JsonElement result) {
		if (result != null) {
			try {
				
				// check if we are returning results for correct feed
				/* if (Application.getInstance().m_activeFeed != null && !m_feed.equals(Application.getInstance().m_activeFeed)) {
					Log.d(TAG, "received results for wrong feed, bailing out.");
					return;
				} */
				
				JsonArray content = result.getAsJsonArray();
				if (content != null) {
					Type listType = new TypeToken<List<Article>>() {}.getType();
					final List<Article> articles = new Gson().fromJson(content, listType);
					
					if (m_offset == 0) {
						m_articles.clear();
					} else {
						while (m_articles.size() > HEADLINES_BUFFER_MAX) {
							m_articles.remove(0);
						}

						if (m_articles.get(m_articles.size()-1).id == HeadlinesFragment.ARTICLE_SPECIAL_LOADMORE) {
							m_articles.remove(m_articles.size()-1); // remove previous placeholder
						}
						
					}
					
					for (Article f : articles)
						if (!m_articles.containsId(f.id))
							m_articles.add(f);

					if (articles.size() == HEADLINES_REQUEST_SIZE) {
						Article placeholder = new Article(HeadlinesFragment.ARTICLE_SPECIAL_LOADMORE);
						m_articles.add(placeholder);
					}

					/* if (m_articles.size() == 0)
						m_activity.setLoadingStatus(R.string.no_headlines_to_display, false);
					else */
					
					//m_activity.setLoadingStatus(R.string.blank, false);
					
					return;
				}
						
			} catch (Exception e) {
				e.printStackTrace();						
			}
		}

		if (m_lastError == ApiError.LOGIN_FAILED) {
			m_activity.login();
		} else {
            m_activity.toast(getErrorMessage());
			//m_activity.setLoadingStatus(getErrorMessage(), false);
		}
    }

	public void setOffset(int skip) {
		m_offset = skip;			
	}
}