From 9042b9a039eaf8ccd9c853c571b9ecce29177a13 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Thu, 7 Feb 2019 18:33:55 +0000 Subject: [PATCH] Adding Item view activity with very basic collapsing toolbar layout and webview to render item html content. Big improvements needed. --- .idea/caches/build_file_checksums.ser | Bin 592 -> 592 bytes app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 17 +++--- .../java/com/readrops/app/AddFeedDialog.java | 8 +-- .../com/readrops/app/BasedRepository.java | 41 +++++++++++++ .../java/com/readrops/app/ItemActivity.java | 57 ++++++++++++++++++ .../java/com/readrops/app/ItemViewModel.java | 26 ++++++++ .../com/readrops/app/LocalFeedRepository.java | 28 ++++----- .../java/com/readrops/app/MainActivity.java | 8 +++ .../readrops/app/database/dao/ItemDao.java | 3 + .../java/com/readrops/app/utils/Utils.java | 23 +++++++ app/src/main/res/layout/activity_item.xml | 53 ++++++++++++++++ .../readrops/readropslibrary/HtmlParser.java | 10 +++ 13 files changed, 245 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/com/readrops/app/BasedRepository.java create mode 100644 app/src/main/java/com/readrops/app/ItemActivity.java create mode 100644 app/src/main/java/com/readrops/app/ItemViewModel.java create mode 100644 app/src/main/res/layout/activity_item.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 821c824c13c6af4aa07dba7ce40000dbf174ec94..09c7e473267b39beb5a1a085c6c3f3312b6df615 100644 GIT binary patch delta 33 pcmcb>a)D*S43-nAYTO&=EM^pBnIYq|$v&9b@Y>b4m1~&oD*)U@4JZHr delta 33 rcmV++0N(%51kePKm;|)?=RmQXqX7~BsQ7p2|1)}Mv=BU+vdn&XDvc4$ diff --git a/app/build.gradle b/app/build.gradle index 0d64166b..379f6605 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.8.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' implementation "com.github.bumptech.glide:okhttp3-integration:4.8.0" - implementation ("com.github.bumptech.glide:recyclerview-integration:4.8.0") { + implementation("com.github.bumptech.glide:recyclerview-integration:4.8.0") { // Excludes the support library because it's already included by Glide. transitive = false } @@ -58,6 +58,4 @@ dependencies { implementation 'org.jsoup:jsoup:1.11.3' - - } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 395d2f92..0bd7b5b9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,7 +4,7 @@ package="com.readrops.app"> - + - - - - + @@ -26,5 +24,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/AddFeedDialog.java b/app/src/main/java/com/readrops/app/AddFeedDialog.java index 1846b5eb..810deb29 100644 --- a/app/src/main/java/com/readrops/app/AddFeedDialog.java +++ b/app/src/main/java/com/readrops/app/AddFeedDialog.java @@ -6,7 +6,6 @@ import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.design.widget.TextInputEditText; @@ -19,16 +18,11 @@ import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; +import com.readrops.app.utils.Utils; import com.readrops.readropslibrary.HtmlParser; import com.readrops.readropslibrary.ParsingResult; -import org.jsoup.HttpStatusException; - -import java.io.IOException; -import java.net.SocketTimeoutException; import java.net.UnknownHostException; -import java.nio.charset.MalformedInputException; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; diff --git a/app/src/main/java/com/readrops/app/BasedRepository.java b/app/src/main/java/com/readrops/app/BasedRepository.java new file mode 100644 index 00000000..afb31dc4 --- /dev/null +++ b/app/src/main/java/com/readrops/app/BasedRepository.java @@ -0,0 +1,41 @@ +package com.readrops.app; + +import android.app.Application; +import android.arch.lifecycle.LiveData; + +import com.readrops.app.database.entities.Item; +import com.readrops.readropslibrary.ParsingResult; + +public class BasedRepository extends ARepository { + + protected BasedRepository(Application application) { + super(application); + } + + public LiveData getItemById(int id) { + return database.itemDao().getItemById(id); + } + + + @Override + public void sync() { + + } + + @Override + public void addFeed(ParsingResult result) { + + } + + @Override + public void deleteFeed(Item item) { + + } + + @Override + public void moveFeed(Item item) { + + } + + +} diff --git a/app/src/main/java/com/readrops/app/ItemActivity.java b/app/src/main/java/com/readrops/app/ItemActivity.java new file mode 100644 index 00000000..4725ad28 --- /dev/null +++ b/app/src/main/java/com/readrops/app/ItemActivity.java @@ -0,0 +1,57 @@ +package com.readrops.app; + +import android.arch.lifecycle.ViewModelProvider; +import android.content.Intent; +import android.graphics.Bitmap; +import android.support.design.widget.CollapsingToolbarLayout; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.webkit.WebView; +import android.widget.ImageView; + +import com.readrops.app.utils.GlideApp; +import com.readrops.app.utils.Utils; +import com.readrops.readropslibrary.Utils.LibUtils; + +import java.io.IOException; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; + +public class ItemActivity extends AppCompatActivity { + + private ItemViewModel viewModel; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_item); + + Intent intent = getIntent(); + int itemId = intent.getIntExtra("itemId", 0); + String imageUrl = intent.getStringExtra("imageUrl"); + + Toolbar toolbar = findViewById(R.id.collasping_layout_toolbar); + setSupportActionBar(toolbar); + + if (getSupportActionBar() != null) + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + CollapsingToolbarLayout toolbarLayout = findViewById(R.id.collapsing_layout); + toolbarLayout.setTitle(""); + + ImageView imageView = findViewById(R.id.collapsing_layout_image); + + GlideApp.with(this) + .load(imageUrl) + .into(imageView); + + WebView webView = findViewById(R.id.item_webview); + + viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ItemViewModel.class); + viewModel.getItemById(itemId).observe(this, item -> { + webView.loadData(item.getContent(), LibUtils.HTML_CONTENT_TYPE, "utf-8"); + }); + } +} diff --git a/app/src/main/java/com/readrops/app/ItemViewModel.java b/app/src/main/java/com/readrops/app/ItemViewModel.java new file mode 100644 index 00000000..f01cd30e --- /dev/null +++ b/app/src/main/java/com/readrops/app/ItemViewModel.java @@ -0,0 +1,26 @@ +package com.readrops.app; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; +import android.support.annotation.NonNull; + +import com.readrops.app.database.Database; +import com.readrops.app.database.entities.Item; + +public class ItemViewModel extends AndroidViewModel { + + BasedRepository repository; + + public ItemViewModel(@NonNull Application application) { + super(application); + + repository = new BasedRepository(application); + } + + public LiveData getItemById(int id) { + return repository.getItemById(id); + } + + +} diff --git a/app/src/main/java/com/readrops/app/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/LocalFeedRepository.java index 5ecc2207..09c9d9e6 100644 --- a/app/src/main/java/com/readrops/app/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/LocalFeedRepository.java @@ -12,6 +12,7 @@ import android.util.Patterns; import com.readrops.app.database.ItemWithFeed; import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Item; +import com.readrops.app.utils.Utils; import com.readrops.readropslibrary.HtmlParser; import com.readrops.readropslibrary.ParsingResult; import com.readrops.readropslibrary.QueryCallback; @@ -28,7 +29,6 @@ import org.jsoup.Jsoup; import java.io.IOException; import java.io.InputStream; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.TreeMap; @@ -203,8 +203,16 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { for (Item dbItem : items) { if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid()))) { if (dbItem.getDescription() != null) { - if (dbItem.getImageLink() == null) - dbItem.setImageLink(HtmlParser.getDescImageLink(dbItem.getDescription(), feed.getSiteUrl())); + + 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 + dbItem.setContent(HtmlParser.deleteCoverImage(dbItem.getContent())); + } + } dbItem.setDescription(Jsoup.parse(dbItem.getDescription()).text()); } @@ -224,23 +232,11 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { } private @ColorInt int getFaviconColor(String favUrl) throws IOException { - Bitmap favicon = getFaviconFromUrl(favUrl); + Bitmap favicon = Utils.getImageFromUrl(favUrl); Palette palette = Palette.from(favicon).generate(); return palette.getDominantSwatch().getRgb(); } - private Bitmap getFaviconFromUrl(String url) throws IOException { - OkHttpClient okHttpClient = new OkHttpClient(); - Request request = new Request.Builder().url(url).build(); - - Response response = okHttpClient.newCall(request).execute(); - - if (response.isSuccessful()) { - InputStream inputStream = response.body().byteStream(); - return BitmapFactory.decodeStream(inputStream); - } else - return null; - } } diff --git a/app/src/main/java/com/readrops/app/MainActivity.java b/app/src/main/java/com/readrops/app/MainActivity.java index 9ef356f7..d20fb968 100644 --- a/app/src/main/java/com/readrops/app/MainActivity.java +++ b/app/src/main/java/com/readrops/app/MainActivity.java @@ -2,6 +2,7 @@ package com.readrops.app; import android.app.Dialog; import android.arch.lifecycle.ViewModelProvider; +import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; @@ -123,6 +124,13 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S ViewPreloadSizeProvider preloadSizeProvider = new ViewPreloadSizeProvider(); adapter = new MainItemListAdapter(GlideApp.with(this), preloadSizeProvider); + adapter.setOnItemClickListener(itemWithFeed -> { + Intent intent = new Intent(this, ItemActivity.class); + intent.putExtra("itemId", itemWithFeed.getItem().getId()); + intent.putExtra("imageUrl", itemWithFeed.getItem().getImageLink()); + + startActivity(intent); + }); RecyclerViewPreloader preloader = new RecyclerViewPreloader(Glide.with(this), adapter, preloadSizeProvider, 10); recyclerView.addOnScrollListener(preloader); diff --git a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java index 0b879c0b..a53ca296 100644 --- a/app/src/main/java/com/readrops/app/database/dao/ItemDao.java +++ b/app/src/main/java/com/readrops/app/database/dao/ItemDao.java @@ -31,4 +31,7 @@ public interface ItemDao { @Insert void insertAll(List items); + + @Query("Select * from Item Where id = :id") + LiveData getItemById(int id); } 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 bec47a15..26c775f0 100644 --- a/app/src/main/java/com/readrops/app/utils/Utils.java +++ b/app/src/main/java/com/readrops/app/utils/Utils.java @@ -1,10 +1,19 @@ package com.readrops.app.utils; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Looper; import android.widget.Toast; +import java.io.IOException; +import java.io.InputStream; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + public final class Utils { public static final String HTTP_PREFIX = "http://"; @@ -22,4 +31,18 @@ public final class Utils { toast.show(); } + + public static Bitmap getImageFromUrl(String url) throws IOException { + OkHttpClient okHttpClient = new OkHttpClient(); + Request request = new Request.Builder().url(url).build(); + + Response response = okHttpClient.newCall(request).execute(); + + if (response.isSuccessful()) { + InputStream inputStream = response.body().byteStream(); + return BitmapFactory.decodeStream(inputStream); + } else + return null; + } + } diff --git a/app/src/main/res/layout/activity_item.xml b/app/src/main/res/layout/activity_item.xml new file mode 100644 index 00000000..bf15a948 --- /dev/null +++ b/app/src/main/res/layout/activity_item.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java index 0ee6afc1..59d0e6bf 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java @@ -111,4 +111,14 @@ public final class HtmlParser { else return null; } + + public static String deleteCoverImage(String content) { + Document document = Jsoup.parse(content); + Elements elements = document.select("img"); + + if (!elements.isEmpty()) + elements.first().remove(); + + return document.toString(); + } }