Skip to content

Commit 19e9a43

Browse files
authored
feat: allow to delete the last note (#174)
* feat: allow to delete the last note * fix: avoid inserting a note with null id * fix: remove unused import * fix: remove unnecessary call to getNotes * fix: avoid wildcard imports * feat: only display "delete note" menu option if the current note is not the empty default one * fix: use getCurrentNoteViewText instead of value to check if note has text * feat: allow deleting default empty note if more than 1 note exist * docs(changelog): added feature to CHANGELOG.md * style: break long line in two * fix: refresh menu items when note was empty and is not anymore, or when it wasn't and now is
1 parent dea7fdc commit 19e9a43

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9+
- Option to delete the last open note ([#157])
910
- Option to uncheck all checked items ([#156])
1011

1112
## [1.3.1] - 2025-07-12
@@ -64,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6465
[#99]: https://github.com/FossifyOrg/Notes/issues/99
6566
[#110]: https://github.com/FossifyOrg/Notes/issues/110
6667
[#156]: https://github.com/FossifyOrg/Notes/issues/156
68+
[#157]: https://github.com/FossifyOrg/Notes/issues/157
6769
[#164]: https://github.com/FossifyOrg/Notes/issues/164
6870
[#178]: https://github.com/FossifyOrg/Notes/issues/178
6971

app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class MainActivity : SimpleActivity() {
147147
private var searchIndex = 0
148148
private var searchMatches = emptyList<Int>()
149149
private var isSearchActive = false
150+
private var wasNoteTextEmpty: Boolean = false
150151

151152
private lateinit var searchQueryET: MyEditText
152153
private lateinit var searchPrevBtn: ImageView
@@ -252,6 +253,7 @@ class MainActivity : SimpleActivity() {
252253
private fun refreshMenuItems() {
253254
val multipleNotesExist = mNotes.size > 1
254255
val isCurrentItemChecklist = isCurrentItemChecklist()
256+
val isDefaultEmptyNote = isDefaultEmptyNote()
255257

256258
binding.mainToolbar.menu.apply {
257259
findItem(R.id.undo).apply {
@@ -266,7 +268,6 @@ class MainActivity : SimpleActivity() {
266268

267269
findItem(R.id.rename_note).isVisible = multipleNotesExist
268270
findItem(R.id.open_note).isVisible = multipleNotesExist
269-
findItem(R.id.delete_note).isVisible = multipleNotesExist
270271
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist
271272
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist
272273
findItem(R.id.uncheck_all_items).isVisible = isCurrentItemChecklist
@@ -278,6 +279,7 @@ class MainActivity : SimpleActivity() {
278279
mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
279280
findItem(R.id.more_apps_from_us).isVisible =
280281
!resources.getBoolean(org.fossify.commons.R.bool.hide_google_relations)
282+
findItem(R.id.delete_note).isVisible = !isDefaultEmptyNote || mNotes.size > 1
281283

282284
saveNoteButton = findItem(R.id.save_note)
283285
saveNoteButton!!.isVisible =
@@ -399,6 +401,16 @@ class MainActivity : SimpleActivity() {
399401
}
400402
}
401403

404+
private fun isDefaultEmptyNote(): Boolean {
405+
return if (::mCurrentNote.isInitialized) {
406+
(mCurrentNote.title == getString(R.string.general_note) &&
407+
getCurrentNoteText().isNullOrEmpty() &&
408+
mCurrentNote.value.isEmpty())
409+
} else {
410+
false
411+
}
412+
}
413+
402414
@SuppressLint("NewApi")
403415
private fun checkShortcuts() {
404416
val appIconColor = config.appIconColor
@@ -557,6 +569,7 @@ class MainActivity : SimpleActivity() {
557569

558570
mNotes = notes
559571
mCurrentNote = mNotes[0]
572+
wasNoteTextEmpty = mCurrentNote.value.isEmpty()
560573
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this)
561574
binding.viewPager.apply {
562575
adapter = mAdapter
@@ -1339,7 +1352,7 @@ class MainActivity : SimpleActivity() {
13391352
}
13401353

13411354
fun deleteNote(deleteFile: Boolean, note: Note) {
1342-
if (mNotes.size <= 1 || note != mCurrentNote) {
1355+
if (mNotes.isEmpty() || note != mCurrentNote) {
13431356
return
13441357
}
13451358

@@ -1360,8 +1373,12 @@ class MainActivity : SimpleActivity() {
13601373
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
13611374
ensureBackgroundThread {
13621375
val currentNoteIndex = mNotes.indexOf(note)
1363-
val noteToRefresh =
1376+
1377+
val noteToRefresh = if (mNotes.size == 1) {
1378+
null
1379+
} else {
13641380
mNotes[if (currentNoteIndex > 0) currentNoteIndex - 1 else currentNoteIndex + 1]
1381+
}
13651382

13661383
notesDB.deleteNote(note)
13671384
widgetsDB.deleteNoteWidgets(note.id!!)
@@ -1370,20 +1387,21 @@ class MainActivity : SimpleActivity() {
13701387
}
13711388
}
13721389

1373-
private fun refreshNotes(note: Note, deleteFile: Boolean) {
1390+
private fun refreshNotes(note: Note?, deleteFile: Boolean) {
13741391
NotesHelper(this).getNotes {
13751392
mNotes = it
1376-
val noteId = note.id
1393+
val currentNote = note ?: mNotes[0]
1394+
val noteId = currentNote.id
13771395
updateSelectedNote(noteId!!)
1378-
if (config.widgetNoteId == note.id) {
1396+
if (config.widgetNoteId == currentNote.id) {
13791397
config.widgetNoteId = mCurrentNote.id!!
13801398
updateWidgets()
13811399
}
13821400

13831401
initViewPager()
13841402

13851403
if (deleteFile) {
1386-
deleteFile(FileDirItem(note.path, note.title)) {
1404+
deleteFile(FileDirItem(currentNote.path, currentNote.title)) {
13871405
if (!it) {
13881406
toast(org.fossify.commons.R.string.unknown_error_occurred)
13891407
}
@@ -1541,6 +1559,14 @@ class MainActivity : SimpleActivity() {
15411559
}
15421560
}
15431561

1562+
if (getCurrentNoteText().isNullOrEmpty()) {
1563+
wasNoteTextEmpty = true
1564+
shouldRecreateMenu = true
1565+
} else if (wasNoteTextEmpty) {
1566+
wasNoteTextEmpty = false
1567+
shouldRecreateMenu = true
1568+
}
1569+
15441570
if (shouldRecreateMenu) {
15451571
refreshMenuItems()
15461572
}

app/src/main/kotlin/org/fossify/notes/helpers/NotesHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.fossify.notes.helpers
33
import android.content.Context
44
import android.os.Handler
55
import android.os.Looper
6-
import kotlinx.serialization.encodeToString
76
import kotlinx.serialization.json.Json
87
import org.fossify.commons.activities.BaseSimpleActivity
98
import org.fossify.commons.helpers.ExportResult
@@ -42,7 +41,8 @@ class NotesHelper(val context: Context) {
4241
if (notes.isEmpty()) {
4342
val generalNote = context.resources.getString(R.string.general_note)
4443
val note = Note(null, generalNote, "", NoteType.TYPE_TEXT, "", PROTECTION_NONE, "")
45-
context.notesDB.insertOrUpdate(note)
44+
val insertedId = context.notesDB.insertOrUpdate(note)
45+
note.id = insertedId
4646
notes.add(note)
4747
}
4848

0 commit comments

Comments
 (0)