From cda09a4e8a5603a50e6067cf6aa3a0a85e354838 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Mon, 14 Dec 2020 15:29:54 +0100 Subject: [PATCH] Send all images in the webpage to the Image fragment. --- app/build.gradle | 1 + .../bou/readerforselfoss/api/selfoss/SelfossModels.kt | 10 ++++++++++ .../bou/readerforselfoss/fragments/ArticleFragment.kt | 10 ++++++---- .../bou/readerforselfoss/fragments/ImageFragment.kt | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b45b1c8..ae41e22 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -102,6 +102,7 @@ dependencies { implementation "androidx.browser:browser:$android_version" implementation "androidx.cardview:cardview:$android_version" implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha5' + implementation 'org.jsoup:jsoup:1.13.1' //multidex implementation 'androidx.multidex:multidex:2.0.1' diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt index a0c33e0..eac6397 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt @@ -5,6 +5,7 @@ import android.net.Uri import android.os.Parcel import android.os.Parcelable import android.text.Html +import org.jsoup.Jsoup import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.isEmptyOrNullOrNullString @@ -128,6 +129,15 @@ data class Item( return constructUrl(config, "thumbnails", thumbnail) } + fun getImages() : ArrayList { + var allImages = ArrayList() + + for ( image in Jsoup.parse(content).getElementsByTag("img")) { + allImages.add(image.attr("src")) + } + return allImages + } + fun getTitleDecoded(): String { return Html.fromHtml(title).toString() } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index dd4d584..9f4fc63 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -53,6 +53,7 @@ import retrofit2.Callback import retrofit2.Response import java.net.MalformedURLException import java.net.URL +import kotlin.collections.ArrayList import kotlin.concurrent.thread class ArticleFragment : Fragment() { @@ -65,6 +66,7 @@ class ArticleFragment : Fragment() { private lateinit var contentSource: String private lateinit var contentImage: String private lateinit var contentTitle: String + private lateinit var allImages : ArrayList private lateinit var editor: SharedPreferences.Editor private lateinit var fab: FloatingActionButton private lateinit var appColors: AppColors @@ -116,6 +118,7 @@ class ArticleFragment : Fragment() { contentTitle = allItems[pageNumber.toInt()].getTitleDecoded() contentImage = allItems[pageNumber.toInt()].getThumbnail(activity!!) contentSource = allItems[pageNumber.toInt()].sourceAndDateText() + allImages = allItems[pageNumber.toInt()].getImages() prefs = PreferenceManager.getDefaultSharedPreferences(activity) editor = prefs.edit() @@ -545,10 +548,9 @@ class ArticleFragment : Fragment() { fun performClick(): Boolean { if (rootView!!.webcontent.hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE || rootView!!.webcontent.hitTestResult.type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { - //TODO: Transfer all images in the webpage to the Image fragment - var allImages = ArrayList() - allImages.add(rootView!!.webcontent.hitTestResult.extra.toString()) - val position : Int = 0 + + val position : Int = allImages.indexOf(rootView!!.webcontent.hitTestResult.extra) + fragmentManager!!.beginTransaction().replace(R.id.reader_activity_view, ImageFragment.newInstance(position, allImages)).addToBackStack(null).commit() return false diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ImageFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ImageFragment.kt index c70ec43..2dad2c8 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ImageFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ImageFragment.kt @@ -9,7 +9,7 @@ import kotlinx.android.synthetic.main.fragment_article.view.webcontent class ImageFragment : Fragment() { - private lateinit var position: Number + private var position: Int = 0 private lateinit var allImages: ArrayList override fun onCreate(savedInstanceState: Bundle?) { @@ -31,7 +31,7 @@ class ImageFragment : Fragment() { view.webcontent.settings.setSupportZoom(true) view.webcontent.settings.setBuiltInZoomControls(true) view.webcontent.settings.setDisplayZoomControls(false) - view.webcontent.loadUrl(allImages[0]) + view.webcontent.loadUrl(allImages[position]) return view }