Skip to content

Commit f698d78

Browse files
committed
lets be smarter about displaying the Save button when autosave is off
1 parent b59649d commit f698d78

File tree

2 files changed

+71
-47
lines changed

2 files changed

+71
-47
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
4343
private var noteViewWithTextSelected: MyEditText? = null
4444
private var wasInit = false
4545
private var storedUseEnglish = false
46+
private var showSaveButton = false
47+
private var saveNoteButton: MenuItem? = null
4648

4749
override fun onCreate(savedInstanceState: Bundle?) {
4850
super.onCreate(savedInstanceState)
@@ -111,7 +113,9 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
111113
findItem(R.id.open_note).isVisible = shouldBeVisible
112114
findItem(R.id.delete_note).isVisible = shouldBeVisible
113115
findItem(R.id.export_all_notes).isVisible = shouldBeVisible
114-
findItem(R.id.save_note).isVisible = !config.autosaveNotes
116+
117+
saveNoteButton = findItem(R.id.save_note)
118+
saveNoteButton!!.isVisible = !config.autosaveNotes && showSaveButton
115119
}
116120

117121
pager_title_strip.beVisibleIf(shouldBeVisible)
@@ -249,6 +253,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
249253
private fun addNewNote(note: Note) {
250254
val id = dbHelper.insertNote(note)
251255
mNotes = dbHelper.getNotes()
256+
showSaveButton = false
252257
invalidateOptionsMenu()
253258
initViewPager()
254259
updateSelectedNote(id)
@@ -468,6 +473,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
468473

469474
private fun saveNote() {
470475
saveCurrentNote()
476+
showSaveButton = false
477+
invalidateOptionsMenu()
471478
}
472479

473480
private fun getNoteIndexWithId(id: Int): Int {
@@ -509,6 +516,13 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
509516
config.currentNoteId = mCurrentNote.id
510517
}
511518

519+
fun currentNoteTextChanged(newText: String) {
520+
showSaveButton = newText != mCurrentNote.value
521+
if (showSaveButton != saveNoteButton?.isVisible) {
522+
invalidateOptionsMenu()
523+
}
524+
}
525+
512526
private fun checkWhatsNewDialog() {
513527
arrayListOf<Release>().apply {
514528
add(Release(25, R.string.release_25))

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

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,61 @@ class NoteFragment : Fragment() {
5050
return view
5151
}
5252

53+
override fun onResume() {
54+
super.onResume()
55+
56+
val config = context!!.config
57+
view.notes_view.apply {
58+
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
59+
60+
val fileContents = context.getNoteStoredValue(note)
61+
62+
if (fileContents == null) {
63+
(activity as MainActivity).deleteNote(false)
64+
return
65+
}
66+
67+
setColors(config.textColor, config.primaryColor, config.backgroundColor)
68+
setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize())
69+
gravity = getTextGravity()
70+
if (text.toString() != fileContents) {
71+
setText(fileContents)
72+
setSelection(if (config.placeCursorToEnd) text.length else 0)
73+
}
74+
}
75+
76+
if (config.showWordCount) {
77+
view.notes_counter.beVisible()
78+
view.notes_counter.setTextColor(config.textColor)
79+
setWordCounter(view.notes_view.text.toString())
80+
} else {
81+
view.notes_counter.beGone()
82+
}
83+
84+
if (config.showWordCount || !config.autosaveNotes) {
85+
view.notes_view.addTextChangedListener(textWatcher)
86+
} else {
87+
view.notes_view.addTextChangedListener(null)
88+
}
89+
}
90+
91+
override fun onPause() {
92+
super.onPause()
93+
if (context!!.config.autosaveNotes) {
94+
saveText()
95+
}
96+
view.notes_view.removeTextChangedListener(textWatcher)
97+
}
98+
5399
override fun setMenuVisibility(menuVisible: Boolean) {
54100
super.setMenuVisibility(menuVisible)
55-
if (noteId != 0 && context?.config?.autosaveNotes == true) {
101+
if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) {
56102
saveText()
57103
}
104+
105+
if (menuVisible && noteId != 0) {
106+
(activity as MainActivity).currentNoteTextChanged(getCurrentNoteViewText())
107+
}
58108
}
59109

60110
fun getNotesView() = view.notes_view
@@ -98,50 +148,8 @@ class NoteFragment : Fragment() {
98148
else -> Gravity.LEFT
99149
}
100150

101-
override fun onResume() {
102-
super.onResume()
103-
104-
val config = context!!.config
105-
106-
view.notes_view.apply {
107-
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
108-
109-
val fileContents = context.getNoteStoredValue(note)
110-
111-
if (fileContents == null) {
112-
(activity as MainActivity).deleteNote(false)
113-
return
114-
}
115-
116-
setColors(config.textColor, config.primaryColor, config.backgroundColor)
117-
setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize())
118-
gravity = getTextGravity()
119-
if (text.toString() != fileContents) {
120-
setText(fileContents)
121-
setSelection(if (config.placeCursorToEnd) text.length else 0)
122-
}
123-
}
124-
125-
if (config.showWordCount) {
126-
view.notes_view.addTextChangedListener(textWatcher)
127-
view.notes_counter.beVisible()
128-
view.notes_counter.setTextColor(config.textColor)
129-
setWordCounter(view.notes_view.text)
130-
} else {
131-
view.notes_counter.beGone()
132-
}
133-
}
134-
135-
override fun onPause() {
136-
super.onPause()
137-
if (context!!.config.autosaveNotes) {
138-
saveText()
139-
}
140-
view.notes_view.removeTextChangedListener(textWatcher)
141-
}
142-
143-
private fun setWordCounter(text: Editable) {
144-
val words = text.toString().replace("\n", " ").split(" ")
151+
private fun setWordCounter(text: String) {
152+
val words = text.replace("\n", " ").split(" ")
145153
notes_counter.text = words.count { it.isNotEmpty() }.toString()
146154
}
147155

@@ -153,7 +161,9 @@ class NoteFragment : Fragment() {
153161
}
154162

155163
override fun afterTextChanged(editable: Editable) {
156-
setWordCounter(editable)
164+
val text = editable.toString()
165+
setWordCounter(text)
166+
(activity as MainActivity).currentNoteTextChanged(text)
157167
}
158168
}
159169
}

0 commit comments

Comments
 (0)