Commit dc5e6c99 by Paktalin

Refactored AddWordFragment

parent e81663b6
package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
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.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.invisible
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.submit.*
class AddWordFragment : WordFragment() {
private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
open class AddWordFragment : Fragment() {
private 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() }
word.setOnTouchListener { _, _ ->
visible(mainActivity.btnSubmitLayout);false }
translation.setOnTouchListener { _, _ ->
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) {
// TODO addAll entry point
clearFields()
mainActivity.vocabularyAdapter.addWord(WordPojo(word, translation))
private fun submit() {
finish()
mainActivity.vocabularyAdapter.addWord(WordPojo(word.text.toString(), translation.text.toString()))
this.word.requestFocus()
return
}
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() }
}
private fun cancel() {
finish()
mainActivity.hideKeyboardNotFromActivity(mainActivity)
}
private fun updateButtons() {
if (!wordEmpty || !translationEmpty) visible(btnClear)
if (wordEmpty && translationEmpty) invisible(btnClear)
private fun finish() {
word.text.clear()
translation.text.clear()
gone(mainActivity.btnSubmitLayout)
}
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