Skip to content

Commit d47226e

Browse files
committed
toggle undo/redo menu button visibility as required
1 parent 2c3a693 commit d47226e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
4646
private var wasInit = false
4747
private var storedEnableLineWrap = true
4848
private var showSaveButton = false
49+
private var showUndoButton = false
50+
private var showRedoButton = false
4951
private var saveNoteButton: MenuItem? = null
5052

5153
override fun onCreate(savedInstanceState: Bundle?) {
@@ -105,6 +107,11 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
105107

106108
override fun onCreateOptionsMenu(menu: Menu): Boolean {
107109
menuInflater.inflate(R.menu.menu, menu)
110+
menu.apply {
111+
findItem(R.id.undo).isVisible = showUndoButton
112+
findItem(R.id.redo).isVisible = showRedoButton
113+
}
114+
108115
return true
109116
}
110117

@@ -573,13 +580,28 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
573580
config.currentNoteId = mCurrentNote.id
574581
}
575582

576-
fun currentNoteTextChanged(newText: String) {
583+
fun currentNoteTextChanged(newText: String, showUndo: Boolean, showRedo: Boolean) {
584+
var shouldRecreateMenu = false
585+
if (showUndo != showUndoButton) {
586+
showUndoButton = showUndo
587+
shouldRecreateMenu = true
588+
}
589+
590+
if (showRedo != showRedoButton) {
591+
showRedoButton = showRedo
592+
shouldRecreateMenu = true
593+
}
594+
577595
if (!config.autosaveNotes) {
578596
showSaveButton = newText != mCurrentNote.value
579597
if (showSaveButton != saveNoteButton?.isVisible) {
580-
invalidateOptionsMenu()
598+
shouldRecreateMenu = true
581599
}
582600
}
601+
602+
if (shouldRecreateMenu) {
603+
invalidateOptionsMenu()
604+
}
583605
}
584606

585607
private fun checkWhatsNewDialog() {

app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.notes.fragments
22

3+
import android.annotation.SuppressLint
34
import android.graphics.Typeface
45
import android.os.Bundle
56
import android.support.v4.app.Fragment
@@ -115,7 +116,7 @@ class NoteFragment : Fragment() {
115116
if (menuVisible && noteId != 0) {
116117
val currentText = getCurrentNoteViewText()
117118
if (currentText != null) {
118-
(activity as MainActivity).currentNoteTextChanged(currentText)
119+
(activity as MainActivity).currentNoteTextChanged(currentText, isUndoAvailable(), isRedoAvailable())
119120
}
120121
}
121122
}
@@ -158,6 +159,7 @@ class NoteFragment : Fragment() {
158159

159160
fun getCurrentNoteViewText() = view.notes_view?.text?.toString()
160161

162+
@SuppressLint("RtlHardcoded")
161163
private fun getTextGravity() = when (context!!.config.gravity) {
162164
GRAVITY_CENTER -> Gravity.CENTER_HORIZONTAL
163165
GRAVITY_RIGHT -> Gravity.RIGHT
@@ -237,7 +239,7 @@ class NoteFragment : Fragment() {
237239
override fun afterTextChanged(editable: Editable) {
238240
val text = editable.toString()
239241
setWordCounter(text)
240-
(activity as MainActivity).currentNoteTextChanged(text)
242+
(activity as MainActivity).currentNoteTextChanged(text, isUndoAvailable(), isRedoAvailable())
241243
}
242244
}
243245
}

0 commit comments

Comments
 (0)