Simplify ArticleFragment logic (#388)

* Send a single article to ArticleFragment

* Allow using the internal browser
This commit is contained in:
davidoskky 2021-12-14 20:09:50 +01:00 committed by GitHub
parent 7592ab512b
commit 5db312bbb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 60 deletions

View File

@ -135,7 +135,7 @@ class ReaderActivity : AppCompatActivity() {
override fun getItemCount(): Int = allItems.size override fun getItemCount(): Int = allItems.size
override fun createFragment(position: Int): Fragment = ArticleFragment.newInstance(position, allItems) override fun createFragment(position: Int): Fragment = ArticleFragment.newInstance(allItems[position])
} }

View File

@ -14,11 +14,11 @@ import androidx.preference.PreferenceManager
import android.view.* import android.view.*
import android.webkit.* import android.webkit.*
import android.widget.Toast import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.core.widget.NestedScrollView import androidx.core.widget.NestedScrollView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.room.Room import androidx.room.Room
import apps.amine.bou.readerforselfoss.ImageActivity import apps.amine.bou.readerforselfoss.ImageActivity
@ -52,10 +52,9 @@ import java.util.concurrent.ExecutionException
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class ArticleFragment : Fragment() { class ArticleFragment : Fragment() {
private lateinit var pageNumber: Number
private var fontSize: Int = 16 private var fontSize: Int = 16
private lateinit var allItems: ArrayList<Item> private lateinit var item: Item
private var mCustomTabActivityHelper: CustomTabActivityHelper? = null; private var mCustomTabActivityHelper: CustomTabActivityHelper? = null
private lateinit var url: String private lateinit var url: String
private lateinit var contentText: String private lateinit var contentText: String
private lateinit var contentSource: String private lateinit var contentSource: String
@ -91,9 +90,7 @@ class ArticleFragment : Fragment() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
pageNumber = requireArguments().getInt(ARG_POSITION) item = requireArguments().getParcelable(ARG_ITEMS)!!
// TODO: The full list of items is not required, only the item used should be passed
allItems = requireArguments().getParcelableArrayList<Item>(ARG_ITEMS) as ArrayList<Item>
db = Room.databaseBuilder( db = Room.databaseBuilder(
requireContext(), requireContext(),
@ -105,16 +102,16 @@ class ArticleFragment : Fragment() {
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View {
try { try {
_binding = FragmentArticleBinding.inflate(inflater, container, false) _binding = FragmentArticleBinding.inflate(inflater, container, false)
url = allItems[pageNumber.toInt()].getLinkDecoded() url = item.getLinkDecoded()
contentText = allItems[pageNumber.toInt()].content contentText = item.content
contentTitle = allItems[pageNumber.toInt()].getTitleDecoded() contentTitle = item.getTitleDecoded()
contentImage = allItems[pageNumber.toInt()].getThumbnail(requireActivity()) contentImage = item.getThumbnail(requireActivity())
contentSource = allItems[pageNumber.toInt()].sourceAndDateText() contentSource = item.sourceAndDateText()
allImages = allItems[pageNumber.toInt()].getImages() allImages = item.getImages()
prefs = PreferenceManager.getDefaultSharedPreferences(activity) prefs = PreferenceManager.getDefaultSharedPreferences(activity)
editor = prefs.edit() editor = prefs.edit()
@ -164,26 +161,18 @@ class ArticleFragment : Fragment() {
object : FloatingToolbar.ItemClickListener { object : FloatingToolbar.ItemClickListener {
override fun onItemClick(item: MenuItem) { override fun onItemClick(item: MenuItem) {
when (item.itemId) { when (item.itemId) {
R.id.more_action -> getContentFromMercury(customTabsIntent, prefs) R.id.more_action -> getContentFromMercury(customTabsIntent)
R.id.share_action -> requireActivity().shareLink(url, contentTitle) R.id.share_action -> requireActivity().shareLink(url, contentTitle)
R.id.open_action -> requireActivity().openItemUrl( R.id.open_action -> requireActivity().openInBrowserAsNewTask(this@ArticleFragment.item)
allItems,
pageNumber.toInt(),
url,
customTabsIntent,
false,
false,
requireActivity()
)
R.id.unread_action -> if (context != null) { R.id.unread_action -> if (context != null) {
if (allItems[pageNumber.toInt()].unread) { if (this@ArticleFragment.item.unread) {
SharedItems.readItem( SharedItems.readItem(
context!!, context!!,
api, api,
db, db,
allItems[pageNumber.toInt()] this@ArticleFragment.item
) )
allItems[pageNumber.toInt()].unread = false this@ArticleFragment.item.unread = false
Toast.makeText( Toast.makeText(
context, context,
R.string.marked_as_read, R.string.marked_as_read,
@ -194,9 +183,9 @@ class ArticleFragment : Fragment() {
context!!, context!!,
api, api,
db, db,
allItems[pageNumber.toInt()] this@ArticleFragment.item
) )
allItems[pageNumber.toInt()].unread = true this@ArticleFragment.item.unread = true
Toast.makeText( Toast.makeText(
context, context,
R.string.marked_as_unread, R.string.marked_as_unread,
@ -224,7 +213,7 @@ class ArticleFragment : Fragment() {
} }
if (contentText.isEmptyOrNullOrNullString()) { if (contentText.isEmptyOrNullOrNullString()) {
getContentFromMercury(customTabsIntent, prefs) getContentFromMercury(customTabsIntent)
} else { } else {
binding.titleView.text = contentTitle binding.titleView.text = contentTitle
if (typeface != null) { if (typeface != null) {
@ -266,11 +255,11 @@ class ArticleFragment : Fragment() {
.setMessage(requireContext().getString(R.string.webview_dialog_issue_message)) .setMessage(requireContext().getString(R.string.webview_dialog_issue_message))
.setTitle(requireContext().getString(R.string.webview_dialog_issue_title)) .setTitle(requireContext().getString(R.string.webview_dialog_issue_title))
.setPositiveButton(android.R.string.ok .setPositiveButton(android.R.string.ok
) { dialog, which -> ) { _, _ ->
val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext())
val editor = sharedPref.edit() val editor = sharedPref.edit()
editor.putBoolean("prefer_article_viewer", false) editor.putBoolean("prefer_article_viewer", false)
editor.commit() editor.apply()
requireActivity().finish() requireActivity().finish()
} }
.create() .create()
@ -293,10 +282,7 @@ class ArticleFragment : Fragment() {
} }
} }
private fun getContentFromMercury( private fun getContentFromMercury(customTabsIntent: CustomTabsIntent) {
customTabsIntent: CustomTabsIntent,
prefs: SharedPreferences
) {
if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) { if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) {
binding.progressBar.visibility = View.VISIBLE binding.progressBar.visibility = View.VISIBLE
val parser = MercuryApi() val parser = MercuryApi()
@ -527,29 +513,22 @@ class ArticleFragment : Fragment() {
private fun openInBrowserAfterFailing(customTabsIntent: CustomTabsIntent) { private fun openInBrowserAfterFailing(customTabsIntent: CustomTabsIntent) {
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
requireActivity().openItemUrl( requireActivity().openItemUrlInternalBrowser(
allItems,
pageNumber.toInt(),
url, url,
customTabsIntent, customTabsIntent,
true,
false,
requireActivity() requireActivity()
) )
} }
companion object { companion object {
private const val ARG_POSITION = "position"
private const val ARG_ITEMS = "items" private const val ARG_ITEMS = "items"
fun newInstance( fun newInstance(
position: Int, item: Item
allItems: ArrayList<Item>
): ArticleFragment { ): ArticleFragment {
val fragment = ArticleFragment() val fragment = ArticleFragment()
val args = Bundle() val args = Bundle()
args.putInt(ARG_POSITION, position) args.putParcelable(ARG_ITEMS, item)
args.putParcelableArrayList(ARG_ITEMS, allItems)
fragment.arguments = args fragment.arguments = args
return fragment return fragment
} }

View File

@ -86,6 +86,18 @@ fun Context.openItemUrlInternally(
intent.putExtra("currentItem", currentItem) intent.putExtra("currentItem", currentItem)
app.startActivity(intent) app.startActivity(intent)
} else { } else {
this.openItemUrlInternalBrowser(
linkDecoded,
customTabsIntent,
app)
}
}
fun Context.openItemUrlInternalBrowser(
linkDecoded: String,
customTabsIntent: CustomTabsIntent,
app: Activity
) {
try { try {
CustomTabActivityHelper.openCustomTab( CustomTabActivityHelper.openCustomTab(
app, app,
@ -100,7 +112,6 @@ fun Context.openItemUrlInternally(
openInBrowser(linkDecoded, app) openInBrowser(linkDecoded, app)
} }
} }
}
fun Context.openItemUrl( fun Context.openItemUrl(
allItems: ArrayList<Item>, allItems: ArrayList<Item>,
@ -121,7 +132,7 @@ fun Context.openItemUrl(
} else { } else {
if (!internalBrowser) { if (!internalBrowser) {
openInBrowser(linkDecoded, app) openInBrowser(linkDecoded, app)
} else { } else if (articleViewer) {
this.openItemUrlInternally( this.openItemUrlInternally(
allItems, allItems,
currentItem, currentItem,
@ -130,6 +141,12 @@ fun Context.openItemUrl(
articleViewer, articleViewer,
app app
) )
} else {
this.openItemUrlInternalBrowser(
linkDecoded,
customTabsIntent,
app
)
} }
} }
} }