Commit 6b2e1892 by Paktalin

Empty word items may be added as well

parent 41f1256d
package com.paktalin.vocabularynotebook.ui.fragments package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.* import com.paktalin.vocabularynotebook.utils.*
...@@ -8,13 +10,25 @@ import kotlinx.android.synthetic.main.content_main.* ...@@ -8,13 +10,25 @@ import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.fragment_editable_word.* import kotlinx.android.synthetic.main.fragment_editable_word.*
class AddWordFragment : WordFragment() { class AddWordFragment : WordFragment() {
private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
word.setOnTouchListener { _, _ -> word.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout); false } visible(mainActivity.btnSubmitLayout);false }
translation.setOnTouchListener { _, _ -> translation.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout); false } visible(mainActivity.btnSubmitLayout); false }
word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() })
translation.addTextChangedListener(textWatcher {
translationEmpty = translation.text.isEmpty() })
} }
override fun cancelEditing() { override fun cancelEditing() {
...@@ -35,8 +49,15 @@ class AddWordFragment : WordFragment() { ...@@ -35,8 +49,15 @@ class AddWordFragment : WordFragment() {
mainActivity.vocabularyFragment.addWord(wordItem) mainActivity.vocabularyFragment.addWord(wordItem)
} }
override fun updateButtons() { private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
super.updateButtons() return object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { setEmpty() }
}
}
private fun updateButtons() {
if (!wordEmpty || !translationEmpty) visible(btnClear) if (!wordEmpty || !translationEmpty) visible(btnClear)
if (wordEmpty && translationEmpty) invisible(btnClear) if (wordEmpty && translationEmpty) invisible(btnClear)
} }
......
...@@ -19,12 +19,6 @@ import kotlinx.android.synthetic.main.content_main.* ...@@ -19,12 +19,6 @@ import kotlinx.android.synthetic.main.content_main.*
abstract class WordFragment : Fragment() { abstract class WordFragment : Fragment() {
protected lateinit var mainActivity: MainActivity protected lateinit var mainActivity: MainActivity
internal var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
internal var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_editable_word, container, false) return inflater.inflate(R.layout.fragment_editable_word, container, false)
} }
...@@ -34,28 +28,10 @@ abstract class WordFragment : Fragment() { ...@@ -34,28 +28,10 @@ abstract class WordFragment : Fragment() {
mainActivity = activity as MainActivity mainActivity = activity as MainActivity
mainActivity.btnSubmit.setOnClickListener { submitWord() } mainActivity.btnSubmit.setOnClickListener { submitWord() }
mainActivity.btnCancel.setOnClickListener { cancelEditing() } mainActivity.btnCancel.setOnClickListener { cancelEditing() }
word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() })
translation.addTextChangedListener(textWatcher {
translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener { clearFields() } btnClear.setOnClickListener { clearFields() }
} }
open fun updateButtons() {
if (!wordEmpty && !translationEmpty) visible(mainActivity.btnSubmit)
if (wordEmpty || translationEmpty) gone(mainActivity.btnSubmit)
}
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
return object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { setEmpty() }
}
}
fun submitWord() { fun submitWord() {
mainActivity.hideKeyboardNotFromActivity(mainActivity) mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout) gone(mainActivity.btnSubmitLayout)
......
...@@ -96,7 +96,6 @@ ...@@ -96,7 +96,6 @@
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_margin="@dimen/small_margin" android:layout_margin="@dimen/small_margin"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:visibility="gone"
app:srcCompat="@drawable/ic_done_icon" app:srcCompat="@drawable/ic_done_icon"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
</LinearLayout> </LinearLayout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment