Replace json feed model by a kotlin version with generated adapters. Remove gson dependency

This commit is contained in:
Shinokuni 2020-01-16 17:21:16 +01:00
parent b0963bb9ec
commit a8404ef00f
11 changed files with 69 additions and 280 deletions

View File

@ -6,16 +6,16 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropsdb.entities.Item;
import com.readrops.readropsdb.entities.account.Account;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.matchers.FeedMatcher;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.matchers.ItemMatcher;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.utils.Utils;
import com.readrops.app.utils.matchers.FeedMatcher;
import com.readrops.app.utils.matchers.ItemMatcher;
import com.readrops.readropsdb.entities.Feed;
import com.readrops.readropsdb.entities.Item;
import com.readrops.readropsdb.entities.account.Account;
import com.readrops.readropslibrary.localfeed.AFeed;
import com.readrops.readropslibrary.localfeed.RSSQuery;
import com.readrops.readropslibrary.localfeed.RSSQueryResult;

View File

@ -43,7 +43,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
implementation('com.squareup.retrofit2:converter-moshi:2.7.1') {
exclude group: 'moshi', module: 'moshi' // moshi converter uses moshi 1.8.0 which breaks codegen 1.9.2
}

View File

@ -1,27 +0,0 @@
package com.readrops.readropslibrary.localfeed;
/*
A simple class to give an abstract level to rss/atom/json feed classes
*/
public abstract class AFeed {
protected String etag;
protected String lastModified;
public String getEtag() {
return etag;
}
public void setEtag(String etag) {
this.etag = etag;
}
public String getLastModified() {
return lastModified;
}
public void setLastModified(String lastModified) {
this.lastModified = lastModified;
}
}

View File

@ -0,0 +1,9 @@
package com.readrops.readropslibrary.localfeed
/*
A simple class to give an abstract level to rss/atom/json feed classes
*/
abstract class AFeed {
var etag: String? = null
var lastModified: String? = null
}

View File

@ -3,14 +3,15 @@ package com.readrops.readropslibrary.localfeed;
import android.accounts.NetworkErrorException;
import android.util.Log;
import com.google.gson.Gson;
import com.readrops.readropslibrary.utils.HttpBuilder;
import com.readrops.readropslibrary.utils.LibUtils;
import com.readrops.readropslibrary.utils.UnknownFormatException;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSLink;
import com.readrops.readropslibrary.utils.HttpBuilder;
import com.readrops.readropslibrary.utils.LibUtils;
import com.readrops.readropslibrary.utils.UnknownFormatException;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
@ -153,8 +154,11 @@ public class RSSQuery {
((ATOMFeed) feed).setUrl(response.request().url().toString());
break;
case RSS_JSON:
Gson gson = new Gson();
feed = gson.fromJson(xml, JSONFeed.class);
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<JSONFeed> jsonFeedAdapter = moshi.adapter(JSONFeed.class);
feed = jsonFeedAdapter.fromJson(xml);
break;
}

View File

@ -1,39 +0,0 @@
package com.readrops.readropslibrary.localfeed.json;
import com.google.gson.annotations.SerializedName;
public class JSONAuthor {
private String name;
private String url;
@SerializedName("avatar")
private String avatarUrl;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
}

View File

@ -0,0 +1,9 @@
package com.readrops.readropslibrary.localfeed.json
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class JSONAuthor(val name: String,
val url: String,
@Json(name = "avatar") val avatarUrl: String?)

View File

@ -1,83 +0,0 @@
package com.readrops.readropslibrary.localfeed.json;
import com.google.gson.annotations.SerializedName;
import com.readrops.readropslibrary.localfeed.AFeed;
import java.util.List;
public class JSONFeed extends AFeed {
private String title;
@SerializedName("home_page_url")
private String homePageUrl;
@SerializedName("feed_url")
private String feedUrl;
private String description;
@SerializedName("icon")
private String iconUrl;
@SerializedName("favicon")
private String faviconUrl;
private List<JSONItem> items;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getHomePageUrl() {
return homePageUrl;
}
public void setHomePageUrl(String homePageUrl) {
this.homePageUrl = homePageUrl;
}
public String getFeedUrl() {
return feedUrl;
}
public void setFeedUrl(String feedUrl) {
this.feedUrl = feedUrl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getFaviconUrl() {
return faviconUrl;
}
public void setFaviconUrl(String faviconUrl) {
this.faviconUrl = faviconUrl;
}
public List<JSONItem> getItems() {
return items;
}
public void setItems(List<JSONItem> items) {
this.items = items;
}
}

View File

@ -0,0 +1,15 @@
package com.readrops.readropslibrary.localfeed.json
import com.readrops.readropslibrary.localfeed.AFeed
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class JSONFeed(val version: String,
val title: String,
@Json(name = "home_page_url") val homePageUrl: String?,
@Json(name = "feed_url") val feedUrl: String?,
val description: String?,
@Json(name = "icon") val iconUrl: String?,
@Json(name = "favicon") val faviconUrl: String?,
val items: List<JSONItem>) : AFeed()

View File

@ -1,119 +0,0 @@
package com.readrops.readropslibrary.localfeed.json;
import com.google.gson.annotations.SerializedName;
public class JSONItem {
private String id;
private String title;
private String summary;
@SerializedName("content_text")
private String contentText;
@SerializedName("content_html")
private String contentHtml;
private String url;
@SerializedName("image")
private String imageUrl;
@SerializedName("date_published")
private String pubDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContentText() {
return contentText;
}
public void setContentText(String contentText) {
this.contentText = contentText;
}
public String getContentHtml() {
return contentHtml;
}
public void setContentHtml(String contentHtml) {
this.contentHtml = contentHtml;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getModDate() {
return modDate;
}
public void setModDate(String modDate) {
this.modDate = modDate;
}
public JSONAuthor getAuthor() {
return author;
}
public void setAuthor(JSONAuthor author) {
this.author = author;
}
public String getContent() {
if (contentHtml == null)
return contentText;
else
return contentHtml;
}
@SerializedName("date_modified")
private String modDate;
private JSONAuthor author;
}

View File

@ -0,0 +1,21 @@
package com.readrops.readropslibrary.localfeed.json
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class JSONItem(val id: String,
val title: String?,
val summary: String?,
@Json(name = "content_text") val contentText: String?,
@Json(name = "content_html") val contentHtml: String?,
val url: String?,
@Json(name = "image") val imageUrl: String?,
@Json(name = "date_published") val pubDate: String,
@Json(name = "date_modified") val modDate: String?,
val author: JSONAuthor?) {
fun getContent(): String? {
return contentHtml ?: contentText
}
}