use of de.tum.in.tumcampusapp.component.ui.news.model.News in project TumCampusApp by TCA-Team.
the class NewsController method onRequestCard.
/**
* Adds the newest news card
*
* @param context Context
*/
@Override
public void onRequestCard(Context context) {
Collection<Integer> sources = getActiveSources(context);
List<News> news;
if (Utils.getSettingBool(context, "card_news_latest_only", true)) {
news = newsDao.getBySourcesLatest(sources.toArray(new Integer[sources.size()]));
} else {
news = newsDao.getBySources(sources.toArray(new Integer[sources.size()]));
}
// Display resulting cards
for (News n : news) {
NewsCard card;
if (n.isFilm()) {
card = new FilmCard(context);
} else {
card = new NewsCard(context);
}
card.setNews(n);
card.apply();
}
}
use of de.tum.in.tumcampusapp.component.ui.news.model.News in project TumCampusApp by TCA-Team.
the class NewsController method downloadFromExternal.
/**
* Download news from external interface (JSON)
*
* @param force True to force download over normal sync period, else false
* @throws JSONException parsing could fail
*/
public void downloadFromExternal(boolean force) throws JSONException {
SyncManager sync = new SyncManager(mContext);
if (!force && !sync.needSync(this, TIME_TO_SYNC)) {
return;
}
NetUtils net = new NetUtils(mContext);
// Load all news sources
Optional<JSONArray> jsonArray = net.downloadJsonArray(NEWS_SOURCES_URL, CacheManager.VALIDITY_ONE_MONTH, force);
if (jsonArray.isPresent()) {
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsSourcesDao.insert(new NewsSources(obj.getInt(Const.JSON_SOURCE), obj.getString(Const.JSON_TITLE), obj.has(Const.JSON_ICON) ? obj.getString(Const.JSON_ICON) : ""));
}
}
// Load all news since the last sync
jsonArray = net.downloadJsonArray(NEWS_URL + getLastId(), CacheManager.VALIDITY_ONE_DAY, force);
// Delete all too old items
cleanupDb();
if (!jsonArray.isPresent()) {
return;
}
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsDao.insert(new News(obj.getString(Const.JSON_NEWS), obj.getString(Const.JSON_TITLE), obj.getString(Const.JSON_LINK), obj.getString(Const.JSON_SRC), obj.getString(Const.JSON_IMAGE), DateUtils.getDateTime(obj.getString(Const.JSON_DATE)), DateUtils.getDateTime(obj.getString(Const.JSON_CREATED)), 0));
}
sync.replaceIntoDb(this);
}
use of de.tum.in.tumcampusapp.component.ui.news.model.News in project TumCampusApp by TCA-Team.
the class NewsDaoTest method getBySourcesLatestSomeTest.
/**
* There are several items per source
* Expected output: Some items are retrieved
*/
@Test
public void getBySourcesLatestSomeTest() {
DateTime now = DateTime.now();
dao.insert(createNewsItem("123", now.minusDays(1).toDate()));
dao.insert(createNewsItem("123", now.minusMonths(1).toDate()));
dao.insert(createNewsItem("124", now.minusYears(1).toDate()));
dao.insert(createNewsItem("124", now.minusHours(100).toDate()));
List<News> news = dao.getBySourcesLatest(new Integer[] { 123, 124, 125, 126 });
assertThat(news).hasSize(2);
assertThat(news.get(0).getId()).isEqualTo("3");
assertThat(news.get(1).getId()).isEqualTo("0");
}
Aggregations