You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.9 KiB
146 lines
3.9 KiB
package ru.defend.defdevteam.tstu;
|
|
|
|
import android.util.Log;
|
|
|
|
/**
|
|
* Created by thedefend on 29.11.16.
|
|
*/
|
|
|
|
public class FeedController implements FeedReader.ReaderCallback, FeedParser.ParserCallback{
|
|
|
|
FeedReader feedReader;
|
|
FeedUpdater feedUpdater;
|
|
String newsFeed;
|
|
NewsItemList newsItemList;
|
|
int updateRate; //in minutes
|
|
private CabinetActivity activity;
|
|
|
|
public FeedController(CabinetActivity activity, int updateRate) {
|
|
this(activity, updateRate, "http://press.tstu.ru/?format=feed");
|
|
}
|
|
|
|
public FeedController(CabinetActivity activity, int updateRate, String rssUrl) {
|
|
feedReader = new FeedReader(this, rssUrl);
|
|
feedReader.registerCallback(this);
|
|
newsItemList = new NewsItemList();
|
|
feedUpdater = new FeedUpdater();
|
|
this.activity = activity;
|
|
this.updateRate = updateRate;
|
|
feedUpdater.start();
|
|
}
|
|
|
|
private void startFeedReader() {
|
|
switch (feedReader.getThreadState()) {
|
|
case THREAD_FAIL:
|
|
feedReader.start();
|
|
break;
|
|
case THREAD_NULL:
|
|
feedReader.start();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private boolean checkFeedReader() throws Exception{
|
|
switch (feedReader.getThreadState()) {
|
|
case THREAD_COMPLETE:
|
|
return true;
|
|
case THREAD_NULL:
|
|
return false;
|
|
default:
|
|
throw new Exception("Failed to get rss source string from FeedReader class");
|
|
}
|
|
}
|
|
//
|
|
// public void feedUpdateAction(final String result) {
|
|
// activity.runOnUiThread(new Runnable() {
|
|
// @Override
|
|
// public void run() {
|
|
// //activity.newsTextView.setText(result);
|
|
// }
|
|
// });
|
|
// }
|
|
|
|
private class FeedUpdater extends Thread {
|
|
@Override
|
|
public void run() {
|
|
startFeedReader();
|
|
try {
|
|
//TODO: вернуть к норме updateRate*60*1000
|
|
sleep(updateRate*60*1000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public FeedUpdater() {
|
|
//this.start();
|
|
}
|
|
}
|
|
|
|
// public String getRssString() throws Exception{
|
|
// if(checkFeedReader()) {
|
|
// newsFeed = feedReader.getResult();
|
|
// return newsFeed;
|
|
// }
|
|
// throw new Exception("Feed Reader returned empty string");
|
|
// }
|
|
|
|
public void addNewsList(String title, String text, String link) {
|
|
newsItemList.add(new NewsItemList().new Group(
|
|
title,
|
|
text,
|
|
link
|
|
));
|
|
}
|
|
|
|
public void drawNewsList(NewsItemList newsItemList) {
|
|
for (NewsItemList.Group item: newsItemList) {
|
|
final String title = item.getTitle();
|
|
final String text = item.getText();
|
|
final String link = item.getLink();
|
|
activity.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
activity.addNewsItem(title, text, link);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public void logIt(final String LOGIT) {
|
|
activity.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Log.i("MAIN DEBUG MESSAGE:", LOGIT);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onSuccess(final String result){
|
|
this.newsFeed = result;
|
|
addNewsList("Callback Reader Test", result, "");
|
|
FeedParser parser = new FeedParser(result);
|
|
parser.registerCallback(this);
|
|
parser.parseXML();
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(){
|
|
//Ill add this code later!
|
|
}
|
|
|
|
@Override
|
|
public void onSuccessParsing() {
|
|
drawNewsList(this.newsItemList);
|
|
}
|
|
|
|
@Override
|
|
public void onFailureParsing() {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|