1
1
package com.omgodse.notally.miscellaneous
2
2
3
- import android.content.Context
4
3
import android.graphics.Typeface
5
- import android.os.Build
6
4
import android.text.Editable
7
5
import android.text.InputType
8
6
import android.text.Spannable
@@ -11,55 +9,42 @@ import android.text.style.StrikethroughSpan
11
9
import android.text.style.StyleSpan
12
10
import android.text.style.TypefaceSpan
13
11
import android.text.style.URLSpan
14
- import android.view.*
12
+ import android.view.KeyEvent
13
+ import android.view.Menu
14
+ import android.view.MenuItem
15
15
import android.view.inputmethod.EditorInfo
16
16
import android.widget.EditText
17
- import com.google.android.material.chip.ChipGroup
18
17
import com.omgodse.notally.activities.TakeNote
19
- import com.omgodse.notally.databinding.LabelBinding
20
- import com.omgodse.notally.room.ListItem
21
18
import com.omgodse.notally.room.SpanRepresentation
22
- import java.util.*
23
-
24
- fun List<ListItem>?.getBody () = buildString {
25
- this @getBody?.forEachIndexed { index, (body) ->
26
- appendLine(" ${(index + 1 )} ) $body " )
27
- }
28
- }
29
-
30
-
31
- fun ChipGroup.bindLabels (labels : HashSet <String >) {
32
- if (labels.isEmpty()) {
33
- visibility = View .GONE
34
- } else {
35
- visibility = View .VISIBLE
36
- removeAllViews()
37
- val inflater = LayoutInflater .from(context)
38
- for (label in labels) {
39
- val view = LabelBinding .inflate(inflater, this , true ).root
40
- view.text = label
41
- }
42
- }
43
- }
44
19
20
+ /* *
21
+ * For some reason, this method crashes sometimes with an
22
+ * IndexOutOfBoundsException that I've not been able to replicate.
23
+ * When this happens, to prevent the entire app from crashing and becoming
24
+ * unusable, the exception is suppressed.
25
+ */
45
26
fun String.applySpans (representations : List <SpanRepresentation >): Editable {
46
27
val editable = Editable .Factory .getInstance().newEditable(this )
47
28
representations.forEach { (bold, link, italic, monospace, strikethrough, start, end) ->
48
- if (bold) {
49
- editable.setSpan(StyleSpan (Typeface .BOLD ), start, end)
50
- }
51
- if (italic) {
52
- editable.setSpan(StyleSpan (Typeface .ITALIC ), start, end)
53
- }
54
- if (link) {
55
- val url = getURL(start, end)
56
- editable.setSpan(URLSpan (url), start, end)
57
- }
58
- if (monospace) {
59
- editable.setSpan(TypefaceSpan (" monospace" ), start, end)
60
- }
61
- if (strikethrough) {
62
- editable.setSpan(StrikethroughSpan (), start, end)
29
+ try {
30
+ if (bold) {
31
+ editable.setSpan(StyleSpan (Typeface .BOLD ), start, end)
32
+ }
33
+ if (italic) {
34
+ editable.setSpan(StyleSpan (Typeface .ITALIC ), start, end)
35
+ }
36
+ if (link) {
37
+ val url = getURL(start, end)
38
+ editable.setSpan(URLSpan (url), start, end)
39
+ }
40
+ if (monospace) {
41
+ editable.setSpan(TypefaceSpan (" monospace" ), start, end)
42
+ }
43
+ if (strikethrough) {
44
+ editable.setSpan(StrikethroughSpan (), start, end)
45
+ }
46
+ } catch (exception: Exception ) {
47
+ exception.printStackTrace()
63
48
}
64
49
}
65
50
return editable
@@ -72,29 +57,13 @@ private fun String.getURL(start: Int, end: Int): String {
72
57
} else TakeNote .getURLFrom(substring(start, length))
73
58
}
74
59
75
- /* *
76
- * For some reason, this method crashes sometimes with an
77
- * IndexOutOfBoundsException that I've not been able to replicate.
78
- * When this happens, to prevent the entire app from crashing and becoming
79
- * unusable, the exception is suppressed.
80
- */
81
60
private fun Spannable.setSpan (span : Any , start : Int , end : Int ) {
82
- try {
83
- if (end <= length) {
84
- setSpan(span, start, end, Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
85
- } else setSpan(span, start, length, Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
86
- } catch (exception: Exception ) {
87
- exception.printStackTrace()
88
- }
61
+ if (end <= length) {
62
+ setSpan(span, start, end, Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
63
+ } else setSpan(span, start, length, Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
89
64
}
90
65
91
66
92
- fun Context.getLocale (): Locale {
93
- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
94
- resources.configuration.locales[0 ]
95
- } else resources.configuration.locale
96
- }
97
-
98
67
fun EditText.setOnNextAction (onNext : () -> Unit ) {
99
68
setRawInputType(InputType .TYPE_CLASS_TEXT or InputType .TYPE_TEXT_FLAG_CAP_SENTENCES )
100
69
0 commit comments