Search in sources :

Example 6 with News

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();
    }
}
Also used : News(de.tum.in.tumcampusapp.component.ui.news.model.News) FilmCard(de.tum.in.tumcampusapp.component.ui.tufilm.FilmCard)

Example 7 with News

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);
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) News(de.tum.in.tumcampusapp.component.ui.news.model.News) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) JSONArray(org.json.JSONArray)

Example 8 with News

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");
}
Also used : News(de.tum.in.tumcampusapp.component.ui.news.model.News) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

News (de.tum.in.tumcampusapp.component.ui.news.model.News)8 DateTime (org.joda.time.DateTime)3 Test (org.junit.Test)3 NewsSources (de.tum.in.tumcampusapp.component.ui.news.model.NewsSources)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 AccessTokenManager (de.tum.in.tumcampusapp.api.tumonline.AccessTokenManager)1 TUMOnlineRequest (de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest)1 OrgItemList (de.tum.in.tumcampusapp.component.other.departments.model.OrgItemList)1 TuitionList (de.tum.in.tumcampusapp.component.tumui.tutionfees.model.TuitionList)1 NewsController (de.tum.in.tumcampusapp.component.ui.news.NewsController)1 FilmCard (de.tum.in.tumcampusapp.component.ui.tufilm.FilmCard)1 Kino (de.tum.in.tumcampusapp.component.ui.tufilm.model.Kino)1 NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)1 SyncManager (de.tum.in.tumcampusapp.utils.sync.SyncManager)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1