Commit a046a823 by Paktalin

Fixed bug with wrong clickListener

parent d14cc276
...@@ -90,11 +90,11 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity ...@@ -90,11 +90,11 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
} else container.id = 18071999 } else container.id = 18071999
// start EditWordFragment // start EditWordFragment
val wordInfoFragment = EditWordFragment() val editWordFragment = EditWordFragment()
val arguments = Bundle() val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem) arguments.putSerializable("wordItem", wordItem)
wordInfoFragment.arguments = arguments editWordFragment.arguments = arguments
(activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, wordInfoFragment).commit() (activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, editWordFragment).commit()
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......
...@@ -13,10 +13,6 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p ...@@ -13,10 +13,6 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
init { init {
if (time == null) time = Date(System.currentTimeMillis()) if (time == null) time = Date(System.currentTimeMillis())
} }
fun print(): String {
return "word: $word; translation: $translation; time: $time"
}
} }
constructor(pojo: Pojo, id: String, vocabularyId: String) constructor(pojo: Pojo, id: String, vocabularyId: String)
...@@ -24,7 +20,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p ...@@ -24,7 +20,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
fun delete() { fun delete() {
ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId) ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId)
.collection("WORDS").document(id).delete() .collection("words").document(id).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") } .addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) } .addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
} }
......
package com.paktalin.vocabularynotebook.ui package com.paktalin.vocabularynotebook.ui
import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class AddWordFragment : WordFragment() { class AddWordFragment : WordFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) { override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar() mainActivity.showProgressBar()
ConfiguredFirestore.instance ConfiguredFirestore.instance
......
...@@ -7,6 +7,7 @@ import android.view.LayoutInflater ...@@ -7,6 +7,7 @@ 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.ImageView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
...@@ -22,7 +23,7 @@ class EditWordFragment : WordFragment() { ...@@ -22,7 +23,7 @@ class EditWordFragment : WordFragment() {
mainActivity = activity as MainActivity mainActivity = activity as MainActivity
hidePreviousViews(container) hidePreviousViews(container)
wordItem = arguments!!["wordItem"] as WordItem wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.fragment_new_word, container, false) return super.onCreateView(inflater, container, savedInstanceState)
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
...@@ -60,7 +61,12 @@ class EditWordFragment : WordFragment() { ...@@ -60,7 +61,12 @@ class EditWordFragment : WordFragment() {
hideSubmitButton() hideSubmitButton()
mainActivity.hideProgressBar() mainActivity.hideProgressBar()
wordItem.pojo = wordPojo wordItem.pojo = wordPojo
updateRecycleView(wordItem) } updateRecycleView(wordItem)
// 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()} }
......
...@@ -11,6 +11,7 @@ import com.paktalin.vocabularynotebook.R ...@@ -11,6 +11,7 @@ import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager import android.view.WindowManager
import android.app.Activity import android.app.Activity
import android.support.v4.app.Fragment
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
...@@ -80,7 +81,8 @@ class MainActivity : AppCompatActivity() { ...@@ -80,7 +81,8 @@ class MainActivity : AppCompatActivity() {
val arguments = Bundle() val arguments = Bundle()
arguments.putString("vocabularyId", vocabularyId) arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment).commitNowAllowingStateLoss() supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment)
.commitNowAllowingStateLoss()
} else { } else {
Log.w(TAG, "There's no collection \"vocabularies\"") Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() } showToastNoWords() }
...@@ -94,9 +96,7 @@ class MainActivity : AppCompatActivity() { ...@@ -94,9 +96,7 @@ class MainActivity : AppCompatActivity() {
fun hideKeyboardNotFromActivity(activity: Activity) { fun hideKeyboardNotFromActivity(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = activity.currentFocus var view = activity.currentFocus
if (view == null) { if (view == null) { view = View(activity) }
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0) imm.hideSoftInputFromWindow(view.windowToken, 0)
} }
...@@ -106,7 +106,11 @@ class MainActivity : AppCompatActivity() { ...@@ -106,7 +106,11 @@ class MainActivity : AppCompatActivity() {
fun showToastNoWords() { fun showToastNoWords() {
Toast.makeText(this@MainActivity, Toast.makeText(this@MainActivity,
"You don't have any WORDS yet. Add your fist one!", Toast.LENGTH_SHORT).show() "You don't have any words yet. Add your fist one!", Toast.LENGTH_SHORT).show()
}
fun removeFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss()
} }
override fun onPause() { override fun onPause() {
......
...@@ -30,7 +30,6 @@ class VocabularyFragment : Fragment() { ...@@ -30,7 +30,6 @@ class VocabularyFragment : Fragment() {
return inflater.inflate(R.layout.fragment_vocabulary, container, false) return inflater.inflate(R.layout.fragment_vocabulary, container, false)
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
setEmptyAdapter() setEmptyAdapter()
...@@ -57,7 +56,7 @@ class VocabularyFragment : Fragment() { ...@@ -57,7 +56,7 @@ class VocabularyFragment : Fragment() {
if (it.documents.size != 0) if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId) setVocabularyAdapter(it.documents, vocabularyId)
else { else {
Log.i(TAG, "There are no documents in collection \"WORDS\"") Log.i(TAG, "There are no documents in collection \"words\"")
(activity as MainActivity).showToastNoWords() (activity as MainActivity).showToastNoWords()
}} }}
} }
......
...@@ -4,7 +4,9 @@ import android.os.Bundle ...@@ -4,7 +4,9 @@ import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageButton import android.widget.ImageButton
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
...@@ -18,6 +20,7 @@ abstract class WordFragment : Fragment() { ...@@ -18,6 +20,7 @@ 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() }
...@@ -25,8 +28,14 @@ abstract class WordFragment : Fragment() { ...@@ -25,8 +28,14 @@ abstract class WordFragment : Fragment() {
private var translationEmpty: Boolean = true private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() } set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() }
word.addTextChangedListener(textWatcher { word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() }) wordEmpty = word.text.isEmpty() })
...@@ -34,10 +43,8 @@ abstract class WordFragment : Fragment() { ...@@ -34,10 +43,8 @@ abstract class WordFragment : Fragment() {
translationEmpty = translation.text.isEmpty() }) translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener { clearFields() } btnClear.setOnClickListener { clearFields() }
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() }
} }
private fun updateButtons() { private fun updateButtons() {
if (!wordEmpty || !translationEmpty) if (!wordEmpty || !translationEmpty)
showClearButton() showClearButton()
...@@ -58,24 +65,25 @@ abstract class WordFragment : Fragment() { ...@@ -58,24 +65,25 @@ abstract class WordFragment : Fragment() {
} }
private fun showSubmitButton() { private fun showSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE } mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE }
protected fun hideSubmitButton() { protected fun hideSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE } mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE }
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE } private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE } private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun submitWord() { fun submitWord() {
(activity as MainActivity).hideKeyboardNotFromActivity(activity as MainActivity) mainActivity.hideKeyboardNotFromActivity(mainActivity)
val word = word.text.toString() val word = word.text.toString()
val translation = translation.text.toString() val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId val vocabularyId = mainActivity.vocabularyId
val wordPojo = WordItem.Pojo(word, translation, null) val wordPojo = WordItem.Pojo(word, translation, null)
saveToFirestore(wordPojo, vocabularyId) saveToFirestore(wordPojo, vocabularyId)
return
} }
protected fun clearFields() { protected fun clearFields() {
......
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