Skip to content

Commit 0811d58

Browse files
committed
feat: add setting to ask for the name of new notes
When enabled, the note list's create button asks for the new note's name. The field starts with the name the note would otherwise get: the dated default, or the search text when creating from search. The new settings row pushes the diagnostics button below the settings dialog's visible area, so the conflict device test now scrolls to it.
1 parent b21daa4 commit 0811d58

8 files changed

Lines changed: 201 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1616
stays off, with an explanation, when the notes folder has no `notes.sqlite`.
1717
- Opening a note while the note list search is active now opens "Find in note" with the same
1818
text, highlighting the matches and scrolling to the first one without showing the keyboard.
19+
- An "Ask for name of new notes" setting, off by default, makes the note list's create button ask
20+
for the new note's name first. The field starts with the name the note would otherwise get, the
21+
dated default or the search text, so accepting it keeps the previous behavior.
1922

2023
## [0.10.0] - 2026-09-28
2124

‎app/src/androidTest/kotlin/org/qownnotes/mobile/AppLaunchTest.kt‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,40 @@ class AppLaunchTest {
12101210
}
12111211
}
12121212

1213+
@Test
1214+
fun askingForANewNoteNameSuggestsTheAutomaticNameAndUsesTheChosenOne() {
1215+
importAccount("alice", "Existing note", "etag-1", 10)
1216+
1217+
listAction("settings")
1218+
composeRule.onNodeWithTag("toggle-ask-for-new-note-name").performScrollTo().assertIsOff()
1219+
.performClick()
1220+
composeRule.onNodeWithTag("toggle-ask-for-new-note-name").assertIsOn()
1221+
composeRule.onNodeWithTag("close-settings").performClick()
1222+
1223+
composeRule.onNodeWithTag("create-note").performClick()
1224+
composeRule.waitForTag("new-note-name-field")
1225+
composeRule.onNodeWithTag(
1226+
"new-note-name-field"
1227+
).assertTextContains("Note ", substring = true)
1228+
composeRule.onNodeWithTag("cancel-new-note").performClick()
1229+
composeRule.waitUntil(timeoutMillis = 10_000) {
1230+
composeRule.onAllNodesWithTag("new-note-name-dialog").fetchSemanticsNodes().isEmpty()
1231+
}
1232+
assertEquals(1, runBlocking { notesOf("alice").size })
1233+
1234+
composeRule.onNodeWithTag("create-note").performClick()
1235+
composeRule.waitForTag("new-note-name-field")
1236+
// The suggested name is selected, so typing replaces it.
1237+
composeRule.onNodeWithTag("new-note-name-field").performTextInput("Meeting: Monday")
1238+
composeRule.onNodeWithTag("confirm-new-note").performClick()
1239+
1240+
composeRule.waitForTag("markdown-editor")
1241+
onView(withId(R.id.markdown_editor)).check { view, _ ->
1242+
assertEquals("# Meeting Monday\n\n", (view as TextView).text.toString())
1243+
}
1244+
assertTrue(runBlocking { notesOf("alice").any { it.title == "Meeting Monday" } })
1245+
}
1246+
12131247
@Test
12141248
fun creationAdoptsTheCanonicalServerTitle() {
12151249
val account = importAccount("alice", "Existing note", "etag-1", 10)
@@ -1257,7 +1291,7 @@ class AppLaunchTest {
12571291
assertEquals("# Existing note\n\nBase content", conflicted.lastSyncedContent)
12581292

12591293
listAction("settings")
1260-
composeRule.onNodeWithTag("open-diagnostics").performClick()
1294+
composeRule.onNodeWithTag("open-diagnostics").performScrollTo().performClick()
12611295
composeRule.waitForText("Category: Conflict", substring = true)
12621296
}
12631297

‎app/src/androidTest/kotlin/org/qownnotes/mobile/TestQOwnNotesApplication.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TestQOwnNotesApplication : QOwnNotesApplication() {
8484
component.settings.setSwipeNoteActions(false)
8585
component.settings.setHideCreateButtonOnScroll(true)
8686
component.settings.setCompactNoteList(false)
87+
component.settings.setAskForNewNoteName(false)
8788
component.settings.setAppearance(AppAppearance())
8889
fakeBackend.reset()
8990
fakeAccountImporter.reset()

‎app/src/main/kotlin/org/qownnotes/mobile/AppSettings.kt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ class AppSettings(context: Context, name: String = PREFERENCES) {
7777
mutableHideCreateButtonOnScroll.value = enabled
7878
}
7979

80+
private val mutableAskForNewNoteName =
81+
MutableStateFlow(preferences.getBoolean(ASK_FOR_NEW_NOTE_NAME, false))
82+
83+
/**
84+
* Whether the note list's create button first asks for the new note's name, offering the
85+
* name the note would otherwise have been given.
86+
*/
87+
val askForNewNoteName: StateFlow<Boolean> = mutableAskForNewNoteName.asStateFlow()
88+
89+
fun setAskForNewNoteName(enabled: Boolean) {
90+
if (enabled == mutableAskForNewNoteName.value) return
91+
preferences.edit().putBoolean(ASK_FOR_NEW_NOTE_NAME, enabled).apply()
92+
mutableAskForNewNoteName.value = enabled
93+
}
94+
8095
private val mutableCompactNoteList =
8196
MutableStateFlow(preferences.getBoolean(COMPACT_NOTE_LIST, false))
8297

@@ -215,6 +230,7 @@ class AppSettings(context: Context, name: String = PREFERENCES) {
215230
const val SHOW_NOTE_PREVIEW = "showNotePreview"
216231
const val SWIPE_NOTE_ACTIONS = "swipeNoteActions"
217232
const val HIDE_CREATE_BUTTON_ON_SCROLL = "hideCreateButtonOnScroll"
233+
const val ASK_FOR_NEW_NOTE_NAME = "askForNewNoteName"
218234
const val COMPACT_NOTE_LIST = "compactNoteList"
219235
const val HEADER_COLOR = "headerColor"
220236
const val NOTE_BACKGROUND_COLOR = "noteBackgroundColor"

‎app/src/main/kotlin/org/qownnotes/mobile/MainActivity.kt‎

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import androidx.compose.foundation.selection.toggleable
5151
import androidx.compose.foundation.shape.CircleShape
5252
import androidx.compose.foundation.shape.RoundedCornerShape
5353
import androidx.compose.foundation.text.BasicTextField
54+
import androidx.compose.foundation.text.KeyboardActions
55+
import androidx.compose.foundation.text.KeyboardOptions
5456
import androidx.compose.foundation.text.selection.SelectionContainer
5557
import androidx.compose.foundation.verticalScroll
5658
import androidx.compose.material.icons.Icons
@@ -155,6 +157,7 @@ import androidx.compose.ui.semantics.selected
155157
import androidx.compose.ui.semantics.semantics
156158
import androidx.compose.ui.text.TextRange
157159
import androidx.compose.ui.text.font.FontFamily
160+
import androidx.compose.ui.text.input.ImeAction
158161
import androidx.compose.ui.text.input.TextFieldValue
159162
import androidx.compose.ui.text.style.TextOverflow
160163
import androidx.compose.ui.unit.IntOffset
@@ -551,12 +554,12 @@ private fun NotesNavigation(
551554
onReconnectAccount = onReconnectAccount,
552555
onManageAccounts = { managingAccounts = true },
553556
onOpenBookmarks = { browsingBookmarks = true },
554-
onCreate = { accountId, category, searchText ->
557+
onCreate = { accountId, category, name ->
555558
scope.launch {
556-
val note = if (searchText == null) {
559+
val note = if (name == null) {
557560
component.createNote(accountId, category)
558561
} else {
559-
component.createNoteFromSearch(accountId, searchText, category)
562+
component.createNamedNote(accountId, name, category)
560563
}
561564
noteHistory = emptyList()
562565
selectedNoteId = note.localId
@@ -866,6 +869,7 @@ private fun NoteListScreen(
866869
onReconnectAccount: (String) -> Unit,
867870
onManageAccounts: () -> Unit,
868871
onOpenBookmarks: () -> Unit,
872+
/** Creates a note in an account and category, with the given name or an automatic one. */
869873
onCreate: (String, String, String?) -> Unit,
870874
/** Opens a note with the active search text, if any, so the note can find it too. */
871875
onOpen: (String, String?) -> Unit
@@ -939,6 +943,12 @@ private fun NoteListScreen(
939943
.collectAsStateWithLifecycle(context = UiDispatcher)
940944
val compactNoteList by component.settings.compactNoteList
941945
.collectAsStateWithLifecycle(context = UiDispatcher)
946+
val askForNewNoteName by component.settings.askForNewNoteName
947+
.collectAsStateWithLifecycle(context = UiDispatcher)
948+
// Visibility is kept apart from the name because the field may report a last value change
949+
// while the dialog closes, which must not reopen it.
950+
var namingNewNote by rememberSaveable(accountId) { mutableStateOf(false) }
951+
var newNoteName by rememberSaveable(accountId) { mutableStateOf("") }
942952
val appearance by component.settings.appearance
943953
.collectAsStateWithLifecycle(context = UiDispatcher)
944954
val headerContainer =
@@ -1005,9 +1015,19 @@ private fun NoteListScreen(
10051015
previousOffset = offset
10061016
}
10071017
}
1008-
val createNote = {
1018+
val createNamedNote = { name: String? ->
10091019
val category = (categoryScope as? NoteCategoryScope.Category)?.value.orEmpty()
1010-
onCreate(accountId, category, query.takeIf(NoteNames::isValid))
1020+
onCreate(accountId, category, name)
1021+
}
1022+
val createNote = {
1023+
val searchName = query.takeIf(NoteNames::isValid)
1024+
if (askForNewNoteName) {
1025+
// Offer the name the note would have been given without asking.
1026+
newNoteName = component.defaultNoteName(searchName)
1027+
namingNewNote = true
1028+
} else {
1029+
createNamedNote(searchName)
1030+
}
10111031
}
10121032

10131033
LaunchedEffect(accountId) { withContext(UiDispatcher) { component.refresh(accountId) } }
@@ -1534,6 +1554,17 @@ private fun NoteListScreen(
15341554
onDismiss = { tagFilterOpen = false }
15351555
)
15361556
}
1557+
if (namingNewNote) {
1558+
NewNoteNameDialog(
1559+
name = newNoteName,
1560+
onNameChange = { newNoteName = it },
1561+
onDismiss = { namingNewNote = false },
1562+
onConfirm = {
1563+
namingNewNote = false
1564+
createNamedNote(newNoteName)
1565+
}
1566+
)
1567+
}
15371568
if (showSettings) {
15381569
AlertDialog(
15391570
onDismissRequest = { showSettings = false },
@@ -1576,6 +1607,13 @@ private fun NoteListScreen(
15761607
onCheckedChange = component.settings::setHideCreateButtonOnScroll,
15771608
testTag = "toggle-hide-create-button-on-scroll"
15781609
)
1610+
SettingsCheckbox(
1611+
label = "Ask for name of new notes",
1612+
description = "Suggest the automatic name and let you change it.",
1613+
checked = askForNewNoteName,
1614+
onCheckedChange = component.settings::setAskForNewNoteName,
1615+
testTag = "toggle-ask-for-new-note-name"
1616+
)
15791617
OutlinedTextField(
15801618
value = bookmarksPath,
15811619
onValueChange = { component.settings.setBookmarksPath(accountId, it) },
@@ -4085,6 +4123,64 @@ private fun RenameNoteDialog(
40854123
)
40864124
}
40874125

4126+
/** Asks for a new note's name, starting from the name it would otherwise have been given. */
4127+
@Composable
4128+
private fun NewNoteNameDialog(
4129+
name: String,
4130+
onNameChange: (String) -> Unit,
4131+
onDismiss: () -> Unit,
4132+
onConfirm: () -> Unit
4133+
) {
4134+
val focusRequester = remember { FocusRequester() }
4135+
// The suggested name is selected, so typing replaces it and accepting keeps it.
4136+
var fieldValue by remember {
4137+
mutableStateOf(TextFieldValue(name, selection = TextRange(0, name.length)))
4138+
}
4139+
// Focus is taken once the field is placed in the dialog's window, as in RenameNoteDialog.
4140+
var fieldPlaced by remember { mutableStateOf(false) }
4141+
LaunchedEffect(fieldPlaced) {
4142+
if (fieldPlaced) focusRequester.requestFocus()
4143+
}
4144+
val valid = NoteNames.isValid(name)
4145+
AlertDialog(
4146+
onDismissRequest = onDismiss,
4147+
title = { Text("New note") },
4148+
text = {
4149+
OutlinedTextField(
4150+
value = fieldValue,
4151+
onValueChange = {
4152+
fieldValue = it
4153+
onNameChange(it.text)
4154+
},
4155+
label = { Text("Note name") },
4156+
singleLine = true,
4157+
supportingText = {
4158+
Text("Characters a file name cannot hold are replaced by spaces.")
4159+
},
4160+
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
4161+
keyboardActions = KeyboardActions(onDone = { if (valid) onConfirm() }),
4162+
modifier = Modifier.fillMaxWidth().focusRequester(focusRequester)
4163+
.onPlaced { fieldPlaced = true }
4164+
.testTag("new-note-name-field")
4165+
)
4166+
},
4167+
confirmButton = {
4168+
TextButton(
4169+
onClick = onConfirm,
4170+
enabled = valid,
4171+
modifier = Modifier.testTag("confirm-new-note")
4172+
) { Text("Create") }
4173+
},
4174+
dismissButton = {
4175+
TextButton(
4176+
onClick = onDismiss,
4177+
modifier = Modifier.testTag("cancel-new-note")
4178+
) { Text("Cancel") }
4179+
},
4180+
modifier = Modifier.testTag("new-note-name-dialog")
4181+
)
4182+
}
4183+
40884184
@Composable
40894185
private fun ChangeNoteCategoryDialog(
40904186
currentCategory: String,

‎app/src/main/kotlin/org/qownnotes/mobile/QOwnNotesApplication.kt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,12 @@ class ApplicationComponent(
448448
suspend fun createNote(accountId: String, category: String = ""): Note =
449449
persistNewNote(noteFactory.create(accountId, category))
450450

451-
suspend fun createNoteFromSearch(accountId: String, query: String, category: String): Note =
452-
persistNewNote(noteFactory.createFromSearch(accountId, query, category))
451+
/** The name a new note would get now, from the search text when it holds a usable name. */
452+
fun defaultNoteName(searchText: String? = null): String = noteFactory.defaultName(searchText)
453+
454+
/** Creates a note named by the search text or by the name the user was asked for. */
455+
suspend fun createNamedNote(accountId: String, name: String, category: String): Note =
456+
persistNewNote(noteFactory.createNamed(accountId, name, category))
453457

454458
/** Creates the note that text shared by another application is put into. */
455459
suspend fun createSharedNote(accountId: String, shared: SharedText): Note =

‎core/src/main/kotlin/org/qownnotes/mobile/core/NoteNaming.kt‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,21 @@ class NoteFactory(
6464
fun create(accountId: String, category: String = ""): Note =
6565
create(accountId, namingPolicy.createName(), body = "", category)
6666

67+
/**
68+
* The name [create] would give a note now, or the sanitized search text when that is usable,
69+
* which is the name [createFromSearch] would give it. Offered when asking for a new note's name.
70+
*/
71+
fun defaultName(query: String? = null): String =
72+
query?.let(NoteNames::sanitize)?.takeIf(String::isNotEmpty) ?: namingPolicy.createName()
73+
6774
/** Uses the search text as the new note's name in the currently selected category. */
68-
fun createFromSearch(accountId: String, query: String, category: String = ""): Note {
69-
val title = NoteNames.sanitize(query)
70-
require(title.isNotEmpty()) { "Search text must contain a usable note name" }
75+
fun createFromSearch(accountId: String, query: String, category: String = ""): Note =
76+
createNamed(accountId, query, category)
77+
78+
/** Creates an empty note with the given name, sanitized so a file can carry it. */
79+
fun createNamed(accountId: String, name: String, category: String = ""): Note {
80+
val title = NoteNames.sanitize(name)
81+
require(title.isNotEmpty()) { "Name must contain a usable note name" }
7182
return create(accountId, title, body = "", category)
7283
}
7384

‎core/src/test/kotlin/org/qownnotes/mobile/core/NoteFactoryTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ class NoteFactoryTest {
5050
}
5151
}
5252

53+
/** The name offered when asking is the name the note would get without asking. */
54+
@Test
55+
fun `default name matches the automatic name or the usable search text`() {
56+
val factory = factory()
57+
58+
assertEquals(factory.create("account").title, factory.defaultName())
59+
assertEquals("Note 2026-08-31 14h08s27", factory.defaultName(" / "))
60+
assertEquals(
61+
factory.createFromSearch("account", " Report: 2026/08 ").title,
62+
factory.defaultName(" Report: 2026/08 ")
63+
)
64+
}
65+
66+
@Test
67+
fun `creates a note with a chosen name in the selected category`() {
68+
val note = factory().createNamed("account", " Meeting: Monday ", "Work")
69+
70+
assertEquals("Meeting Monday", note.title)
71+
assertEquals("# Meeting Monday\n\n", note.content)
72+
assertEquals("Work", note.category)
73+
assertThrows(IllegalArgumentException::class.java) {
74+
factory().createNamed("account", " ")
75+
}
76+
}
77+
5378
@Test
5479
fun `shared text is named by the sharing application and kept under that heading`() {
5580
val note = factory().createFromSharedText(

0 commit comments

Comments
 (0)