Commit c4329ccd by Paktalin

Refactoring

parent a046a823
...@@ -13,6 +13,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary ...@@ -13,6 +13,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.EditWordFragment import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity import com.paktalin.vocabularynotebook.ui.MainActivity
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
...@@ -98,9 +99,9 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity ...@@ -98,9 +99,9 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.word) val tvWord: TextView = itemView.word
val tvTranslation: TextView = itemView.findViewById(R.id.translation) val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.findViewById(R.id.layout) val layout: LinearLayout = itemView.layout
} }
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName } companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
......
...@@ -7,14 +7,12 @@ import android.view.LayoutInflater ...@@ -7,14 +7,12 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
import kotlinx.android.synthetic.main.notebook_sheet.*
import kotlinx.android.synthetic.main.word_item.view.*
class EditWordFragment : WordFragment() { class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem private lateinit var wordItem: WordItem
...@@ -45,9 +43,9 @@ class EditWordFragment : WordFragment() { ...@@ -45,9 +43,9 @@ class EditWordFragment : WordFragment() {
private fun hidePreviousViews(container: ViewGroup?) { private fun hidePreviousViews(container: ViewGroup?) {
if (container != null) { if (container != null) {
container.findViewById<ImageView>(R.id.line).visibility = View.GONE container.line.visibility = View.GONE
container.findViewById<TextView>(R.id.word).visibility = View.GONE container.word.visibility = View.GONE
container.findViewById<TextView>(R.id.translation).visibility = View.GONE container.translation.visibility = View.GONE
} }
} }
...@@ -62,18 +60,26 @@ class EditWordFragment : WordFragment() { ...@@ -62,18 +60,26 @@ class EditWordFragment : WordFragment() {
mainActivity.hideProgressBar() mainActivity.hideProgressBar()
wordItem.pojo = wordPojo wordItem.pojo = wordPojo
updateRecycleView(wordItem) updateRecycleView(wordItem)
stop()
// set onClickListener from AddWordFragment }
mainActivity.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener {
(mainActivity.supportFragmentManager.findFragmentById(R.id.fragment_new_word) as AddWordFragment).submitWord()}
mainActivity.removeFragment(this@EditWordFragment) }
.addOnFailureListener { .addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace()) Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
Toast.makeText(mainActivity, "Couldn't update the word", Toast.LENGTH_SHORT).show()} } Toast.makeText(mainActivity, "Couldn't update the word", Toast.LENGTH_SHORT).show()
stop()
}
}
private fun stop() {
// set onClickListener from AddWordFragment
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentNewWord as AddWordFragment).submitWord() }
mainActivity.removeFragment(this)
}
override fun updateRecycleView(wordItem: WordItem) { override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.updateWordItem(wordItem) mainActivity.vocabularyFragment.updateWordItem(wordItem)
} }
companion object { private val TAG = "VN/" + EditWordFragment::class.java.simpleName } companion object {
private val TAG = "VN/" + EditWordFragment::class.java.simpleName
}
} }
\ No newline at end of file
...@@ -7,11 +7,10 @@ import android.text.TextWatcher ...@@ -7,11 +7,10 @@ import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
import kotlinx.android.synthetic.main.notebook_sheet.*
abstract class WordFragment : Fragment() { abstract class WordFragment : Fragment() {
...@@ -20,7 +19,6 @@ abstract class WordFragment : Fragment() { ...@@ -20,7 +19,6 @@ abstract class WordFragment : Fragment() {
const val WORDS = "words" const val WORDS = "words"
} }
protected lateinit var mainActivity: MainActivity protected lateinit var mainActivity: MainActivity
open var TAG = WordFragment::class.simpleName
private var wordEmpty: Boolean = true private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() } set(value) { field = value; updateButtons() }
...@@ -35,7 +33,7 @@ abstract class WordFragment : Fragment() { ...@@ -35,7 +33,7 @@ abstract class WordFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity mainActivity = activity as MainActivity
mainActivity.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() } mainActivity.btnSubmit.setOnClickListener { submitWord() }
word.addTextChangedListener(textWatcher { word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() }) wordEmpty = word.text.isEmpty() })
...@@ -65,10 +63,10 @@ abstract class WordFragment : Fragment() { ...@@ -65,10 +63,10 @@ abstract class WordFragment : Fragment() {
} }
private fun showSubmitButton() { private fun showSubmitButton() {
mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE } mainActivity.btnSubmitLayout.visibility = View.VISIBLE }
protected fun hideSubmitButton() { protected fun hideSubmitButton() {
mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE } mainActivity.btnSubmitLayout.visibility = View.GONE }
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE } private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
android:orientation="vertical"> android:orientation="vertical">
<fragment <fragment
android:id="@+id/fragment_new_word" android:id="@+id/fragmentNewWord"
android:name="com.paktalin.vocabularynotebook.ui.AddWordFragment" android:name="com.paktalin.vocabularynotebook.ui.AddWordFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
app:layout_constraintBottom_toBottomOf="parent"> app:layout_constraintBottom_toBottomOf="parent">
<ImageButton <ImageButton
android:id="@+id/btnAddWord" android:id="@+id/btnSubmit"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="end" android:layout_gravity="end"
......
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