Simplify ArticleFragment logic (#388)
* Send a single article to ArticleFragment * Allow using the internal browser
This commit is contained in:
parent
7592ab512b
commit
5db312bbb8
@ -135,7 +135,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
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])
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,11 +14,11 @@ import androidx.preference.PreferenceManager
|
||||
import android.view.*
|
||||
import android.webkit.*
|
||||
import android.widget.Toast
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.core.widget.NestedScrollView
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import androidx.room.Room
|
||||
import apps.amine.bou.readerforselfoss.ImageActivity
|
||||
@ -52,10 +52,9 @@ import java.util.concurrent.ExecutionException
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class ArticleFragment : Fragment() {
|
||||
private lateinit var pageNumber: Number
|
||||
private var fontSize: Int = 16
|
||||
private lateinit var allItems: ArrayList<Item>
|
||||
private var mCustomTabActivityHelper: CustomTabActivityHelper? = null;
|
||||
private lateinit var item: Item
|
||||
private var mCustomTabActivityHelper: CustomTabActivityHelper? = null
|
||||
private lateinit var url: String
|
||||
private lateinit var contentText: String
|
||||
private lateinit var contentSource: String
|
||||
@ -91,9 +90,7 @@ class ArticleFragment : Fragment() {
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
pageNumber = requireArguments().getInt(ARG_POSITION)
|
||||
// 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>
|
||||
item = requireArguments().getParcelable(ARG_ITEMS)!!
|
||||
|
||||
db = Room.databaseBuilder(
|
||||
requireContext(),
|
||||
@ -105,16 +102,16 @@ class ArticleFragment : Fragment() {
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
): View {
|
||||
try {
|
||||
_binding = FragmentArticleBinding.inflate(inflater, container, false)
|
||||
|
||||
url = allItems[pageNumber.toInt()].getLinkDecoded()
|
||||
contentText = allItems[pageNumber.toInt()].content
|
||||
contentTitle = allItems[pageNumber.toInt()].getTitleDecoded()
|
||||
contentImage = allItems[pageNumber.toInt()].getThumbnail(requireActivity())
|
||||
contentSource = allItems[pageNumber.toInt()].sourceAndDateText()
|
||||
allImages = allItems[pageNumber.toInt()].getImages()
|
||||
url = item.getLinkDecoded()
|
||||
contentText = item.content
|
||||
contentTitle = item.getTitleDecoded()
|
||||
contentImage = item.getThumbnail(requireActivity())
|
||||
contentSource = item.sourceAndDateText()
|
||||
allImages = item.getImages()
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(activity)
|
||||
editor = prefs.edit()
|
||||
@ -164,26 +161,18 @@ class ArticleFragment : Fragment() {
|
||||
object : FloatingToolbar.ItemClickListener {
|
||||
override fun onItemClick(item: MenuItem) {
|
||||
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.open_action -> requireActivity().openItemUrl(
|
||||
allItems,
|
||||
pageNumber.toInt(),
|
||||
url,
|
||||
customTabsIntent,
|
||||
false,
|
||||
false,
|
||||
requireActivity()
|
||||
)
|
||||
R.id.open_action -> requireActivity().openInBrowserAsNewTask(this@ArticleFragment.item)
|
||||
R.id.unread_action -> if (context != null) {
|
||||
if (allItems[pageNumber.toInt()].unread) {
|
||||
if (this@ArticleFragment.item.unread) {
|
||||
SharedItems.readItem(
|
||||
context!!,
|
||||
api,
|
||||
db,
|
||||
allItems[pageNumber.toInt()]
|
||||
this@ArticleFragment.item
|
||||
)
|
||||
allItems[pageNumber.toInt()].unread = false
|
||||
this@ArticleFragment.item.unread = false
|
||||
Toast.makeText(
|
||||
context,
|
||||
R.string.marked_as_read,
|
||||
@ -194,9 +183,9 @@ class ArticleFragment : Fragment() {
|
||||
context!!,
|
||||
api,
|
||||
db,
|
||||
allItems[pageNumber.toInt()]
|
||||
this@ArticleFragment.item
|
||||
)
|
||||
allItems[pageNumber.toInt()].unread = true
|
||||
this@ArticleFragment.item.unread = true
|
||||
Toast.makeText(
|
||||
context,
|
||||
R.string.marked_as_unread,
|
||||
@ -224,7 +213,7 @@ class ArticleFragment : Fragment() {
|
||||
}
|
||||
|
||||
if (contentText.isEmptyOrNullOrNullString()) {
|
||||
getContentFromMercury(customTabsIntent, prefs)
|
||||
getContentFromMercury(customTabsIntent)
|
||||
} else {
|
||||
binding.titleView.text = contentTitle
|
||||
if (typeface != null) {
|
||||
@ -266,11 +255,11 @@ class ArticleFragment : Fragment() {
|
||||
.setMessage(requireContext().getString(R.string.webview_dialog_issue_message))
|
||||
.setTitle(requireContext().getString(R.string.webview_dialog_issue_title))
|
||||
.setPositiveButton(android.R.string.ok
|
||||
) { dialog, which ->
|
||||
) { _, _ ->
|
||||
val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val editor = sharedPref.edit()
|
||||
editor.putBoolean("prefer_article_viewer", false)
|
||||
editor.commit()
|
||||
editor.apply()
|
||||
requireActivity().finish()
|
||||
}
|
||||
.create()
|
||||
@ -293,10 +282,7 @@ class ArticleFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getContentFromMercury(
|
||||
customTabsIntent: CustomTabsIntent,
|
||||
prefs: SharedPreferences
|
||||
) {
|
||||
private fun getContentFromMercury(customTabsIntent: CustomTabsIntent) {
|
||||
if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
val parser = MercuryApi()
|
||||
@ -527,29 +513,22 @@ class ArticleFragment : Fragment() {
|
||||
|
||||
private fun openInBrowserAfterFailing(customTabsIntent: CustomTabsIntent) {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
requireActivity().openItemUrl(
|
||||
allItems,
|
||||
pageNumber.toInt(),
|
||||
requireActivity().openItemUrlInternalBrowser(
|
||||
url,
|
||||
customTabsIntent,
|
||||
true,
|
||||
false,
|
||||
requireActivity()
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARG_POSITION = "position"
|
||||
private const val ARG_ITEMS = "items"
|
||||
|
||||
fun newInstance(
|
||||
position: Int,
|
||||
allItems: ArrayList<Item>
|
||||
item: Item
|
||||
): ArticleFragment {
|
||||
val fragment = ArticleFragment()
|
||||
val args = Bundle()
|
||||
args.putInt(ARG_POSITION, position)
|
||||
args.putParcelableArrayList(ARG_ITEMS, allItems)
|
||||
args.putParcelable(ARG_ITEMS, item)
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
}
|
||||
|
@ -86,6 +86,18 @@ fun Context.openItemUrlInternally(
|
||||
intent.putExtra("currentItem", currentItem)
|
||||
app.startActivity(intent)
|
||||
} else {
|
||||
this.openItemUrlInternalBrowser(
|
||||
linkDecoded,
|
||||
customTabsIntent,
|
||||
app)
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.openItemUrlInternalBrowser(
|
||||
linkDecoded: String,
|
||||
customTabsIntent: CustomTabsIntent,
|
||||
app: Activity
|
||||
) {
|
||||
try {
|
||||
CustomTabActivityHelper.openCustomTab(
|
||||
app,
|
||||
@ -100,7 +112,6 @@ fun Context.openItemUrlInternally(
|
||||
openInBrowser(linkDecoded, app)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.openItemUrl(
|
||||
allItems: ArrayList<Item>,
|
||||
@ -121,7 +132,7 @@ fun Context.openItemUrl(
|
||||
} else {
|
||||
if (!internalBrowser) {
|
||||
openInBrowser(linkDecoded, app)
|
||||
} else {
|
||||
} else if (articleViewer) {
|
||||
this.openItemUrlInternally(
|
||||
allItems,
|
||||
currentItem,
|
||||
@ -130,6 +141,12 @@ fun Context.openItemUrl(
|
||||
articleViewer,
|
||||
app
|
||||
)
|
||||
} else {
|
||||
this.openItemUrlInternalBrowser(
|
||||
linkDecoded,
|
||||
customTabsIntent,
|
||||
app
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user