Skip to content

Commit e763f05

Browse files
committed
Add optional Word Count to Note
- Setting included (default: deactived) - Translation for en/de refs #40
1 parent 11b57f1 commit e763f05

8 files changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class SettingsActivity : SimpleActivity() {
3535
setupClickableLinks()
3636
setupMonospacedFont()
3737
setupShowKeyboard()
38+
setupShowWordCount()
3839
setupFontSize()
3940
setupGravity()
4041
setupWidgetNote()
@@ -90,6 +91,14 @@ class SettingsActivity : SimpleActivity() {
9091
}
9192
}
9293

94+
private fun setupShowWordCount() {
95+
settings_show_word_count.isChecked = config.showWordCount
96+
settings_show_word_count_holder.setOnClickListener {
97+
settings_show_word_count.toggle()
98+
config.showWordCount = settings_show_word_count.isChecked
99+
}
100+
}
101+
93102
private fun setupFontSize() {
94103
settings_font_size.text = getFontSizeText()
95104
settings_font_size_holder.setOnClickListener {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.simplemobiletools.notes.fragments
33
import android.graphics.Typeface
44
import android.os.Bundle
55
import android.support.v4.app.Fragment
6+
import android.text.Editable
7+
import android.text.TextWatcher
68
import android.text.method.LinkMovementMethod
79
import android.text.util.Linkify
810
import android.util.TypedValue
@@ -95,6 +97,7 @@ class NoteFragment : Fragment() {
9597
super.onResume()
9698

9799
val config = context!!.config
100+
98101
view.notes_view.apply {
99102
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
100103

@@ -113,10 +116,45 @@ class NoteFragment : Fragment() {
113116
setSelection(if (config.placeCursorToEnd) text.length else 0)
114117
}
115118
}
119+
120+
if (config.showWordCount) {
121+
view.notes_view.addTextChangedListener(textWatcher)
122+
view.notes_counter.visibility = View.VISIBLE
123+
setWordCounter(view.notes_view.text)
124+
}
125+
else {
126+
view.notes_counter.visibility = View.GONE
127+
}
116128
}
117129

118130
override fun onPause() {
119131
super.onPause()
120132
saveText()
133+
134+
removeTextWatcher()
135+
}
136+
137+
private fun removeTextWatcher() {
138+
//Avoid memory leak
139+
view.notes_view.removeTextChangedListener(textWatcher)
140+
}
141+
142+
private fun setWordCounter(text: Editable) {
143+
//Replace new lines with space
144+
val wordArray = text.toString().replace("\n", " ").split(" ")
145+
//Count only items which are not empty
146+
notes_counter.text = wordArray.count { it.isNotEmpty() }.toString()
147+
}
148+
149+
private var textWatcher: TextWatcher = object : TextWatcher {
150+
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
151+
}
152+
153+
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
154+
}
155+
156+
override fun afterTextChanged(editable: Editable) {
157+
setWordCounter(editable)
158+
}
121159
}
122160
}

app/src/main/kotlin/com/simplemobiletools/notes/helpers/Config.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Config(context: Context) : BaseConfig(context) {
2424
get() = prefs.getBoolean(SHOW_KEYBOARD, true)
2525
set(showKeyboard) = prefs.edit().putBoolean(SHOW_KEYBOARD, showKeyboard).apply()
2626

27+
var showWordCount: Boolean
28+
get() = prefs.getBoolean(SHOW_WORDCOUNT, false)
29+
set(showWordCount) = prefs.edit().putBoolean(SHOW_WORDCOUNT, showWordCount).apply()
30+
2731
var fontSize: Int
2832
get() = prefs.getInt(FONT_SIZE, FONT_SIZE_MEDIUM)
2933
set(size) = prefs.edit().putInt(FONT_SIZE, size).apply()

app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ val CLICKABLE_LINKS = "clickable_links"
1010
val WIDGET_NOTE_ID = "widget_note_id"
1111
val MONOSPACED_FONT = "monospaced_font"
1212
val SHOW_KEYBOARD = "show_keyboard"
13+
val SHOW_WORDCOUNT = "show_word_count"
1314
val FONT_SIZE = "font_size"
1415
val GRAVITY = "gravity"
1516
val CURSOR_PLACEMENT = "cursor_placement"

app/src/main/res/layout/activity_settings.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,29 @@
159159

160160
</RelativeLayout>
161161

162+
<RelativeLayout
163+
android:id="@+id/settings_show_word_count_holder"
164+
android:layout_width="match_parent"
165+
android:layout_height="wrap_content"
166+
android:layout_marginTop="@dimen/medium_margin"
167+
android:background="?attr/selectableItemBackground"
168+
android:paddingBottom="@dimen/bigger_margin"
169+
android:paddingLeft="@dimen/activity_margin"
170+
android:paddingRight="@dimen/activity_margin"
171+
android:paddingTop="@dimen/bigger_margin">
172+
173+
<com.simplemobiletools.commons.views.MySwitchCompat
174+
android:id="@+id/settings_show_word_count"
175+
android:layout_width="match_parent"
176+
android:layout_height="wrap_content"
177+
android:background="@null"
178+
android:clickable="false"
179+
android:paddingLeft="@dimen/medium_margin"
180+
android:paddingStart="@dimen/medium_margin"
181+
android:text="@string/show_wordcount"/>
182+
183+
</RelativeLayout>
184+
162185
<RelativeLayout
163186
android:id="@+id/settings_font_size_holder"
164187
android:layout_width="match_parent"

app/src/main/res/layout/fragment_note.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
android:layout_height="match_parent"
1212
android:fillViewport="true">
1313

14+
<RelativeLayout
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content">
17+
1418
<com.simplemobiletools.commons.views.MyEditText
1519
android:id="@+id/notes_view"
1620
android:layout_width="match_parent"
@@ -22,5 +26,18 @@
2226
android:padding="@dimen/activity_margin"
2327
android:textCursorDrawable="@null"/>
2428

29+
<TextView
30+
android:id="@+id/notes_counter"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:textStyle="italic"
34+
android:text="123"
35+
android:padding="5dp"
36+
android:layout_alignParentBottom="true"
37+
android:layout_alignParentEnd="true"
38+
android:layout_alignParentRight="true" />
39+
40+
</RelativeLayout>
41+
2542
</ScrollView>
2643
</RelativeLayout>

app/src/main/res/values-de/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<string name="place_cursor_end">Platziere Cursor am Ende der Notiz</string>
3333
<string name="monospaced_font">Benutze Monospace Schrift</string>
3434
<string name="show_keyboard">Zeige Tastatur beim Start</string>
35+
<string name="show_wordcount">Zeige Wort Zähler</string>
3536
<string name="alignment">Ausrichtung</string>
3637
<string name="left">Linksbündig</string>
3738
<string name="center">Zentriert</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<string name="place_cursor_end">Place cursor to the end of note</string>
3333
<string name="monospaced_font">Use monospaced font</string>
3434
<string name="show_keyboard">Show keyboard on startup</string>
35+
<string name="show_wordcount">Show word counter</string>
3536
<string name="alignment">Alignment</string>
3637
<string name="left">Left</string>
3738
<string name="center">Center</string>

0 commit comments

Comments
 (0)