This commit is contained in:
Amine 2018-12-09 16:38:35 +01:00
parent fa9cce6783
commit 9eac51e729
6 changed files with 92 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package apps.amine.bou.readerforselfoss
import android.content.SharedPreferences
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
@ -13,6 +14,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.room.Room
import apps.amine.bou.readerforselfoss.api.selfoss.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
@ -51,6 +53,11 @@ class ReaderActivity : AppCompatActivity() {
private lateinit var toolbarMenu: Menu
private lateinit var db: AppDatabase
private lateinit var prefs: SharedPreferences
private var activeAlignment: Int = 1
val JUSTIFY = 1
val ALIGN_LEFT = 2
private fun showMenuItem(willAddToFavorite: Boolean) {
toolbarMenu.findItem(R.id.save).isVisible = willAddToFavorite
@ -65,6 +72,8 @@ class ReaderActivity : AppCompatActivity() {
showMenuItem(false)
}
private lateinit var editor: SharedPreferences.Editor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -85,11 +94,13 @@ class ReaderActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
prefs = PreferenceManager.getDefaultSharedPreferences(this)
editor = prefs.edit()
debugReadingItems = prefs.getBoolean("read_debug", false)
userIdentifier = prefs.getString("unique_id", "")
markOnScroll = prefs.getBoolean("mark_on_scroll", false)
activeAlignment = prefs.getInt("text_align", JUSTIFY)
api = SelfossApi(
this,
@ -223,6 +234,11 @@ class ReaderActivity : AppCompatActivity() {
}
}
fun alignmentMenu(showJustify: Boolean) {
toolbarMenu.findItem(R.id.align_left).isVisible = !showJustify
toolbarMenu.findItem(R.id.align_justify).isVisible = showJustify
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.reader_menu, menu)
@ -233,6 +249,11 @@ class ReaderActivity : AppCompatActivity() {
} else {
canFavorite()
}
if (activeAlignment == JUSTIFY) {
alignmentMenu(false)
} else {
alignmentMenu(true)
}
return true
}
@ -314,10 +335,29 @@ class ReaderActivity : AppCompatActivity() {
}
}
}
R.id.align_left -> {
editor.putInt("text_align", ALIGN_LEFT)
editor.apply()
alignmentMenu(true)
refreshFragment()
}
R.id.align_justify -> {
editor.putInt("text_align", JUSTIFY)
editor.apply()
alignmentMenu(false)
refreshFragment()
}
}
return super.onOptionsItemSelected(item)
}
private fun refreshFragment() {
finish()
overridePendingTransition(0, 0)
startActivity(intent)
overridePendingTransition(0, 0)
}
companion object {
var allItems: ArrayList<Item> = ArrayList()
}

View File

@ -67,6 +67,7 @@ class ArticleFragment : Fragment() {
private lateinit var fab: FloatingActionButton
private lateinit var appColors: AppColors
private lateinit var db: AppDatabase
private lateinit var textAlignment: String
override fun onStop() {
super.onStop()
@ -91,6 +92,7 @@ class ArticleFragment : Fragment() {
private var rootView: ViewGroup? = null
private lateinit var prefs: SharedPreferences
override fun onCreateView(
inflater: LayoutInflater,
@ -107,9 +109,10 @@ class ArticleFragment : Fragment() {
contentImage = allItems[pageNumber.toInt()].getThumbnail(activity!!)
contentSource = allItems[pageNumber.toInt()].sourceAndDateText()
val prefs = PreferenceManager.getDefaultSharedPreferences(activity)
prefs = PreferenceManager.getDefaultSharedPreferences(activity)
editor = prefs.edit()
fontSize = prefs.getString("reader_font_size", "16").toInt()
refreshAlignment()
val settings = activity!!.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE)
val debugReadingItems = prefs.getBoolean("read_debug", false)
@ -204,7 +207,7 @@ class ArticleFragment : Fragment() {
} else {
rootView!!.titleView.text = contentTitle
htmlToWebview(contentText, prefs)
htmlToWebview()
if (!contentImage.isEmptyOrNullOrNullString() && context != null) {
rootView!!.imageView.visibility = View.VISIBLE
@ -248,6 +251,14 @@ class ArticleFragment : Fragment() {
return rootView
}
private fun refreshAlignment() {
textAlignment = when (prefs.getInt("text_align", 1)) {
1 -> "justify"
2 -> "left"
else -> "justify"
}
}
private fun getContentFromMercury(
customTabsIntent: CustomTabsIntent,
prefs: SharedPreferences
@ -283,7 +294,8 @@ class ArticleFragment : Fragment() {
}
try {
htmlToWebview(response.body()!!.content.orEmpty(), prefs)
contentText = response.body()!!.content.orEmpty()
htmlToWebview()
} catch (e: Exception) {
if (context != null) {
ACRA.getErrorReporter().maybeHandleSilentException(e, context!!)
@ -346,7 +358,7 @@ class ArticleFragment : Fragment() {
}
}
private fun htmlToWebview(c: String, prefs: SharedPreferences) {
private fun htmlToWebview() {
val stringColor = String.format("#%06X", 0xFFFFFF and appColors.colorAccent)
rootView!!.webcontent.visibility = View.VISIBLE
@ -428,12 +440,12 @@ class ArticleFragment : Fragment() {
| }
| * {
| font-size: ${fontSize}px;
| text-align: justify;
| text-align: $textAlignment;
| word-break: break-word;
| overflow:hidden;
| }
| a, pre, code {
| text-align: left;
| text-align: $textAlignment;
| }
| pre, code {
| white-space: pre-wrap;
@ -443,7 +455,7 @@ class ArticleFragment : Fragment() {
| </style>
|</head>
|<body>
| $c
| $contentText
|</body>""".trimMargin(),
"text/html",
"utf-8",

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M3,21h18v-2L3,19v2zM3,17h18v-2L3,15v2zM3,13h18v-2L3,11v2zM3,9h18L21,7L3,7v2zM3,3v2h18L21,3L3,3z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15,15L3,15v2h12v-2zM15,7L3,7v2h12L15,7zM3,13h18v-2L3,11v2zM3,21h18v-2L3,19v2zM3,3v2h18L21,3L3,3z"/>
</vector>

View File

@ -2,6 +2,18 @@
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/align_left"
android:icon="@drawable/ic_format_align_left"
android:visible="true"
app:showAsAction="ifRoom"
android:title="@string/reader_text_align_left" />
<item android:id="@+id/align_justify"
android:icon="@drawable/ic_format_align_justify"
android:visible="true"
app:showAsAction="ifRoom"
android:title="@string/reader_text_align_justify" />
<item
android:id="@+id/unsave"
android:icon="@drawable/heart_on"

View File

@ -170,4 +170,6 @@
<string name="pref_header_experimental">Experimental</string>
<string name="webview_dialog_issue_message">Webview not available. Disabling the article viewer to avoid any future crashes. Will load articles inside of your browser from now on.</string>
<string name="webview_dialog_issue_title">Webview issue</string>
<string name="reader_text_align_left">Align left</string>
<string name="reader_text_align_justify">Justify</string>
</resources>