lets be smarter about displaying the Save button when autosave is off

This commit is contained in:
tibbi 2018-01-26 20:09:43 +01:00
parent b59649dfea
commit f698d784de
2 changed files with 71 additions and 47 deletions

View File

@ -43,6 +43,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private var noteViewWithTextSelected: MyEditText? = null private var noteViewWithTextSelected: MyEditText? = null
private var wasInit = false private var wasInit = false
private var storedUseEnglish = false private var storedUseEnglish = false
private var showSaveButton = false
private var saveNoteButton: MenuItem? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -111,7 +113,9 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
findItem(R.id.open_note).isVisible = shouldBeVisible findItem(R.id.open_note).isVisible = shouldBeVisible
findItem(R.id.delete_note).isVisible = shouldBeVisible findItem(R.id.delete_note).isVisible = shouldBeVisible
findItem(R.id.export_all_notes).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) pager_title_strip.beVisibleIf(shouldBeVisible)
@ -249,6 +253,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun addNewNote(note: Note) { private fun addNewNote(note: Note) {
val id = dbHelper.insertNote(note) val id = dbHelper.insertNote(note)
mNotes = dbHelper.getNotes() mNotes = dbHelper.getNotes()
showSaveButton = false
invalidateOptionsMenu() invalidateOptionsMenu()
initViewPager() initViewPager()
updateSelectedNote(id) updateSelectedNote(id)
@ -468,6 +473,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun saveNote() { private fun saveNote() {
saveCurrentNote() saveCurrentNote()
showSaveButton = false
invalidateOptionsMenu()
} }
private fun getNoteIndexWithId(id: Int): Int { private fun getNoteIndexWithId(id: Int): Int {
@ -509,6 +516,13 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
config.currentNoteId = mCurrentNote.id config.currentNoteId = mCurrentNote.id
} }
fun currentNoteTextChanged(newText: String) {
showSaveButton = newText != mCurrentNote.value
if (showSaveButton != saveNoteButton?.isVisible) {
invalidateOptionsMenu()
}
}
private fun checkWhatsNewDialog() { private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply { arrayListOf<Release>().apply {
add(Release(25, R.string.release_25)) add(Release(25, R.string.release_25))

View File

@ -50,11 +50,61 @@ class NoteFragment : Fragment() {
return view 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) { override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
if (noteId != 0 && context?.config?.autosaveNotes == true) { if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) {
saveText() saveText()
} }
if (menuVisible && noteId != 0) {
(activity as MainActivity).currentNoteTextChanged(getCurrentNoteViewText())
}
} }
fun getNotesView() = view.notes_view fun getNotesView() = view.notes_view
@ -98,50 +148,8 @@ class NoteFragment : Fragment() {
else -> Gravity.LEFT else -> Gravity.LEFT
} }
override fun onResume() { private fun setWordCounter(text: String) {
super.onResume() val words = text.replace("\n", " ").split(" ")
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(" ")
notes_counter.text = words.count { it.isNotEmpty() }.toString() notes_counter.text = words.count { it.isNotEmpty() }.toString()
} }
@ -153,7 +161,9 @@ class NoteFragment : Fragment() {
} }
override fun afterTextChanged(editable: Editable) { override fun afterTextChanged(editable: Editable) {
setWordCounter(editable) val text = editable.toString()
setWordCounter(text)
(activity as MainActivity).currentNoteTextChanged(text)
} }
} }
} }