Browse Source

Updated addNewsItem - added title, text and link fields on that method! FeedController and FeedReader rewrited using callbacks!

master
Defend 8 years ago
parent
commit
6a5cb0d5b7
  1. 25
      app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java
  2. 14
      app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java
  3. 4
      app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java
  4. 16
      app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java

25
app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java

@ -180,8 +180,8 @@ public class CabinetActivity extends AppCompatActivity
AuthURL = "http://web-iais.admin.tstu.ru:7777/zion/f?p=stud_main";
loadSite(AuthURL);
addNewsItem(null);
addNewsItem(null);
addNewsItem("Hello guys!", "Its my first trying!", null);
addNewsItem("Hello guys!", "Its my second trying!", null);
}
@ -307,24 +307,23 @@ public class CabinetActivity extends AppCompatActivity
}
}
public void addNewsItem (View v) {
//Это всего лишь тестовая функция
LinearLayout mainLayer = (LinearLayout) findViewById(R.id.newsfeed_scroll);
public void addNewsItem (String title, String text, String link) {
LayoutInflater inflater = getLayoutInflater();
mainLayer.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null));
LinearLayout newsFeed = (LinearLayout) findViewById(R.id.newsfeed_scroll);
newsFeed.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null));
View layout = inflater.inflate(R.layout.cabinet_newsfeed_item, null);
mainLayer.addView(layout);
newsFeed.addView(layout);
TextView text = (TextView) layout.findViewById(R.id.newsfeed_item_title);
text.setText("В ТГТУ прошел региональный этап Всероссийской олимпиады школьников по информатике и ИКТ");
TextView titleTextView = (TextView) layout.findViewById(R.id.newsfeed_item_title);
titleTextView.setText(title);
TextView text2 = (TextView) layout.findViewById(R.id.newsfeed_item_text);
TextView textView = (TextView) layout.findViewById(R.id.newsfeed_item_text);
textView.setText(text);
new DownloadImageTask((ImageView) layout.findViewById(R.id.newsfeed_item_image))
.execute("http://press.tstu.ru/photo/News/2017/0000494857574.jpg");
.execute(link);
mainLayer.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null));
newsFeed.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null));
}
public void loadSite (String url) {

14
app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java

@ -4,7 +4,7 @@ package ru.defend.defdevteam.tstu;
* Created by thedefend on 29.11.16.
*/
public class FeedController {
public class FeedController implements FeedReader.ReaderCallback{
FeedReader feedReader;
FeedUpdater feedUpdater;
@ -18,6 +18,7 @@ public class FeedController {
public FeedController(CabinetActivity activity, int updateRate, String rssUrl) {
feedReader = new FeedReader(this, rssUrl);
feedReader.registerCallback(this);
feedUpdater = new FeedUpdater();
this.activity = activity;
this.updateRate = updateRate;
@ -80,7 +81,14 @@ public class FeedController {
throw new Exception("Feed Reader returned empty string");
}
interface FeedCallback{
void Callback();
public void onSuccess(String result){
this.newsFeed = result;
}
public void onFailure(){
//Ill add this code later!
}
}

4
app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java

@ -12,11 +12,11 @@ import javax.xml.parsers.DocumentBuilderFactory;
public class FeedParser {
FeedParser(String omg) {
FeedParser(String inputXML) {
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Создается дерево DOM документа из файла
Document document = documentBuilder.parse(omg);
Document document = documentBuilder.parse(inputXML);
// Получаем корневой элемент
Node root = document.getDocumentElement();

16
app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java

@ -10,7 +10,7 @@ import java.net.URL;
* Created by thedefend on 28.11.16.
*/
public class FeedReader extends Thread implements FeedController.FeedCallback {
class FeedReader extends Thread {
private String link;
private String result;
@ -22,6 +22,7 @@ public class FeedReader extends Thread implements FeedController.FeedCallback {
THREAD_COMPLETE
}
private States threadState;
ReaderCallback callback;
FeedReader (FeedController feedController, String link) {
this.link = link;
@ -29,16 +30,20 @@ public class FeedReader extends Thread implements FeedController.FeedCallback {
this.threadState = States.THREAD_NULL;
}
void registerCallback(ReaderCallback callback) {
this.callback = callback;
}
public void run(){
try {
this.threadState = States.THREAD_WORK;
this.result = getRss(link);
this.threadState = States.THREAD_COMPLETE;
//TODO: слишком сильная взаимосвязь с управляющим классом
feedController.feedUpdateAction(result);
callback.onSuccess(result);
} catch (Exception e) {
this.threadState = States.THREAD_FAIL;
e.printStackTrace();
callback.onFailure();
}
}
@ -68,7 +73,8 @@ public class FeedReader extends Thread implements FeedController.FeedCallback {
return this.result;
}
public void Callback(){
interface ReaderCallback{
void onSuccess(String rssFeed);
void onFailure();
}
}

Loading…
Cancel
Save