diff --git a/CHANGELOG.md b/CHANGELOG.md index a1082f36b..80f47ae36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt index fc936b845..e9e6e66de 100644 --- a/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt @@ -147,6 +147,7 @@ class MainActivity : SimpleActivity() { private var searchIndex = 0 private var searchMatches = emptyList() 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() } diff --git a/app/src/main/kotlin/org/fossify/notes/helpers/NotesHelper.kt b/app/src/main/kotlin/org/fossify/notes/helpers/NotesHelper.kt index fd438f73a..44e27d5ac 100644 --- a/app/src/main/kotlin/org/fossify/notes/helpers/NotesHelper.kt +++ b/app/src/main/kotlin/org/fossify/notes/helpers/NotesHelper.kt @@ -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) }