@@ -3,6 +3,8 @@ package com.simplemobiletools.notes.fragments
33import android.graphics.Typeface
44import android.os.Bundle
55import android.support.v4.app.Fragment
6+ import android.text.Editable
7+ import android.text.TextWatcher
68import android.text.method.LinkMovementMethod
79import android.text.util.Linkify
810import android.util.TypedValue
@@ -18,6 +20,7 @@ import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
1820import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
1921import com.simplemobiletools.notes.helpers.NOTE_ID
2022import com.simplemobiletools.notes.models.Note
23+ import kotlinx.android.synthetic.main.fragment_note.*
2124import kotlinx.android.synthetic.main.fragment_note.view.*
2225import java.io.File
2326
@@ -95,6 +98,7 @@ class NoteFragment : Fragment() {
9598 super .onResume()
9699
97100 val config = context!! .config
101+
98102 view.notes_view.apply {
99103 typeface = if (config.monospacedFont) Typeface .MONOSPACE else Typeface .DEFAULT
100104
@@ -113,10 +117,42 @@ class NoteFragment : Fragment() {
113117 setSelection(if (config.placeCursorToEnd) text.length else 0 )
114118 }
115119 }
120+
121+ if (config.showWordCount) {
122+ view.notes_view.addTextChangedListener(textWatcher)
123+ view.notes_counter.visibility = View .VISIBLE
124+ setWordCounter(view.notes_view.text)
125+ }
126+ else {
127+ view.notes_counter.visibility = View .GONE
128+ }
116129 }
117130
118131 override fun onPause () {
119132 super .onPause()
120133 saveText()
134+
135+ removeTextWatcher()
136+ }
137+
138+ private fun removeTextWatcher () {
139+ view.notes_view.removeTextChangedListener(textWatcher)
140+ }
141+
142+ private fun setWordCounter (text : Editable ) {
143+ val wordArray = text.toString().replace(" \n " , " " ).split(" " )
144+ notes_counter.text = wordArray.count { it.isNotEmpty() }.toString()
145+ }
146+
147+ private var textWatcher: TextWatcher = object : TextWatcher {
148+ override fun beforeTextChanged (s : CharSequence , start : Int , count : Int , after : Int ) {
149+ }
150+
151+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {
152+ }
153+
154+ override fun afterTextChanged (editable : Editable ) {
155+ setWordCounter(editable)
156+ }
121157 }
122158}
0 commit comments