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 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.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,
|
url,
|
||||||
pageNumber.toInt(),
|
customTabsIntent,
|
||||||
url,
|
requireActivity()
|
||||||
customTabsIntent,
|
|
||||||
true,
|
|
||||||
false,
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -86,19 +86,30 @@ fun Context.openItemUrlInternally(
|
|||||||
intent.putExtra("currentItem", currentItem)
|
intent.putExtra("currentItem", currentItem)
|
||||||
app.startActivity(intent)
|
app.startActivity(intent)
|
||||||
} else {
|
} else {
|
||||||
try {
|
this.openItemUrlInternalBrowser(
|
||||||
CustomTabActivityHelper.openCustomTab(
|
linkDecoded,
|
||||||
|
customTabsIntent,
|
||||||
|
app)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.openItemUrlInternalBrowser(
|
||||||
|
linkDecoded: String,
|
||||||
|
customTabsIntent: CustomTabsIntent,
|
||||||
|
app: Activity
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
CustomTabActivityHelper.openCustomTab(
|
||||||
app,
|
app,
|
||||||
customTabsIntent,
|
customTabsIntent,
|
||||||
Uri.parse(linkDecoded)
|
Uri.parse(linkDecoded)
|
||||||
) { _, uri ->
|
) { _, uri ->
|
||||||
val intent = Intent(Intent.ACTION_VIEW, uri)
|
val intent = Intent(Intent.ACTION_VIEW, uri)
|
||||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
openInBrowser(linkDecoded, app)
|
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
openInBrowser(linkDecoded, app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user