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);
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations