toggle undo/redo menu button visibility as required

This commit is contained in:
tibbi 2018-07-17 21:23:37 +02:00
parent 2c3a6937a7
commit d47226edf2
2 changed files with 28 additions and 4 deletions

View File

@ -46,6 +46,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private var wasInit = false
private var storedEnableLineWrap = true
private var showSaveButton = false
private var showUndoButton = false
private var showRedoButton = false
private var saveNoteButton: MenuItem? = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -105,6 +107,11 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
menu.apply {
findItem(R.id.undo).isVisible = showUndoButton
findItem(R.id.redo).isVisible = showRedoButton
}
return true
}
@ -573,13 +580,28 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
config.currentNoteId = mCurrentNote.id
}
fun currentNoteTextChanged(newText: String) {
fun currentNoteTextChanged(newText: String, showUndo: Boolean, showRedo: Boolean) {
var shouldRecreateMenu = false
if (showUndo != showUndoButton) {
showUndoButton = showUndo
shouldRecreateMenu = true
}
if (showRedo != showRedoButton) {
showRedoButton = showRedo
shouldRecreateMenu = true
}
if (!config.autosaveNotes) {
showSaveButton = newText != mCurrentNote.value
if (showSaveButton != saveNoteButton?.isVisible) {
invalidateOptionsMenu()
shouldRecreateMenu = true
}
}
if (shouldRecreateMenu) {
invalidateOptionsMenu()
}
}
private fun checkWhatsNewDialog() {

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.notes.fragments
import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.Bundle
import android.support.v4.app.Fragment
@ -115,7 +116,7 @@ class NoteFragment : Fragment() {
if (menuVisible && noteId != 0) {
val currentText = getCurrentNoteViewText()
if (currentText != null) {
(activity as MainActivity).currentNoteTextChanged(currentText)
(activity as MainActivity).currentNoteTextChanged(currentText, isUndoAvailable(), isRedoAvailable())
}
}
}
@ -158,6 +159,7 @@ class NoteFragment : Fragment() {
fun getCurrentNoteViewText() = view.notes_view?.text?.toString()
@SuppressLint("RtlHardcoded")
private fun getTextGravity() = when (context!!.config.gravity) {
GRAVITY_CENTER -> Gravity.CENTER_HORIZONTAL
GRAVITY_RIGHT -> Gravity.RIGHT
@ -237,7 +239,7 @@ class NoteFragment : Fragment() {
override fun afterTextChanged(editable: Editable) {
val text = editable.toString()
setWordCounter(text)
(activity as MainActivity).currentNoteTextChanged(text)
(activity as MainActivity).currentNoteTextChanged(text, isUndoAvailable(), isRedoAvailable())
}
}
}