@@ -87,6 +87,8 @@ class MainActivity : SimpleActivity() {
8787 super .onCreate(savedInstanceState)
8888 setContentView(R .layout.activity_main)
8989 appLaunched(BuildConfig .APPLICATION_ID )
90+ setupOptionsMenu()
91+ refreshMenuItems()
9092
9193 searchQueryET = findViewById(R .id.search_query)
9294 searchPrevBtn = findViewById(R .id.search_previous)
@@ -113,11 +115,12 @@ class MainActivity : SimpleActivity() {
113115
114116 override fun onResume () {
115117 super .onResume()
118+ setupToolbar(main_toolbar)
116119 if (storedEnableLineWrap != config.enableLineWrap) {
117120 initViewPager()
118121 }
119122
120- invalidateOptionsMenu ()
123+ refreshMenuItems ()
121124 pager_title_strip.apply {
122125 setTextSize(TypedValue .COMPLEX_UNIT_PX , getPercentageFontSize())
123126 setGravity(Gravity .CENTER_VERTICAL )
@@ -168,11 +171,26 @@ class MainActivity : SimpleActivity() {
168171 return true
169172 }
170173
171- override fun onPrepareOptionsMenu ( menu : Menu ): Boolean {
174+ private fun refreshMenuItems () {
172175 val multipleNotesExist = mNotes.size > 1
173176 val isCurrentItemChecklist = isCurrentItemChecklist()
174177
175- menu.apply {
178+ main_toolbar.menu.apply {
179+ val areButtonsVisible = (showRedoButton || showUndoButton) && mCurrentNote.type == NoteType .TYPE_TEXT .value
180+ findItem(R .id.undo).apply {
181+ isVisible = areButtonsVisible
182+ isEnabled = showUndoButton && mCurrentNote.type == NoteType .TYPE_TEXT .value
183+ icon.alpha = if (isEnabled) 255 else 127
184+ }
185+
186+ findItem(R .id.redo).apply {
187+ isVisible = areButtonsVisible
188+ isEnabled = showRedoButton && mCurrentNote.type == NoteType .TYPE_TEXT .value
189+ icon.alpha = if (isEnabled) 255 else 127
190+ }
191+ }
192+
193+ main_toolbar.menu.apply {
176194 findItem(R .id.rename_note).isVisible = multipleNotesExist
177195 findItem(R .id.open_note).isVisible = multipleNotesExist
178196 findItem(R .id.delete_note).isVisible = multipleNotesExist
@@ -183,49 +201,51 @@ class MainActivity : SimpleActivity() {
183201 findItem(R .id.sort_checklist).isVisible = isCurrentItemChecklist
184202 findItem(R .id.import_folder).isVisible = ! isQPlus()
185203 findItem(R .id.import_notes).isVisible = isQPlus()
186- findItem(R .id.lock_note).isVisible = mNotes.isNotEmpty() && ! mCurrentNote.isLocked()
187- findItem(R .id.unlock_note).isVisible = mNotes.isNotEmpty() && mCurrentNote.isLocked()
204+ findItem(R .id.lock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && ! mCurrentNote.isLocked() )
205+ findItem(R .id.unlock_note).isVisible = mNotes.isNotEmpty() && (:: mCurrentNote.isInitialized && mCurrentNote. isLocked() )
188206
189207 saveNoteButton = findItem(R .id.save_note)
190- saveNoteButton!! .isVisible = ! config.autosaveNotes && showSaveButton && mCurrentNote.type == NoteType .TYPE_TEXT .value
208+ saveNoteButton!! .isVisible =
209+ ! config.autosaveNotes && showSaveButton && (::mCurrentNote.isInitialized && mCurrentNote.type == NoteType .TYPE_TEXT .value)
191210 }
192211
193212 pager_title_strip.beVisibleIf(multipleNotesExist)
194- return super .onPrepareOptionsMenu(menu)
195- }
196-
197- override fun onOptionsItemSelected (item : MenuItem ): Boolean {
198- if (config.autosaveNotes && item.itemId != R .id.undo && item.itemId != R .id.redo) {
199- saveCurrentNote(false )
200- }
201-
202- val fragment = getCurrentFragment()
203- when (item.itemId) {
204- R .id.open_search -> fragment?.handleUnlocking { openSearch() }
205- R .id.open_note -> displayOpenNoteDialog()
206- R .id.save_note -> fragment?.handleUnlocking { saveNote() }
207- R .id.undo -> undo()
208- R .id.redo -> redo()
209- R .id.new_note -> displayNewNoteDialog()
210- R .id.rename_note -> fragment?.handleUnlocking { displayRenameDialog() }
211- R .id.share -> fragment?.handleUnlocking { shareText() }
212- R .id.lock_note -> lockNote()
213- R .id.unlock_note -> unlockNote()
214- R .id.open_file -> tryOpenFile()
215- R .id.import_folder -> openFolder()
216- R .id.export_as_file -> fragment?.handleUnlocking { tryExportAsFile() }
217- R .id.export_all_notes -> tryExportAllNotes()
218- R .id.export_notes -> tryExportNotes()
219- R .id.import_notes -> tryImportNotes()
220- R .id.print -> fragment?.handleUnlocking { printText() }
221- R .id.delete_note -> fragment?.handleUnlocking { displayDeleteNotePrompt() }
222- R .id.settings -> launchSettings()
223- R .id.about -> launchAbout()
224- R .id.remove_done_items -> fragment?.handleUnlocking { removeDoneItems() }
225- R .id.sort_checklist -> fragment?.handleUnlocking { displaySortChecklistDialog() }
226- else -> return super .onOptionsItemSelected(item)
213+ }
214+
215+ private fun setupOptionsMenu () {
216+ main_toolbar.setOnMenuItemClickListener { menuItem ->
217+ if (config.autosaveNotes && menuItem.itemId != R .id.undo && menuItem.itemId != R .id.redo) {
218+ saveCurrentNote(false )
219+ }
220+
221+ val fragment = getCurrentFragment()
222+ when (menuItem.itemId) {
223+ R .id.open_search -> fragment?.handleUnlocking { openSearch() }
224+ R .id.open_note -> displayOpenNoteDialog()
225+ R .id.save_note -> fragment?.handleUnlocking { saveNote() }
226+ R .id.undo -> undo()
227+ R .id.redo -> redo()
228+ R .id.new_note -> displayNewNoteDialog()
229+ R .id.rename_note -> fragment?.handleUnlocking { displayRenameDialog() }
230+ R .id.share -> fragment?.handleUnlocking { shareText() }
231+ R .id.lock_note -> lockNote()
232+ R .id.unlock_note -> unlockNote()
233+ R .id.open_file -> tryOpenFile()
234+ R .id.import_folder -> openFolder()
235+ R .id.export_as_file -> fragment?.handleUnlocking { tryExportAsFile() }
236+ R .id.export_all_notes -> tryExportAllNotes()
237+ R .id.export_notes -> tryExportNotes()
238+ R .id.import_notes -> tryImportNotes()
239+ R .id.print -> fragment?.handleUnlocking { printText() }
240+ R .id.delete_note -> fragment?.handleUnlocking { displayDeleteNotePrompt() }
241+ R .id.settings -> launchSettings()
242+ R .id.about -> launchAbout()
243+ R .id.remove_done_items -> fragment?.handleUnlocking { removeDoneItems() }
244+ R .id.sort_checklist -> fragment?.handleUnlocking { displaySortChecklistDialog() }
245+ else -> return @setOnMenuItemClickListener false
246+ }
247+ return @setOnMenuItemClickListener true
227248 }
228- return true
229249 }
230250
231251 // https://code.google.com/p/android/issues/detail?id=191430 quickfix
@@ -286,7 +306,7 @@ class MainActivity : SimpleActivity() {
286306 }
287307 }
288308
289- private fun isCurrentItemChecklist () = if (this ::mCurrentNote.isInitialized) mCurrentNote.type == NoteType .TYPE_CHECKLIST .value else false
309+ private fun isCurrentItemChecklist () = if (::mCurrentNote.isInitialized) mCurrentNote.type == NoteType .TYPE_CHECKLIST .value else false
290310
291311 @SuppressLint(" NewApi" )
292312 private fun checkShortcuts () {
@@ -422,7 +442,7 @@ class MainActivity : SimpleActivity() {
422442 .forEach(::removeProtection)
423443
424444 mNotes = notes
425- invalidateOptionsMenu ()
445+ refreshMenuItems ()
426446 mCurrentNote = mNotes[0 ]
427447 mAdapter = NotesPagerAdapter (supportFragmentManager, mNotes, this )
428448 view_pager.apply {
@@ -433,7 +453,7 @@ class MainActivity : SimpleActivity() {
433453 onPageChangeListener {
434454 mCurrentNote = mNotes[it]
435455 config.currentNoteId = mCurrentNote.id!!
436- invalidateOptionsMenu ()
456+ refreshMenuItems ()
437457 }
438458 }
439459
@@ -463,7 +483,7 @@ class MainActivity : SimpleActivity() {
463483 view_pager.onPageChangeListener {
464484 currentTextFragment?.removeTextWatcher()
465485 currentNotesView()?.let { noteView ->
466- noteView.text.clearBackgroundSpans()
486+ noteView.text!! .clearBackgroundSpans()
467487 }
468488
469489 closeSearch()
@@ -483,7 +503,7 @@ class MainActivity : SimpleActivity() {
483503 private fun searchTextChanged (text : String ) {
484504 currentNotesView()?.let { noteView ->
485505 currentTextFragment?.removeTextWatcher()
486- noteView.text.clearBackgroundSpans()
506+ noteView.text!! .clearBackgroundSpans()
487507
488508 if (text.isNotBlank() && text.length > 1 ) {
489509 searchMatches = noteView.value.searchMatches(text)
@@ -1263,7 +1283,7 @@ class MainActivity : SimpleActivity() {
12631283 private fun saveNote () {
12641284 saveCurrentNote(true )
12651285 showSaveButton = false
1266- invalidateOptionsMenu ()
1286+ refreshMenuItems ()
12671287 }
12681288
12691289 private fun undo () {
@@ -1309,7 +1329,7 @@ class MainActivity : SimpleActivity() {
13091329 mCurrentNote.protectionHash = hash
13101330 mCurrentNote.protectionType = type
13111331 NotesHelper (this ).insertOrUpdateNote(mCurrentNote) {
1312- invalidateOptionsMenu ()
1332+ refreshMenuItems ()
13131333 }
13141334 }
13151335 }
@@ -1333,7 +1353,7 @@ class MainActivity : SimpleActivity() {
13331353 shouldShowLockedContent = true
13341354 checkLockState()
13351355 }
1336- invalidateOptionsMenu ()
1356+ refreshMenuItems ()
13371357 }
13381358 }
13391359 }
@@ -1359,7 +1379,7 @@ class MainActivity : SimpleActivity() {
13591379 }
13601380
13611381 if (shouldRecreateMenu) {
1362- invalidateOptionsMenu ()
1382+ refreshMenuItems ()
13631383 }
13641384 }
13651385 }
0 commit comments