diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt index c3184812..9f207d18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt @@ -43,6 +43,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { private var noteViewWithTextSelected: MyEditText? = null private var wasInit = false private var storedUseEnglish = false + private var showSaveButton = false + private var saveNoteButton: MenuItem? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -111,7 +113,9 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { findItem(R.id.open_note).isVisible = shouldBeVisible findItem(R.id.delete_note).isVisible = shouldBeVisible findItem(R.id.export_all_notes).isVisible = shouldBeVisible - findItem(R.id.save_note).isVisible = !config.autosaveNotes + + saveNoteButton = findItem(R.id.save_note) + saveNoteButton!!.isVisible = !config.autosaveNotes && showSaveButton } pager_title_strip.beVisibleIf(shouldBeVisible) @@ -249,6 +253,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { private fun addNewNote(note: Note) { val id = dbHelper.insertNote(note) mNotes = dbHelper.getNotes() + showSaveButton = false invalidateOptionsMenu() initViewPager() updateSelectedNote(id) @@ -468,6 +473,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { private fun saveNote() { saveCurrentNote() + showSaveButton = false + invalidateOptionsMenu() } private fun getNoteIndexWithId(id: Int): Int { @@ -509,6 +516,13 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { config.currentNoteId = mCurrentNote.id } + fun currentNoteTextChanged(newText: String) { + showSaveButton = newText != mCurrentNote.value + if (showSaveButton != saveNoteButton?.isVisible) { + invalidateOptionsMenu() + } + } + private fun checkWhatsNewDialog() { arrayListOf().apply { add(Release(25, R.string.release_25)) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt index cba3eb0c..d694a39c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt @@ -50,11 +50,61 @@ class NoteFragment : Fragment() { return view } + override fun onResume() { + super.onResume() + + val config = context!!.config + view.notes_view.apply { + typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT + + val fileContents = context.getNoteStoredValue(note) + + if (fileContents == null) { + (activity as MainActivity).deleteNote(false) + return + } + + setColors(config.textColor, config.primaryColor, config.backgroundColor) + setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize()) + gravity = getTextGravity() + if (text.toString() != fileContents) { + setText(fileContents) + setSelection(if (config.placeCursorToEnd) text.length else 0) + } + } + + if (config.showWordCount) { + view.notes_counter.beVisible() + view.notes_counter.setTextColor(config.textColor) + setWordCounter(view.notes_view.text.toString()) + } else { + view.notes_counter.beGone() + } + + if (config.showWordCount || !config.autosaveNotes) { + view.notes_view.addTextChangedListener(textWatcher) + } else { + view.notes_view.addTextChangedListener(null) + } + } + + override fun onPause() { + super.onPause() + if (context!!.config.autosaveNotes) { + saveText() + } + view.notes_view.removeTextChangedListener(textWatcher) + } + override fun setMenuVisibility(menuVisible: Boolean) { super.setMenuVisibility(menuVisible) - if (noteId != 0 && context?.config?.autosaveNotes == true) { + if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) { saveText() } + + if (menuVisible && noteId != 0) { + (activity as MainActivity).currentNoteTextChanged(getCurrentNoteViewText()) + } } fun getNotesView() = view.notes_view @@ -98,50 +148,8 @@ class NoteFragment : Fragment() { else -> Gravity.LEFT } - override fun onResume() { - super.onResume() - - val config = context!!.config - - view.notes_view.apply { - typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT - - val fileContents = context.getNoteStoredValue(note) - - if (fileContents == null) { - (activity as MainActivity).deleteNote(false) - return - } - - setColors(config.textColor, config.primaryColor, config.backgroundColor) - setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize()) - gravity = getTextGravity() - if (text.toString() != fileContents) { - setText(fileContents) - setSelection(if (config.placeCursorToEnd) text.length else 0) - } - } - - if (config.showWordCount) { - view.notes_view.addTextChangedListener(textWatcher) - view.notes_counter.beVisible() - view.notes_counter.setTextColor(config.textColor) - setWordCounter(view.notes_view.text) - } else { - view.notes_counter.beGone() - } - } - - override fun onPause() { - super.onPause() - if (context!!.config.autosaveNotes) { - saveText() - } - view.notes_view.removeTextChangedListener(textWatcher) - } - - private fun setWordCounter(text: Editable) { - val words = text.toString().replace("\n", " ").split(" ") + private fun setWordCounter(text: String) { + val words = text.replace("\n", " ").split(" ") notes_counter.text = words.count { it.isNotEmpty() }.toString() } @@ -153,7 +161,9 @@ class NoteFragment : Fragment() { } override fun afterTextChanged(editable: Editable) { - setWordCounter(editable) + val text = editable.toString() + setWordCounter(text) + (activity as MainActivity).currentNoteTextChanged(text) } } }