Commit dc5e6c99 by Paktalin

Refactored AddWordFragment

parent e81663b6
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.view.LayoutInflater
import android.text.TextWatcher import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.invisible
import com.paktalin.vocabularynotebook.utils.visible import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.fragment_editable_word.* import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.submit.* import kotlinx.android.synthetic.main.submit.*
class AddWordFragment : WordFragment() { open class AddWordFragment : Fragment() {
private var wordEmpty: Boolean = true private lateinit var mainActivity: MainActivity
set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_editable_word, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.btnSubmitWord.setOnClickListener { submit() }
mainActivity.btnCancelWord.setOnClickListener { cancel() }
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 cancel() {
clearFields()
gone(mainActivity.btnSubmitLayout)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
} }
override fun save(word:String, translation:String) { private fun submit() {
// TODO addAll entry point finish()
clearFields() mainActivity.vocabularyAdapter.addWord(WordPojo(word.text.toString(), translation.text.toString()))
mainActivity.vocabularyAdapter.addWord(WordPojo(word, translation))
this.word.requestFocus() this.word.requestFocus()
return
} }
private fun textWatcher(setEmpty: () -> Unit): TextWatcher { private fun cancel() {
return object : TextWatcher { finish()
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } mainActivity.hideKeyboardNotFromActivity(mainActivity)
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { setEmpty() }
}
} }
private fun updateButtons() { private fun finish() {
if (!wordEmpty || !translationEmpty) visible(btnClear) word.text.clear()
if (wordEmpty && translationEmpty) invisible(btnClear) translation.text.clear()
gone(mainActivity.btnSubmitLayout)
} }
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName } companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
......
package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.submit.*
abstract class WordFragment : Fragment() {
protected lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_editable_word, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.btnSubmitWord.setOnClickListener { submit() }
mainActivity.btnCancelWord.setOnClickListener { cancel() }
btnClear.setOnClickListener { clearFields() }
}
private fun submit() {
gone(mainActivity.btnSubmitLayout)
save(word.text.toString(), translation.text.toString())
return
}
protected fun clearFields() {
word.text.clear()
translation.text.clear()
}
protected abstract fun cancel()
protected abstract fun save(word:String, translation:String)
}
\ No newline at end of file
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