Skip to content

Commit b0d480f

Browse files
committed
fix for duplicated items
1 parent d2c27f2 commit b0d480f

File tree

3 files changed

+79
-76
lines changed

3 files changed

+79
-76
lines changed

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "livio.rssreader"
88
minSdkVersion 23
99
targetSdkVersion 35
10-
versionCode 105
11-
versionName "1.0.5"
10+
versionCode 107
11+
versionName "1.0.7"
1212
}
1313

1414
compileOptions {
@@ -34,11 +34,11 @@ dependencies {
3434
implementation 'androidx.recyclerview:recyclerview:1.3.2'
3535
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
3636
implementation 'androidx.preference:preference:1.2.1'
37-
implementation 'androidx.work:work-runtime:2.9.0'
37+
implementation 'androidx.work:work-runtime:2.10.0'
3838

3939
implementation 'com.google.android.material:material:1.12.0'
4040

4141
// implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' //added as workaround for issue https://issuetracker.google.com/issues/238425626
42-
implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.0.0")) //27-02-2023: added as workaround for "Duplicate class in Kotlin Android" problem, but I don't use Kotlin!
42+
// implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.0.0")) //27-02-2023: added as workaround for "Duplicate class in Kotlin Android" problem, but I don't use Kotlin!
4343

4444
}

app/src/main/java/livio/rssreader/backend/RSSFeed.java

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@
4040

4141

4242
public final class RSSFeed implements Serializable {
43-
private static final long serialVersionUID = 2L;//publisher, changed value to 2
43+
private static final long serialVersionUID = 2L;//publisher, changed value to 2
4444

4545
private final static boolean debug = false;//BuildConfig.DEBUG;//should be false in prod environment
4646

4747
private String _title = "<untitled>";
48-
private String _language = null;
49-
private Date _pubdate;
48+
private String _language = null;
49+
private Date _pubdate;
5050
private String _publisherurl = null;//publisher
5151

5252
private String etag;
5353
private String lastmod;
5454
private final String feed_id;
5555

56-
private long time; // new field
57-
private final List<RSSItem> _itemlist;
58-
private final static String tagz = "RSSFeed";
59-
60-
private RSSFeed(String feed_id) {//do not use constructor from other classes, instead please use getInstance()
61-
_itemlist = new Vector<>(0);
62-
_pubdate = new Date(); // default date
56+
private long time; // new field
57+
private final List<RSSItem> _itemlist;
58+
private final static String tagz = "RSSFeed";
59+
60+
private RSSFeed(String feed_id) {//do not use constructor from other classes, instead please use getInstance()
61+
_itemlist = new Vector<>(0);
62+
_pubdate = new Date(); // default date
6363
this.feed_id = feed_id;
64-
}
64+
}
6565

6666
public static RSSFeed getInstance(File cachedir, String feed_id) {
6767
File feedFile = new File(cachedir, feed_id.concat(".cache"));
@@ -75,40 +75,40 @@ public static RSSFeed getInstance(File cachedir, String feed_id) {
7575
return new RSSFeed(feed_id);
7676
}
7777

78-
public RSSItem getItem(int location) {
79-
return _itemlist.get(location);
80-
}
81-
82-
public List<RSSItem> getAllItems() {
83-
return _itemlist;
84-
}
85-
86-
public int size() {
87-
return _itemlist.size();
88-
}
89-
90-
private void setTitle(String title) {
91-
_title = title.trim();
92-
}
93-
private void setLanguage(String lang) {
94-
_language = lang.trim();
95-
}
78+
public RSSItem getItem(int location) {
79+
return _itemlist.get(location);
80+
}
81+
82+
public List<RSSItem> getAllItems() {
83+
return _itemlist;
84+
}
85+
86+
public int size() {
87+
return _itemlist.size();
88+
}
89+
90+
private void setTitle(String title) {
91+
_title = title.trim();
92+
}
93+
private void setLanguage(String lang) {
94+
_language = lang.trim();
95+
}
9696
void setPublisherLink(String link) {//publisher
9797
_publisherurl = link;
9898
}
9999

100-
private void setPubDate(String pubdate) {
100+
private void setPubDate(String pubdate) {
101101
_pubdate = DateParser.parseDate(pubdate);
102-
}
103-
104-
public String getTitle() {
105-
return _title;
106-
}
107-
public String getLanguage(String default_lang) {
108-
if (_language == null)
109-
return default_lang;
110-
return _language;
111-
}
102+
}
103+
104+
public String getTitle() {
105+
return _title;
106+
}
107+
public String getLanguage(String default_lang) {
108+
if (_language == null)
109+
return default_lang;
110+
return _language;
111+
}
112112
public String getPublisherLink() {//publisher, note that it defaults to feed_url
113113
return _publisherurl;
114114
}
@@ -133,32 +133,32 @@ public void serialize(Context context, String etag, String lastmod) throws IOExc
133133
this.etag = etag;
134134
this.lastmod = lastmod;
135135

136-
File feedFile = new File(context.getCacheDir(), feed_id.concat(".cache"));
136+
File feedFile = new File(context.getCacheDir(), feed_id.concat(".cache"));
137137
// feedFile.deleteOnExit(); // delete the file when exiting
138-
FileOutputStream fos = new FileOutputStream(feedFile);
139-
ObjectOutputStream os = new ObjectOutputStream(fos);
140-
os.writeObject(this);
141-
os.close();
138+
FileOutputStream fos = new FileOutputStream(feedFile);
139+
ObjectOutputStream os = new ObjectOutputStream(fos);
140+
os.writeObject(this);
141+
os.close();
142142
}
143-
143+
144144
public boolean isFileUpdated(long age) {
145145
return time != 0 && System.currentTimeMillis() - time <= age;
146146
}
147-
148-
// doProcessStream()
147+
148+
// doProcessStream()
149149
// in positive case it returns the number of collected items
150150
// in negative case it returns an error code < 0
151151
private final static int zNullStream = -1;
152-
// final static int zInvalidResponseCode = -2; removed
152+
// final static int zInvalidResponseCode = -2; removed
153153
private final static int zMissingRSSTag = -3;
154154
public final static int zRedirectFeed = -4;
155155
private final static int zEmptyBody = -5;
156156

157157
private final static String encoding_element = "encoding=\"";
158158

159-
//use doProcessStream() only for xml content (not suitable for html content!)
160-
public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_titles, String feed_url, String title) throws IOException, UnsupportedCharsetException {
161-
if (is == null) return new RSSFeedResult(zNullStream);
159+
//use doProcessStream() only for xml content (not suitable for html content!)
160+
public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_titles, String feed_url, String title) throws IOException, UnsupportedCharsetException {
161+
if (is == null) return new RSSFeedResult(zNullStream);
162162
BufferedReader stream;//stream handling modified to support russian web sites with encoding windows-1251
163163
byte[] body = null;
164164
if (encoding.length() > 0)
@@ -179,20 +179,20 @@ public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_ti
179179
_title = title;//set title here and not during web data retrieval
180180
_publisherurl = feed_url;
181181
String newfeedurl = null;
182-
PriorityQueue<RSSItem> result = new PriorityQueue<>();
182+
PriorityQueue<RSSItem> result = new PriorityQueue<>();
183183
time = System.currentTimeMillis();
184-
RSSItem item = null;
185-
int rssstate = 0; // 0: init, 1: rss found, 2: channel found
184+
RSSItem item = null;
185+
int rssstate = 0; // 0: init, 1: rss found, 2: channel found
186186
if (debug) Log.d(tagz, "doProcessStream, encoding="+encoding);
187-
int state = 0; // idle
188-
int deepness = 0;
189-
String st;
190-
StringBuilder content = new StringBuilder(512), econtent = new StringBuilder(512);
191-
StringBuilder tag = new StringBuilder();
187+
int state = 0; // idle
188+
int deepness = 0;
189+
String st;
190+
StringBuilder content = new StringBuilder(512), econtent = new StringBuilder(512);
191+
StringBuilder tag = new StringBuilder();
192192
// String stag;
193-
long timer = System.nanoTime();
193+
long timer = System.nanoTime();
194194
boolean rdf = false;
195-
try {
195+
try {
196196
main:
197197
while ((st = stream.readLine()) != null) {
198198
//---begin---
@@ -203,10 +203,13 @@ public RSSFeedResult doProcessStream(InputStream is, String encoding, int max_ti
203203
if (pos != -1) {
204204
int end = st.indexOf("\"", pos + encoding_element.length());
205205
encoding = st.substring(pos + encoding_element.length(), end);//now encoding is defined
206-
stream.close(); //close stream before re-opening it
207-
stream = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(body), encoding));//re-open stream with proper encoding, beware of loops!!
208-
if (debug)
209-
Log.d(tagz, "encoding: " + encoding);
206+
if (!encoding.isEmpty()) {//change encoding
207+
stream.close(); //close stream before re-opening it
208+
stream = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(body), encoding));//re-open stream with proper encoding, beware of loops!!
209+
if (debug)
210+
Log.d(tagz, "encoding: " + encoding);
211+
continue;
212+
}
210213
}
211214
} else if ((xml = st.indexOf("<rss")) != -1)//check basic rss ?
212215
rssstate = 1; // rss found, looking for channel
@@ -312,7 +315,7 @@ else if ((xml = st.indexOf("<feed")) != -1) { //check basic feed ?
312315
int gt = st.indexOf(">", i); // uu
313316
if (gt == -1) continue main; // uu
314317
i = gt; // uu
315-
//assert: now it is true that (st.charAt(i) == '>')
318+
//assert: now it is true that (st.charAt(i) == '>')
316319

317320
if (deepness == 0)
318321
state = 0; // idle
@@ -439,17 +442,17 @@ else if ((xml = st.indexOf("<feed")) != -1) { //check basic feed ?
439442
if (item != null)
440443
result.add(item);
441444
_itemlist.clear();
442-
while ((result.size() > 0) && (_itemlist.size() < max_titles))
445+
while ((!result.isEmpty()) && (_itemlist.size() < max_titles))
443446
_itemlist.add(result.poll());
444447
if (debug) Log.d(tagz, "number of parsed items: " + _itemlist.size());
445448
return new RSSFeedResult(_itemlist.size());
446449
} else if (newfeedurl != null)
447450
return new RSSFeedResult(zRedirectFeed, newfeedurl);
448451
else return new RSSFeedResult(zMissingRSSTag);
449452
} finally {
450-
stream.close();
453+
stream.close();
451454
}
452-
} // end of doProcessStream()
453-
455+
} // end of doProcessStream()
456+
454457

455458
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:8.1.4'
8+
classpath 'com.android.tools.build:gradle:8.7.3'
99
}
1010
}
1111

0 commit comments

Comments
 (0)