Skip to content

Commit 7ecde39

Browse files
authored
Merge pull request #141 from chrjsorg/feature/chrjsorg/40-word-count
Add optional Word Count to Note
2 parents 11b57f1 + b391772 commit 7ecde39

File tree

21 files changed

+108
-2
lines changed

21 files changed

+108
-2
lines changed

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: 36 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
@@ -18,6 +20,7 @@ import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
1820
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
1921
import com.simplemobiletools.notes.helpers.NOTE_ID
2022
import com.simplemobiletools.notes.models.Note
23+
import kotlinx.android.synthetic.main.fragment_note.*
2124
import kotlinx.android.synthetic.main.fragment_note.view.*
2225
import 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
}

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_WORD_COUNT, false)
29+
set(showWordCount) = prefs.edit().putBoolean(SHOW_WORD_COUNT, 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_WORD_COUNT = "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_word_count"/>
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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
44
android:id="@+id/note_Fragment_holder"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent">
@@ -11,6 +11,11 @@
1111
android:layout_height="match_parent"
1212
android:fillViewport="true">
1313

14+
<RelativeLayout
15+
android:layout_width="match_parent"
16+
android:id="@+id/notes_relative_layout"
17+
android:layout_height="wrap_content">
18+
1419
<com.simplemobiletools.commons.views.MyEditText
1520
android:id="@+id/notes_view"
1621
android:layout_width="match_parent"
@@ -22,5 +27,18 @@
2227
android:padding="@dimen/activity_margin"
2328
android:textCursorDrawable="@null"/>
2429

30+
<TextView
31+
android:id="@+id/notes_counter"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:textStyle="italic"
35+
tools:text="123"
36+
android:padding="5dp"
37+
android:layout_alignParentBottom="true"
38+
android:layout_alignParentEnd="true"
39+
android:layout_alignParentRight="true" />
40+
41+
</RelativeLayout>
42+
2543
</ScrollView>
2644
</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_word_count">Zeige Wortanzahl</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-es/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_word_count">Show word count</string>
3536
<string name="alignment">Alineación del texto</string>
3637
<string name="left">Izquierda</string>
3738
<string name="center">Centrado</string>

app/src/main/res/values-fr/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">Placer le curseur à la fin de la 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_word_count">Show word count</string>
3536
<string name="alignment">Alignment</string>
3637
<string name="left">Gauche</string>
3738
<string name="center">Centre</string>

app/src/main/res/values-hu/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_word_count">Show word count</string>
3536
<string name="alignment">Igazítottság</string>
3637
<string name="left">Bal</string>
3738
<string name="center">Közép</string>

0 commit comments

Comments
 (0)