Improving item layout for items without image cover and fixing some null pointer exceptions for the favicon url and the image url

This commit is contained in:
Shinokuni 2019-02-02 15:10:13 +00:00
parent 79e8146758
commit 0335d67541
8 changed files with 134 additions and 117 deletions

View File

@ -125,10 +125,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
if (dbFeed == null) {
dbFeed = Feed.feedFromRSS(rssFeed.getChannel());
String favUrl = HtmlParser.getFaviconLink(dbFeed.getSiteUrl());
dbFeed.setIconUrl(favUrl);
dbFeed.setColor(getFaviconColor(favUrl));
setFavIconUtils(dbFeed);
dbFeed.setId((int)(database.feedDao().insert(dbFeed)));
}
@ -147,10 +144,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
if (dbFeed == null) {
dbFeed = Feed.feedFromATOM(feed);
String favUrl = HtmlParser.getFaviconLink(dbFeed.getSiteUrl());
dbFeed.setIconUrl(favUrl);
dbFeed.setColor(getFaviconColor(favUrl));
setFavIconUtils(dbFeed);
dbFeed.setId((int)(database.feedDao().insert(dbFeed)));
}
@ -170,10 +164,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
if (dbFeed == null) {
dbFeed = Feed.feedFromJSON(feed);
String favUrl = HtmlParser.getFaviconLink(dbFeed.getSiteUrl());
dbFeed.setIconUrl(favUrl);
dbFeed.setColor(getFaviconColor(favUrl));
setFavIconUtils(dbFeed);
dbFeed.setId((int)(database.feedDao().insert(dbFeed)));
}
@ -197,6 +188,14 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
}
}
private void setFavIconUtils(Feed feed) throws IOException {
String favUrl = HtmlParser.getFaviconLink(feed.getSiteUrl());
if (favUrl != null) {
feed.setIconUrl(favUrl);
feed.setColor(getFaviconColor(favUrl));
}
}
private @ColorInt int getFaviconColor(String favUrl) throws IOException {
Bitmap favicon = getFaviconFromUrl(favUrl);
Palette palette = Palette.from(favicon).generate();
@ -209,9 +208,12 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
InputStream inputStream = response.body().byteStream();
return BitmapFactory.decodeStream(inputStream);
if (response.isSuccessful()) {
InputStream inputStream = response.body().byteStream();
return BitmapFactory.decodeStream(inputStream);
} else
return null;
}
}

View File

@ -27,7 +27,7 @@ import com.readrops.app.database.entities.Item;
import java.util.Collections;
import java.util.List;
public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListAdapter.ViewHolder> implements ListPreloader.PreloadModelProvider<String> {
public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListAdapter.ItemViewHolder> implements ListPreloader.PreloadModelProvider<String> {
private RequestManager manager;
private OnItemClickListener listener;
@ -60,15 +60,15 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.image_list_item, viewGroup, false);
View view = inflater.inflate(R.layout.image_item, viewGroup, false);
return new ViewHolder(view);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
public void onBindViewHolder(@NonNull ItemViewHolder viewHolder, int i) {
ItemWithFeed itemWithFeed = getItem(i);
viewHolder.bind(itemWithFeed);
@ -79,6 +79,7 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
requestOptions = requestOptions.transforms(new CenterCrop(), new RoundedCorners(16));
if (itemWithFeed.getItem().getImageLink() != null) {
viewHolder.itemImage.setVisibility(View.VISIBLE);
manager.load(itemWithFeed.getItem().getImageLink())
.apply(requestOptions)
.transition(DrawableTransitionOptions.withCrossFade(FADE_FACTORY))
@ -114,16 +115,16 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
this.listener = listener;
}
class ViewHolder extends RecyclerView.ViewHolder {
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView itemTitle;
private ImageView itemImage;
private TextView date;
private TextView feedName;
private TextView itemDescription;
private ImageView feedIcon;
private ImageView itemImage;
ViewHolder(@NonNull View itemView) {
ItemViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener((view -> {
@ -134,11 +135,11 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
}));
itemTitle = itemView.findViewById(R.id.item_title);
itemImage = itemView.findViewById(R.id.item_image);
date = itemView.findViewById(R.id.item_date);
feedName = itemView.findViewById(R.id.item_feed_title);
itemDescription = itemView.findViewById(R.id.item_description);
feedIcon = itemView.findViewById(R.id.item_feed_icon);
itemImage = itemView.findViewById(R.id.item_image);
}
private void bind(ItemWithFeed itemWithFeed) {

View File

@ -20,7 +20,7 @@ public interface ItemDao {
@Query("Select * from Item Order By pub_date DESC")
LiveData<List<Item>> getAll();
@Query("Select Item.id, title, Item.description, image_link, pub_date, name, color, icon_url from Item Inner Join Feed on Item.feed_id = Feed.id Order By pub_date DESC, Item.id")
@Query("Select Item.id, title, Item.description, image_link, pub_date, name, color, icon_url from Item Inner Join Feed on Item.feed_id = Feed.id Order By Item.id ASC")
LiveData<List<ItemWithFeed>> getAllItemWithFeeds();
@Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end")

View File

@ -125,6 +125,10 @@ public class Item {
this.feedId = feedId;
}
public boolean hasImage() {
return getImageLink() != null;
}
public static List<Item> itemsFromRSS(List<RSSItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();

View File

@ -2,10 +2,10 @@
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout">
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
@ -30,7 +30,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:listitem="@layout/image_list_item"/>
tools:listitem="@layout/image_item" />
</android.support.v4.widget.SwipeRefreshLayout>
@ -46,9 +46,8 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add_white"
android:onClick="displayAddFeedDialog"
/>
android:src="@drawable/ic_add_white" />
</FrameLayout>
@ -59,7 +58,7 @@
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header"/>
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="6dp">
<RelativeLayout
android:id="@+id/layout_start"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/item_image"
android:layout_toLeftOf="@id/item_image"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
tools:text="This is an item title with sufficient length" />
<TextView
android:id="@+id/item_description"
style="@style/TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/item_image"
android:layout_toLeftOf="@id/item_image"
android:ellipsize="end"
android:maxLines="3"
android:minLines="1"
tools:text="This is an item description" />
<ImageView
android:id="@+id/item_image"
android:layout_width="150dp"
android:layout_height="92dp"
android:layout_alignParentEnd="true"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_start"
android:layout_marginTop="6dp">
<ImageView
android:id="@+id/item_feed_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_rss_feed" />
<TextView
android:id="@+id/item_feed_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:layout_toRightOf="@id/item_feed_icon"
android:ellipsize="end"
tools:text="Numerama" />
<TextView
android:id="@+id/item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
tools:text="January 10 2019" />
</RelativeLayout>
</RelativeLayout>

View File

@ -1,85 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="?android:attr/selectableItemBackground"
android:padding="6dp">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="test title for this textview which is 2 maximum lines"
android:layout_marginRight="8dp"
android:layout_marginEnd="6dp"
android:minLines="1"
android:maxLines="2"
android:ellipsize="end"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@id/item_image"
android:layout_toStartOf="@id/item_image"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>
<TextView
android:id="@+id/item_description"
style="@style/TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_toStartOf="@id/item_image"
android:layout_toLeftOf="@id/item_image"
android:ellipsize="end"
android:maxLines="3"
android:minLines="1" />
<ImageView
android:id="@+id/item_image"
android:layout_width="150dp"
android:layout_height="92dp"
android:layout_alignParentEnd="true"
/>
<RelativeLayout
android:id="@+id/layout_end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_image"
android:layout_marginTop="12dp">
<ImageView
android:id="@+id/item_feed_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/ic_rss_feed"
/>
<TextView
android:id="@+id/item_feed_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_feed_icon"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:ellipsize="end"
tools:text="Numerama"/>
<TextView
android:id="@+id/item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
tools:text="January 10 2019"
/>
</RelativeLayout>
</RelativeLayout>

View File

@ -100,7 +100,11 @@ public final class HtmlParser {
public static String getDescImageLink(String description) {
Document document = Jsoup.parse(description);
Elements elements = document.select("img");
return document.select("img").first().attr("src");
if (!elements.isEmpty())
return elements.first().attr("src");
else
return null;
}
}