Skip to content

Commit 20894dd

Browse files
committed
trim note title
1 parent ae2b03a commit 20894dd

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

  • shared/src/commonMain/kotlin/com/softartdev/notedelight/shared/presentation/note

shared/src/commonMain/kotlin/com/softartdev/notedelight/shared/presentation/note/NoteViewModel.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,19 @@ class NoteViewModel(
106106
private fun createTitleIfNeed(title: String?, text: String) =
107107
if (title.isNullOrEmpty()) createTitle(text) else title
108108

109-
//TODO trim '\n'
110109
private fun createTitle(text: String): String {
111-
// Get the note's length
112-
val length = text.length
110+
var title = text.substring(0, 30.coerceAtMost(text.length))
113111

114-
// Sets the title by getting a substring of the text that is 31 characters long
115-
// or the number of characters in the note plus one, whichever is smaller.
116-
var title = text.substring(0, 30.coerceAtMost(length))
117-
118-
// If the resulting length is more than 30 characters, chops off any
119-
// trailing spaces
120-
if (length > 30) {
112+
if (text.length > 30) {
121113
val lastSpace: Int = title.lastIndexOf(' ')
122114
if (lastSpace > 0) {
123115
title = title.substring(0, lastSpace)
124116
}
125117
}
118+
val firstLine = title.indexOf('\n')
119+
if (firstLine > 0) {
120+
title = title.substring(0, firstLine)
121+
}
126122
return title
127123
}
128124

0 commit comments

Comments
 (0)