Skip to content

Commit 696858e

Browse files
committed
fix #136, make sure swiftkey arrows work well
1 parent 153fd81 commit 696858e

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.os.Bundle
55
import android.support.v4.app.Fragment
66
import android.text.Editable
77
import android.text.TextWatcher
8-
import android.text.method.LinkMovementMethod
98
import android.text.util.Linkify
109
import android.util.TypedValue
1110
import android.view.Gravity
@@ -18,10 +17,7 @@ import com.simplemobiletools.commons.extensions.onGlobalLayout
1817
import com.simplemobiletools.notes.R
1918
import com.simplemobiletools.notes.activities.MainActivity
2019
import com.simplemobiletools.notes.extensions.*
21-
import com.simplemobiletools.notes.helpers.DBHelper
22-
import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
23-
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
24-
import com.simplemobiletools.notes.helpers.NOTE_ID
20+
import com.simplemobiletools.notes.helpers.*
2521
import com.simplemobiletools.notes.models.Note
2622
import kotlinx.android.synthetic.main.fragment_note.*
2723
import kotlinx.android.synthetic.main.fragment_note.view.*
@@ -47,7 +43,7 @@ class NoteFragment : Fragment() {
4743
view.notes_view.apply {
4844
linksClickable = true
4945
autoLinkMask = Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES
50-
movementMethod = LinkMovementMethod.getInstance()
46+
movementMethod = MyMovementMethod.getInstance()
5147
}
5248
}
5349

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.simplemobiletools.notes.helpers
2+
3+
import android.text.Selection
4+
import android.text.Spannable
5+
import android.text.method.ArrowKeyMovementMethod
6+
import android.text.style.ClickableSpan
7+
import android.view.MotionEvent
8+
import android.widget.TextView
9+
10+
class MyMovementMethod : ArrowKeyMovementMethod() {
11+
companion object {
12+
private var sInstance: MyMovementMethod? = null
13+
14+
fun getInstance(): MyMovementMethod {
15+
if (sInstance == null) {
16+
sInstance = MyMovementMethod()
17+
}
18+
return sInstance!!
19+
}
20+
}
21+
22+
override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
23+
val action = event.action
24+
25+
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
26+
var x = event.x.toInt()
27+
var y = event.y.toInt()
28+
29+
x -= widget.totalPaddingLeft
30+
y -= widget.totalPaddingTop
31+
32+
x += widget.scrollX
33+
y += widget.scrollY
34+
35+
val layout = widget.layout
36+
val line = layout.getLineForVertical(y)
37+
val off = layout.getOffsetForHorizontal(line, x.toFloat())
38+
39+
val links = buffer.getSpans(off, off, ClickableSpan::class.java)
40+
if (links.isNotEmpty()) {
41+
if (action == MotionEvent.ACTION_UP) {
42+
links[0].onClick(widget)
43+
} else if (action == MotionEvent.ACTION_DOWN) {
44+
Selection.setSelection(buffer,
45+
buffer.getSpanStart(links[0]),
46+
buffer.getSpanEnd(links[0]))
47+
}
48+
return true
49+
}
50+
}
51+
52+
return super.onTouchEvent(widget, buffer, event)
53+
}
54+
}

0 commit comments

Comments
 (0)