Search in sources :

Example 6 with NewsSources

use of de.tum.in.tumcampusapp.component.ui.news.model.NewsSources 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 7 with NewsSources

use of de.tum.in.tumcampusapp.component.ui.news.model.NewsSources in project TumCampusApp by TCA-Team.

the class NewsActivity method onClick.

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    List<NewsSources> newsSources = nm.getNewsSources();
    if (which < newsSources.size()) {
        Utils.setSetting(this, "news_source_" + newsSources.get(which).getId(), isChecked);
        if (lv != null) {
            // We really don't care if the lv is null, if the position can't be saved. Rather not have the app crash here
            LinearLayoutManager layoutManager = (LinearLayoutManager) lv.getLayoutManager();
            state = layoutManager.findFirstVisibleItemPosition();
        }
        requestDownload(false);
    }
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Example 8 with NewsSources

use of de.tum.in.tumcampusapp.component.ui.news.model.NewsSources in project TumCampusApp by TCA-Team.

the class NewsAdapter method bindNewsView.

public static void bindNewsView(NetUtils net, RecyclerView.ViewHolder newsViewHolder, News news, Context context) {
    NewsViewHolder holder = (NewsViewHolder) newsViewHolder;
    NewsSourcesDao newsSourcesDao = TcaDb.getInstance(context).newsSourcesDao();
    NewsSources newsSource = newsSourcesDao.getNewsSource(Integer.parseInt(news.getSrc()));
    // Set image
    String imgUrl = news.getImage();
    if (imgUrl == null || imgUrl.isEmpty() || imgUrl.equals("null")) {
        holder.img.setVisibility(View.GONE);
    } else {
        holder.img.setVisibility(View.VISIBLE);
        net.loadAndSetImage(imgUrl, holder.img);
    }
    String title = news.getTitle();
    if (news.isFilm()) {
        title = COMPILE.matcher(title).replaceAll("");
    }
    holder.title.setText(title);
    // Adds date
    Date date = news.getDate();
    DateFormat sdf = DateFormat.getDateInstance();
    holder.srcDate.setText(sdf.format(date));
    holder.srcTitle.setText(newsSource.getTitle());
    String icon = newsSource.getIcon();
    if (icon.isEmpty() || "null".equals(icon)) {
        holder.srcIcon.setImageResource(R.drawable.ic_comment);
    } else {
        net.loadAndSetImage(icon, holder.srcIcon);
    }
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) DateFormat(java.text.DateFormat) Date(java.util.Date)

Example 9 with NewsSources

use of de.tum.in.tumcampusapp.component.ui.news.model.NewsSources in project TumCampusApp by TCA-Team.

the class NewsController method getActiveSources.

/**
 * Gather all sources that should be displayed
 *
 * @param context
 * @return
 */
private Collection<Integer> getActiveSources(Context context) {
    Collection<Integer> sources = new ArrayList<>();
    List<NewsSources> newsSources = getNewsSources();
    for (NewsSources newsSource : newsSources) {
        Integer id = newsSource.getId();
        if (Utils.getSettingBool(context, "card_news_source_" + id, true)) {
            sources.add(id);
        }
    }
    return sources;
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) ArrayList(java.util.ArrayList)

Example 10 with NewsSources

use of de.tum.in.tumcampusapp.component.ui.news.model.NewsSources in project TumCampusApp by TCA-Team.

the class NewsController method getAllFromDb.

/**
 * Get all news from the database
 *
 * @return List of News
 */
public List<News> getAllFromDb(Context context) {
    int selectedNewspread = Integer.parseInt(Utils.getSetting(mContext, "news_newspread", "7"));
    List<NewsSources> newsSources = getNewsSources();
    Collection<Integer> newsSourceIds = new ArrayList<>();
    for (NewsSources newsSource : newsSources) {
        int id = newsSource.getId();
        boolean show = Utils.getSettingBool(context, "news_source_" + id, id <= 7);
        if (show) {
            newsSourceIds.add(id);
        }
    }
    return newsDao.getAll(newsSourceIds.toArray(new Integer[newsSourceIds.size()]), selectedNewspread);
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) ArrayList(java.util.ArrayList)

Aggregations

NewsSources (de.tum.in.tumcampusapp.component.ui.news.model.NewsSources)14 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 NewsController (de.tum.in.tumcampusapp.component.ui.news.NewsController)2 News (de.tum.in.tumcampusapp.component.ui.news.model.News)2 NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)2 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 AlertDialog (android.support.v7.app.AlertDialog)1 CheckBoxPreference (android.support.v7.preference.CheckBoxPreference)1 PreferenceCategory (android.support.v7.preference.PreferenceCategory)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 Optional (com.google.common.base.Optional)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 Kino (de.tum.in.tumcampusapp.component.ui.tufilm.model.Kino)1 SyncManager (de.tum.in.tumcampusapp.utils.sync.SyncManager)1 DateFormat (java.text.DateFormat)1