Fix linkify preference when Markdown enabled

pull/1462/head
Alex Baker 5 years ago
parent bc5e08ec14
commit 1a04be6afd

@ -79,6 +79,9 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
@Inject lateinit var timerPlugin: TimerPlugin @Inject lateinit var timerPlugin: TimerPlugin
@Inject lateinit var linkify: Linkify @Inject lateinit var linkify: Linkify
private val linkifyEnabled: Boolean
get() = preferences.getBoolean(R.string.p_linkify_task_edit, false)
val editViewModel: TaskEditViewModel by viewModels() val editViewModel: TaskEditViewModel by viewModels()
lateinit var binding: FragmentTaskEditBinding lateinit var binding: FragmentTaskEditBinding
private var showKeyboard = false private var showKeyboard = false
@ -144,7 +147,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
val title = binding.title val title = binding.title
val markdown = if (preferences.markdown) { val markdown = if (preferences.markdown) {
MarkwonEditorTextWatcher.withPreRender( MarkwonEditorTextWatcher.withPreRender(
MarkwonEditor.create(requireContext().markwon), MarkwonEditor.create(requireContext().markwon(linkifyEnabled)),
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
title title
) )
@ -200,7 +203,7 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener {
lifecycleScope.launch { lifecycleScope.launch {
notificationManager.cancel(model.id) notificationManager.cancel(model.id)
} }
if (preferences.getBoolean(R.string.p_linkify_task_edit, false)) { if (linkifyEnabled) {
linkify.linkify(title) linkify.linkify(title)
} }
} }

@ -38,7 +38,7 @@ class WhatsNewDialog : DialogFragment() {
val textStream = requireContext().assets.open("CHANGELOG.md") val textStream = requireContext().assets.open("CHANGELOG.md")
val text = BufferedReader(textStream.reader()).readText() val text = BufferedReader(textStream.reader()).readText()
binding.changelog.movementMethod = LinkMovementMethod.getInstance() binding.changelog.movementMethod = LinkMovementMethod.getInstance()
requireContext().markwon.setMarkdown(binding.changelog, text) requireContext().markwon(true).setMarkdown(binding.changelog, text)
val begForSubscription = !inventory.hasPro val begForSubscription = !inventory.hasPro
val begForRating = !preferences.getBoolean(R.string.p_clicked_rate, false) val begForRating = !preferences.getBoolean(R.string.p_clicked_rate, false)

@ -5,7 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.Intent.ACTION_VIEW import android.content.Intent.ACTION_VIEW
import android.net.Uri import android.net.Uri
import android.text.util.Linkify import android.text.util.Linkify.*
import android.widget.Toast import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent import androidx.browser.customtabs.CustomTabsIntent
import io.noties.markwon.Markwon import io.noties.markwon.Markwon
@ -53,20 +53,21 @@ object Context {
fun Context.toast(text: String?, duration: Int = Toast.LENGTH_LONG) = fun Context.toast(text: String?, duration: Int = Toast.LENGTH_LONG) =
text?.let { Toast.makeText(this, it, duration).show() } text?.let { Toast.makeText(this, it, duration).show() }
val Context.markwon: Markwon fun Context.markwon(linkify: Boolean = false): Markwon {
get() = val builder = Markwon
Markwon
.builder(this) .builder(this)
.usePlugins( .usePlugins(
listOf( listOf(
TaskListPlugin.create(this), TaskListPlugin.create(this),
TablePlugin.create(this), TablePlugin.create(this),
LinkifyPlugin.create(
Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES or Linkify.PHONE_NUMBERS,
true
),
StrikethroughPlugin.create() StrikethroughPlugin.create()
) )
) )
.build() if (linkify) {
builder.usePlugin(
LinkifyPlugin.create(WEB_URLS or EMAIL_ADDRESSES or PHONE_NUMBERS, true)
)
}
return builder.build()
}
} }

@ -46,7 +46,7 @@ class TaskViewHolder internal constructor(
private val linkify: Linkify, private val linkify: Linkify,
private val locale: Locale private val locale: Locale
) : RecyclerView.ViewHolder(binding.root) { ) : RecyclerView.ViewHolder(binding.root) {
private val markwon = if (preferences.markdown) context.markwon else null private val markwon = if (preferences.markdown) context.markwon(linkifyEnabled) else null
private val row: ViewGroup = binding.row private val row: ViewGroup = binding.row
private val dueDate: TextView = binding.dueDate.apply { private val dueDate: TextView = binding.dueDate.apply {
setOnClickListener { changeDueDate() } setOnClickListener { changeDueDate() }
@ -64,6 +64,9 @@ class TaskViewHolder internal constructor(
lateinit var task: TaskContainer lateinit var task: TaskContainer
private val linkifyEnabled: Boolean
get() = preferences.getBoolean(R.string.p_linkify_task_list, false)
var indent = 0 var indent = 0
set(value) { set(value) {
field = value field = value
@ -146,7 +149,7 @@ class TaskViewHolder internal constructor(
} }
description.visibility = if (task.hasNotes()) View.VISIBLE else View.GONE description.visibility = if (task.hasNotes()) View.VISIBLE else View.GONE
} }
if (preferences.getBoolean(R.string.p_linkify_task_list, false)) { if (markwon != null || preferences.getBoolean(R.string.p_linkify_task_list, false)) {
linkify.setMovementMethod(nameView) { onRowBodyClick() } linkify.setMovementMethod(nameView) { onRowBodyClick() }
linkify.setMovementMethod(description) { onRowBodyClick() } linkify.setMovementMethod(description) { onRowBodyClick() }
if (markwon == null) { if (markwon == null) {

@ -22,9 +22,12 @@ class DescriptionControlSet : TaskEditControlFragment() {
private lateinit var editText: EditText private lateinit var editText: EditText
private val linkifyEnabled: Boolean
get() = preferences.getBoolean(R.string.p_linkify_task_edit, false)
override fun createView(savedInstanceState: Bundle?) { override fun createView(savedInstanceState: Bundle?) {
viewModel.description?.let(editText::setTextKeepState) viewModel.description?.let(editText::setTextKeepState)
if (preferences.getBoolean(R.string.p_linkify_task_edit, false)) { if (linkifyEnabled) {
linkify.linkify(editText) linkify.linkify(editText)
} }
} }
@ -34,7 +37,7 @@ class DescriptionControlSet : TaskEditControlFragment() {
editText = it.notes editText = it.notes
val markdown = if (preferences.markdown) { val markdown = if (preferences.markdown) {
MarkwonEditorTextWatcher.withPreRender( MarkwonEditorTextWatcher.withPreRender(
MarkwonEditor.create(requireContext().markwon), MarkwonEditor.create(requireContext().markwon(linkifyEnabled)),
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
editText editText
) )

Loading…
Cancel
Save