Add optional Word Count to Note

- Setting included (default: deactived)
- Translation for en/de

refs #40
This commit is contained in:
Christopher
2017-11-20 22:39:01 +01:00
parent 11b57f1bda
commit e763f05ec8
8 changed files with 94 additions and 0 deletions

View File

@ -35,6 +35,7 @@ class SettingsActivity : SimpleActivity() {
setupClickableLinks()
setupMonospacedFont()
setupShowKeyboard()
setupShowWordCount()
setupFontSize()
setupGravity()
setupWidgetNote()
@ -90,6 +91,14 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupShowWordCount() {
settings_show_word_count.isChecked = config.showWordCount
settings_show_word_count_holder.setOnClickListener {
settings_show_word_count.toggle()
config.showWordCount = settings_show_word_count.isChecked
}
}
private fun setupFontSize() {
settings_font_size.text = getFontSizeText()
settings_font_size_holder.setOnClickListener {

View File

@ -3,6 +3,8 @@ package com.simplemobiletools.notes.fragments
import android.graphics.Typeface
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.TypedValue
@ -95,6 +97,7 @@ class NoteFragment : Fragment() {
super.onResume()
val config = context!!.config
view.notes_view.apply {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
@ -113,10 +116,45 @@ class NoteFragment : Fragment() {
setSelection(if (config.placeCursorToEnd) text.length else 0)
}
}
if (config.showWordCount) {
view.notes_view.addTextChangedListener(textWatcher)
view.notes_counter.visibility = View.VISIBLE
setWordCounter(view.notes_view.text)
}
else {
view.notes_counter.visibility = View.GONE
}
}
override fun onPause() {
super.onPause()
saveText()
removeTextWatcher()
}
private fun removeTextWatcher() {
//Avoid memory leak
view.notes_view.removeTextChangedListener(textWatcher)
}
private fun setWordCounter(text: Editable) {
//Replace new lines with space
val wordArray = text.toString().replace("\n", " ").split(" ")
//Count only items which are not empty
notes_counter.text = wordArray.count { it.isNotEmpty() }.toString()
}
private var textWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(editable: Editable) {
setWordCounter(editable)
}
}
}

View File

@ -24,6 +24,10 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getBoolean(SHOW_KEYBOARD, true)
set(showKeyboard) = prefs.edit().putBoolean(SHOW_KEYBOARD, showKeyboard).apply()
var showWordCount: Boolean
get() = prefs.getBoolean(SHOW_WORDCOUNT, false)
set(showWordCount) = prefs.edit().putBoolean(SHOW_WORDCOUNT, showWordCount).apply()
var fontSize: Int
get() = prefs.getInt(FONT_SIZE, FONT_SIZE_MEDIUM)
set(size) = prefs.edit().putInt(FONT_SIZE, size).apply()

View File

@ -10,6 +10,7 @@ val CLICKABLE_LINKS = "clickable_links"
val WIDGET_NOTE_ID = "widget_note_id"
val MONOSPACED_FONT = "monospaced_font"
val SHOW_KEYBOARD = "show_keyboard"
val SHOW_WORDCOUNT = "show_word_count"
val FONT_SIZE = "font_size"
val GRAVITY = "gravity"
val CURSOR_PLACEMENT = "cursor_placement"