From a1d411232206c0e2e2a9341340b191536d884481 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Fri, 8 Feb 2019 18:22:42 +0000 Subject: [PATCH] Add item readtime on item list activity and item activity --- .idea/assetWizardSettings.xml | 46 ++++++++++++++++ .../java/com/readrops/app/ItemActivity.java | 16 ++++++ .../com/readrops/app/LocalFeedRepository.java | 10 ++-- .../java/com/readrops/app/MainActivity.java | 1 + .../com/readrops/app/MainItemListAdapter.java | 14 +++++ .../readrops/app/database/dao/ItemDao.java | 4 +- .../readrops/app/database/entities/Item.java | 11 ++++ .../java/com/readrops/app/utils/Utils.java | 9 ++++ app/src/main/res/drawable/ic_read_time.xml | 9 ++++ app/src/main/res/layout/activity_item.xml | 41 ++++++++++---- app/src/main/res/layout/image_item.xml | 53 +++++++++++++++---- app/src/main/res/values-fr-rFR/strings.xml | 3 ++ app/src/main/res/values/strings.xml | 4 ++ 13 files changed, 197 insertions(+), 24 deletions(-) create mode 100644 .idea/assetWizardSettings.xml create mode 100644 app/src/main/res/drawable/ic_read_time.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml new file mode 100644 index 00000000..541b2c33 --- /dev/null +++ b/.idea/assetWizardSettings.xml @@ -0,0 +1,46 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/ItemActivity.java b/app/src/main/java/com/readrops/app/ItemActivity.java index 1609f021..7afb6bd5 100644 --- a/app/src/main/java/com/readrops/app/ItemActivity.java +++ b/app/src/main/java/com/readrops/app/ItemActivity.java @@ -14,6 +14,7 @@ import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.ImageView; +import android.widget.RelativeLayout; import android.widget.TextView; import com.readrops.app.database.ItemWithFeed; @@ -37,6 +38,8 @@ public class ItemActivity extends AppCompatActivity { private TextView author; private TextView readTime; + private RelativeLayout readTimeLayout; + private CollapsingToolbarLayout toolbarLayout; private ReadropsWebView webView; @@ -71,6 +74,7 @@ public class ItemActivity extends AppCompatActivity { title = findViewById(R.id.activity_item_title); author = findViewById(R.id.activity_item_author); readTime = findViewById(R.id.activity_item_readtime); + readTimeLayout = findViewById(R.id.activity_item_readtime_layout); viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ItemViewModel.class); viewModel.getItemById(itemId).observe(this, this::bindUI); @@ -87,6 +91,18 @@ public class ItemActivity extends AppCompatActivity { author.setVisibility(View.VISIBLE); } + if (item.getReadTime() > 0) { + int minutes = (int)Math.round(item.getReadTime()); + if (minutes < 1) + readTime.setText(getResources().getString(R.string.read_time_lower_than_1)); + else if (minutes > 1) + readTime.setText(getResources().getString(R.string.read_time, String.valueOf(minutes))); + else + readTime.setText(getResources().getString(R.string.read_time_one_minute)); + + readTimeLayout.setVisibility(View.VISIBLE); + } + webView.setItem(itemWithFeed); } } diff --git a/app/src/main/java/com/readrops/app/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/LocalFeedRepository.java index 09bbd763..e4a5fbf7 100644 --- a/app/src/main/java/com/readrops/app/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/LocalFeedRepository.java @@ -203,18 +203,22 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { for (Item dbItem : items) { if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid()))) { if (dbItem.getDescription() != null) { + dbItem.setCleanDescription(Jsoup.parse(dbItem.getDescription()).text()); if (dbItem.getImageLink() == null) { String imageUrl = HtmlParser.getDescImageLink(dbItem.getDescription(), feed.getSiteUrl()); if (imageUrl != null) { dbItem.setImageLink(imageUrl); - if (dbItem.getContent() != null) // removing cover image in content if found in description + if (dbItem.getContent() != null) { + // removing cover image in content if found in description dbItem.setContent(HtmlParser.deleteCoverImage(dbItem.getContent())); + + dbItem.setReadTime(Utils.readTimeFromString(dbItem.getContent())); + } else + dbItem.setReadTime(Utils.readTimeFromString(dbItem.getCleanDescription())); } } - - dbItem.setCleanDescription(Jsoup.parse(dbItem.getDescription()).text()); } database.itemDao().insert(dbItem); diff --git a/app/src/main/java/com/readrops/app/MainActivity.java b/app/src/main/java/com/readrops/app/MainActivity.java index d20fb968..fdae0785 100644 --- a/app/src/main/java/com/readrops/app/MainActivity.java +++ b/app/src/main/java/com/readrops/app/MainActivity.java @@ -90,6 +90,7 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S viewModel.getItemsWithFeed().observe(this, (itemWithFeeds -> { newItems = itemWithFeeds; + if (!refreshLayout.isRefreshing()) adapter.submitList(newItems); })); diff --git a/app/src/main/java/com/readrops/app/MainItemListAdapter.java b/app/src/main/java/com/readrops/app/MainItemListAdapter.java index 2de068bc..3c825fdc 100644 --- a/app/src/main/java/com/readrops/app/MainItemListAdapter.java +++ b/app/src/main/java/com/readrops/app/MainItemListAdapter.java @@ -1,6 +1,7 @@ package com.readrops.app; import android.content.res.ColorStateList; +import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -104,6 +105,15 @@ public class MainItemListAdapter extends ListAdapter 1) + viewHolder.itemReadTime.setText(resources.getString(R.string.read_time, String.valueOf(minutes))); + else + viewHolder.itemReadTime.setText(resources.getString(R.string.read_time_one_minute)); } @Override @@ -149,6 +159,7 @@ public class MainItemListAdapter extends ListAdapter> getAll(); - @Query("Select Item.id, title, clean_description, image_link, pub_date, name, color, icon_url from Item Inner Join Feed on Item.feed_id = Feed.id Order By Item.id DESC") + @Query("Select Item.id, title, clean_description, image_link, pub_date, name, color, icon_url, read_time from Item Inner Join Feed on Item.feed_id = Feed.id Order By Item.id DESC") LiveData> getAllItemWithFeeds(); @Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end") @@ -32,6 +32,6 @@ public interface ItemDao { @Insert void insertAll(List items); - @Query("Select title, Item.description, content, pub_date, author, 0 as color, name from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") + @Query("Select title, Item.description, content, pub_date, author, 0 as color, read_time, name from Item Inner Join Feed on Item.feed_id = Feed.id And Item.id = :id") LiveData getItemById(int id); } diff --git a/app/src/main/java/com/readrops/app/database/entities/Item.java b/app/src/main/java/com/readrops/app/database/entities/Item.java index 8493ffbc..cbf33c22 100644 --- a/app/src/main/java/com/readrops/app/database/entities/Item.java +++ b/app/src/main/java/com/readrops/app/database/entities/Item.java @@ -47,6 +47,9 @@ public class Item { @ColumnInfo(index = true) private String guid; + @ColumnInfo(name = "read_time") + private double readTime; + public int getId() { return id; } @@ -139,6 +142,14 @@ public class Item { return getImageLink() != null; } + public double getReadTime() { + return readTime; + } + + public void setReadTime(double readTime) { + this.readTime = readTime; + } + public String getText() { if (content != null) return content; diff --git a/app/src/main/java/com/readrops/app/utils/Utils.java b/app/src/main/java/com/readrops/app/utils/Utils.java index ea550ff9..50606623 100644 --- a/app/src/main/java/com/readrops/app/utils/Utils.java +++ b/app/src/main/java/com/readrops/app/utils/Utils.java @@ -23,6 +23,8 @@ public final class Utils { public static final String HTTPS_PREFIX = "https://"; + public static final int AVERAGE_WORDS_PER_MINUTE = 250; + public static void displayErrorInMainThread(Context context, String message) { Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG); @@ -55,4 +57,11 @@ public final class Utils { return displayMetrics.widthPixels; } + public static double readTimeFromString(String value) { + int nbWords = value.split("\\s+").length; + double minutes = (double)nbWords / AVERAGE_WORDS_PER_MINUTE; + + return minutes; + } + } diff --git a/app/src/main/res/drawable/ic_read_time.xml b/app/src/main/res/drawable/ic_read_time.xml new file mode 100644 index 00000000..2239a4f4 --- /dev/null +++ b/app/src/main/res/drawable/ic_read_time.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_item.xml b/app/src/main/res/layout/activity_item.xml index 5bc3d357..7d097006 100644 --- a/app/src/main/res/layout/activity_item.xml +++ b/app/src/main/res/layout/activity_item.xml @@ -58,6 +58,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" + android:layout_marginBottom="8dp" tools:text="This is a title" /> + tools:text="By Santa Klaus" + tools:visibility="visible"/> - + android:layout_below="@id/activity_item_title" + tools:visibility="visible"> + + + + + + + - + android:layout_toStartOf="@id/item_readtime"> + + + + + + + + android:layout_toStartOf="@id/item_interpoint" + tools:text="3 mins read" /> + + diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 20e70484..cdebedfe 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -17,5 +17,8 @@ Erreur de connexion au site Site inconnu par %1$s + %1$s minutes + Moins d\'une minute + 1 minute \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b4fae48b..c6a51237 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,4 +18,8 @@ Connection error Unknown host by %1$s + %1$s mins read + Less than a minute + 1 min read + ยท