From 9eac51e7297479b1e5fe412ffcf873dde705ef2e Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 9 Dec 2018 16:38:35 +0100 Subject: [PATCH] Closes #257. --- .../bou/readerforselfoss/ReaderActivity.kt | 42 ++++++++++++++++++- .../fragments/ArticleFragment.kt | 26 ++++++++---- .../ic_format_align_justify.xml | 9 ++++ .../drawable-anydpi/ic_format_align_left.xml | 9 ++++ app/src/main/res/menu/reader_menu.xml | 12 ++++++ app/src/main/res/values/strings.xml | 2 + 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable-anydpi/ic_format_align_justify.xml create mode 100644 app/src/main/res/drawable-anydpi/ic_format_align_left.xml diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 84f5569..413733d 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -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 = ArrayList() } 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 b3b068a..d90ccf3 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 @@ -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() { | | | - | $c + | $contentText |""".trimMargin(), "text/html", "utf-8", diff --git a/app/src/main/res/drawable-anydpi/ic_format_align_justify.xml b/app/src/main/res/drawable-anydpi/ic_format_align_justify.xml new file mode 100644 index 0000000..7f4e2fe --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_format_align_justify.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-anydpi/ic_format_align_left.xml b/app/src/main/res/drawable-anydpi/ic_format_align_left.xml new file mode 100644 index 0000000..2896663 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_format_align_left.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/reader_menu.xml b/app/src/main/res/menu/reader_menu.xml index 0037b27..84d298e 100644 --- a/app/src/main/res/menu/reader_menu.xml +++ b/app/src/main/res/menu/reader_menu.xml @@ -2,6 +2,18 @@ + + + + Experimental Webview not available. Disabling the article viewer to avoid any future crashes. Will load articles inside of your browser from now on. Webview issue + Align left + Justify