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
This commit is contained in:
TopFox
2025-07-13 18:29:12 +02:00
committed by GitHub
parent dea7fdc17b
commit 19e9a43eca
3 changed files with 37 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Option to delete the last open note ([#157])
- Option to uncheck all checked items ([#156])
## [1.3.1] - 2025-07-12
@@ -64,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#99]: https://github.com/FossifyOrg/Notes/issues/99
[#110]: https://github.com/FossifyOrg/Notes/issues/110
[#156]: https://github.com/FossifyOrg/Notes/issues/156
[#157]: https://github.com/FossifyOrg/Notes/issues/157
[#164]: https://github.com/FossifyOrg/Notes/issues/164
[#178]: https://github.com/FossifyOrg/Notes/issues/178

View File

@@ -147,6 +147,7 @@ class MainActivity : SimpleActivity() {
private var searchIndex = 0
private var searchMatches = emptyList<Int>()
private var isSearchActive = false
private var wasNoteTextEmpty: Boolean = false
private lateinit var searchQueryET: MyEditText
private lateinit var searchPrevBtn: ImageView
@@ -252,6 +253,7 @@ class MainActivity : SimpleActivity() {
private fun refreshMenuItems() {
val multipleNotesExist = mNotes.size > 1
val isCurrentItemChecklist = isCurrentItemChecklist()
val isDefaultEmptyNote = isDefaultEmptyNote()
binding.mainToolbar.menu.apply {
findItem(R.id.undo).apply {
@@ -266,7 +268,6 @@ class MainActivity : SimpleActivity() {
findItem(R.id.rename_note).isVisible = multipleNotesExist
findItem(R.id.open_note).isVisible = multipleNotesExist
findItem(R.id.delete_note).isVisible = multipleNotesExist
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist
findItem(R.id.uncheck_all_items).isVisible = isCurrentItemChecklist
@@ -278,6 +279,7 @@ class MainActivity : SimpleActivity() {
mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
findItem(R.id.more_apps_from_us).isVisible =
!resources.getBoolean(org.fossify.commons.R.bool.hide_google_relations)
findItem(R.id.delete_note).isVisible = !isDefaultEmptyNote || mNotes.size > 1
saveNoteButton = findItem(R.id.save_note)
saveNoteButton!!.isVisible =
@@ -399,6 +401,16 @@ class MainActivity : SimpleActivity() {
}
}
private fun isDefaultEmptyNote(): Boolean {
return if (::mCurrentNote.isInitialized) {
(mCurrentNote.title == getString(R.string.general_note) &&
getCurrentNoteText().isNullOrEmpty() &&
mCurrentNote.value.isEmpty())
} else {
false
}
}
@SuppressLint("NewApi")
private fun checkShortcuts() {
val appIconColor = config.appIconColor
@@ -557,6 +569,7 @@ class MainActivity : SimpleActivity() {
mNotes = notes
mCurrentNote = mNotes[0]
wasNoteTextEmpty = mCurrentNote.value.isEmpty()
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this)
binding.viewPager.apply {
adapter = mAdapter
@@ -1339,7 +1352,7 @@ class MainActivity : SimpleActivity() {
}
fun deleteNote(deleteFile: Boolean, note: Note) {
if (mNotes.size <= 1 || note != mCurrentNote) {
if (mNotes.isEmpty() || note != mCurrentNote) {
return
}
@@ -1360,8 +1373,12 @@ class MainActivity : SimpleActivity() {
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
ensureBackgroundThread {
val currentNoteIndex = mNotes.indexOf(note)
val noteToRefresh =
val noteToRefresh = if (mNotes.size == 1) {
null
} else {
mNotes[if (currentNoteIndex > 0) currentNoteIndex - 1 else currentNoteIndex + 1]
}
notesDB.deleteNote(note)
widgetsDB.deleteNoteWidgets(note.id!!)
@@ -1370,12 +1387,13 @@ class MainActivity : SimpleActivity() {
}
}
private fun refreshNotes(note: Note, deleteFile: Boolean) {
private fun refreshNotes(note: Note?, deleteFile: Boolean) {
NotesHelper(this).getNotes {
mNotes = it
val noteId = note.id
val currentNote = note ?: mNotes[0]
val noteId = currentNote.id
updateSelectedNote(noteId!!)
if (config.widgetNoteId == note.id) {
if (config.widgetNoteId == currentNote.id) {
config.widgetNoteId = mCurrentNote.id!!
updateWidgets()
}
@@ -1383,7 +1401,7 @@ class MainActivity : SimpleActivity() {
initViewPager()
if (deleteFile) {
deleteFile(FileDirItem(note.path, note.title)) {
deleteFile(FileDirItem(currentNote.path, currentNote.title)) {
if (!it) {
toast(org.fossify.commons.R.string.unknown_error_occurred)
}
@@ -1541,6 +1559,14 @@ class MainActivity : SimpleActivity() {
}
}
if (getCurrentNoteText().isNullOrEmpty()) {
wasNoteTextEmpty = true
shouldRecreateMenu = true
} else if (wasNoteTextEmpty) {
wasNoteTextEmpty = false
shouldRecreateMenu = true
}
if (shouldRecreateMenu) {
refreshMenuItems()
}

View File

@@ -3,7 +3,6 @@ package org.fossify.notes.helpers
import android.content.Context
import android.os.Handler
import android.os.Looper
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.fossify.commons.activities.BaseSimpleActivity
import org.fossify.commons.helpers.ExportResult
@@ -42,7 +41,8 @@ class NotesHelper(val context: Context) {
if (notes.isEmpty()) {
val generalNote = context.resources.getString(R.string.general_note)
val note = Note(null, generalNote, "", NoteType.TYPE_TEXT, "", PROTECTION_NONE, "")
context.notesDB.insertOrUpdate(note)
val insertedId = context.notesDB.insertOrUpdate(note)
note.id = insertedId
notes.add(note)
}