Skip to content

Commit

Permalink
fix #136, make sure swiftkey arrows work well
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed Apr 5, 2018
1 parent 153fd81 commit 696858e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.TypedValue
import android.view.Gravity
Expand All @@ -18,10 +17,7 @@ import com.simplemobiletools.commons.extensions.onGlobalLayout
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.MainActivity
import com.simplemobiletools.notes.extensions.*
import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
import com.simplemobiletools.notes.helpers.NOTE_ID
import com.simplemobiletools.notes.helpers.*
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.fragment_note.*
import kotlinx.android.synthetic.main.fragment_note.view.*
Expand All @@ -47,7 +43,7 @@ class NoteFragment : Fragment() {
view.notes_view.apply {
linksClickable = true
autoLinkMask = Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES
movementMethod = LinkMovementMethod.getInstance()
movementMethod = MyMovementMethod.getInstance()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.simplemobiletools.notes.helpers

import android.text.Selection
import android.text.Spannable
import android.text.method.ArrowKeyMovementMethod
import android.text.style.ClickableSpan
import android.view.MotionEvent
import android.widget.TextView

class MyMovementMethod : ArrowKeyMovementMethod() {
companion object {
private var sInstance: MyMovementMethod? = null

fun getInstance(): MyMovementMethod {
if (sInstance == null) {
sInstance = MyMovementMethod()
}
return sInstance!!
}
}

override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
val action = event.action

if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
var x = event.x.toInt()
var y = event.y.toInt()

x -= widget.totalPaddingLeft
y -= widget.totalPaddingTop

x += widget.scrollX
y += widget.scrollY

val layout = widget.layout
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())

val links = buffer.getSpans(off, off, ClickableSpan::class.java)
if (links.isNotEmpty()) {
if (action == MotionEvent.ACTION_UP) {
links[0].onClick(widget)
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(links[0]),
buffer.getSpanEnd(links[0]))
}
return true
}
}

return super.onTouchEvent(widget, buffer, event)
}
}

0 comments on commit 696858e

Please sign in to comment.