Skip to content

Commit 33d1425

Browse files
authored
Merge pull request #105 from Aga-C/fix-multiline-pasting
Fixed multiline pasting for checklist (#99)
2 parents 527277e + 241b5c0 commit 33d1425

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

app/src/main/kotlin/org/fossify/notes/dialogs/NewChecklistItemDialog.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,39 @@ class NewChecklistItemDialog(val activity: Activity, callback: (titles: ArrayLis
5757
}
5858
}
5959

60-
private fun addNewEditText() {
60+
private fun addNewEditText(initialText: String? = null, position: Int? = null) {
6161
ItemAddChecklistBinding.inflate(activity.layoutInflater).apply {
6262
titleEditText.setOnEditorActionListener { _, actionId, _ ->
6363
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE || actionId == KeyEvent.KEYCODE_ENTER) {
64-
addNewEditText()
64+
addNewEditText(position = titles.indexOf(titleEditText) + 1)
6565
true
6666
} else {
6767
false
6868
}
6969
}
70-
titles.add(titleEditText)
71-
binding.checklistHolder.addView(this.root)
70+
titleEditText.onTextChangeListener { text ->
71+
val lines = text.lines().filter { it.trim().isNotEmpty() }
72+
if (lines.size > 1) {
73+
val currentPosition = titles.indexOf(titleEditText)
74+
lines.forEachIndexed { i, line ->
75+
if (i == 0) {
76+
titleEditText.setText(line)
77+
} else {
78+
addNewEditText(line, currentPosition + i)
79+
}
80+
}
81+
}
82+
}
83+
if (initialText != null) {
84+
titleEditText.append(initialText)
85+
}
86+
if (position != null && position < titles.size) {
87+
titles.add(position, titleEditText)
88+
binding.checklistHolder.addView(this.root, position)
89+
} else {
90+
titles.add(titleEditText)
91+
binding.checklistHolder.addView(this.root)
92+
}
7293
activity.updateTextColors(binding.checklistHolder)
7394
binding.dialogHolder.post {
7495
binding.dialogHolder.fullScroll(View.FOCUS_DOWN)

0 commit comments

Comments
 (0)