Browse Source

Added NewsItemList, testing FeedParser. I have found some issues after writing code in that module.

master
Defend 8 years ago
parent
commit
91aab3f63e
  1. 16
      app/app.iml
  2. 6
      app/src/main/java/ru/defend/defdevteam/tstu/CabinetActivity.java
  3. 55
      app/src/main/java/ru/defend/defdevteam/tstu/FeedController.java
  4. 32
      app/src/main/java/ru/defend/defdevteam/tstu/FeedParser.java
  5. 93
      app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java
  6. 5
      app/src/main/res/layout/cabinet_newsfeed.xml
  7. 4
      app/src/main/res/layout/cabinet_newsfeed_item.xml

16
app/app.iml

@ -66,14 +66,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@ -82,6 +74,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />

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

@ -180,9 +180,6 @@ public class CabinetActivity extends AppCompatActivity
AuthURL = "http://web-iais.admin.tstu.ru:7777/zion/f?p=stud_main";
loadSite(AuthURL);
addNewsItem("Hello guys!", "Its my first trying!", null);
addNewsItem("Hello guys!", "Its my second trying!", null);
}
@Override
@ -310,7 +307,6 @@ public class CabinetActivity extends AppCompatActivity
public void addNewsItem (String title, String text, String link) {
LayoutInflater inflater = getLayoutInflater();
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);
newsFeed.addView(layout);
@ -322,8 +318,6 @@ public class CabinetActivity extends AppCompatActivity
new DownloadImageTask((ImageView) layout.findViewById(R.id.newsfeed_item_image))
.execute(link);
newsFeed.addView(inflater.inflate(R.layout.cabinet_newsfeed_item, null));
}
public void loadSite (String url) {

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

@ -1,10 +1,12 @@
package ru.defend.defdevteam.tstu;
import android.util.Log;
/**
* Created by thedefend on 29.11.16.
*/
public class FeedController implements FeedReader.ReaderCallback{
public class FeedController implements FeedReader.ReaderCallback, FeedParser.ParserCallback{
FeedReader feedReader;
FeedUpdater feedUpdater;
@ -46,15 +48,15 @@ public class FeedController implements FeedReader.ReaderCallback{
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);
}
});
}
//
// public void feedUpdateAction(final String result) {
// activity.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// //activity.newsTextView.setText(result);
// }
// });
// }
private class FeedUpdater extends Thread {
@Override
@ -81,14 +83,45 @@ public class FeedController implements FeedReader.ReaderCallback{
throw new Exception("Feed Reader returned empty string");
}
public void onSuccess(String result){
@Override
public void onSuccess(final String result){
this.newsFeed = result;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.addNewsItem("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(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);
}
});
}
}
@Override
public void onFailureParsing() {
}
}

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

@ -3,6 +3,9 @@ package ru.defend.defdevteam.tstu;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -12,7 +15,27 @@ import javax.xml.parsers.DocumentBuilderFactory;
public class FeedParser {
ParserCallback callback;
NewsItemList newsItemList;
String inputXML;
interface ParserCallback {
void onSuccessParsing(NewsItemList newsItemList);
void onFailureParsing();
}
void registerCallback(ParserCallback callback) {
this.callback = callback;
}
FeedParser(String inputXML) {
newsItemList = new NewsItemList();
this.inputXML = inputXML;
}
void parseXML() {
NewsItemList.Group group = new NewsItemList().new Group(); // <---Remove that
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Создается дерево DOM документа из файла
@ -20,10 +43,17 @@ public class FeedParser {
// Получаем корневой элемент
Node root = document.getDocumentElement();
String exp = root.getTextContent(); // <---Remove that
group.set("Node",exp,""); // <---Remove that
newsItemList.add(group);// <---Remove that
} catch (Exception e) {
e.printStackTrace();
}
group.set("Node2","end",""); // <---Remove that
newsItemList.add(group);// <---Remove that
group.set("Node3","2end",""); // <---Remove that
newsItemList.add(group);// <---Remove that
callback.onSuccessParsing(newsItemList);
}
}

93
app/src/main/java/ru/defend/defdevteam/tstu/NewsItemList.java

@ -0,0 +1,93 @@
package ru.defend.defdevteam.tstu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* Created by itsmy on 13.02.2017.
*/
public class NewsItemList implements Iterable<NewsItemList.Group> {
private ArrayList<Group> list;
NewsItemList() {
list = new ArrayList<>();
}
public int size() {
return list.size();
}
public Group get(int index) {
return list.get(index);
}
public void add(Group group) {
this.list.add(group);
}
@Override
public Iterator<Group> iterator() {
return new NewsIterator();
}
private class NewsIterator implements Iterator<Group> {
private int cursor;
public NewsIterator() {
this.cursor = 0;
}
public boolean hasNext() {
return this.cursor < size();
}
public Group next() {
if(this.hasNext()) {
Group current = get(cursor);
cursor ++;
return current;
}
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public class Group {
private String title;
private String text;
private String link;
Group() {
}
public void set(String title, String text, String link) {
this.title = title;
this.text = text;
this.link = link;
}
public String getTitle() {
return this.title;
}
public String getText() {
return this.text;
}
public String getLink() {
return this.link;
}
}
}

5
app/src/main/res/layout/cabinet_newsfeed.xml

@ -16,11 +16,6 @@
android:layout_height="wrap_content"
android:id="@+id/newsfeed_scroll">
<include layout="@layout/cabinet_newsfeed_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/newsfeed_item_1"/>
</LinearLayout>
</ScrollView>

4
app/src/main/res/layout/cabinet_newsfeed_item.xml

@ -19,7 +19,7 @@
android:paddingTop="10dp">
<TextView
android:text="Lorem Ipsum"
android:text=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
@ -32,7 +32,7 @@
android:id="@+id/newsfeed_item_image"/>
<TextView
android:text="TEXT"
android:text=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"

Loading…
Cancel
Save