@@ -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 }
0 commit comments