@@ -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