From 6a5cb0d5b73c99ea5c152f268b4d39d9140beca0 Mon Sep 17 00:00:00 2001 From: Defend Date: Mon, 13 Feb 2017 22:16:01 +0300 Subject: [PATCH] Updated addNewsItem - added title, text and link fields on that method! FeedController and FeedReader rewrited using callbacks! --- .../defdevteam/tstu/CabinetActivity.java | 25 +++++++++---------- .../defdevteam/tstu/FeedController.java | 14 ++++++++--- .../ru/defend/defdevteam/tstu/FeedParser.java | 4 +-- .../ru/defend/defdevteam/tstu/FeedReader.java | 16 ++++++++---- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java b/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java index 86854ee..934e142 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java +++ b/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) { diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java index 991eeee..6d53c26 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java +++ b/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! + } + + + } diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java index 5503293..f4945c9 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java +++ b/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(); diff --git a/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java b/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java index 77b9ef7..67aea1f 100644 --- a/app/src/main/java/ru/defend/defdevteam/tstu/FeedReader.java +++ b/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(); } }